AndroidAPS/app/src/main/java/info/nightscout/androidaps/MainActivity.java

388 lines
17 KiB
Java
Raw Normal View History

2016-06-04 17:28:05 +02:00
package info.nightscout.androidaps;
2017-02-10 14:24:56 +01:00
import android.content.Context;
import android.content.Intent;
2016-07-03 19:21:41 +02:00
import android.content.pm.PackageManager;
2017-02-10 14:24:56 +01:00
import android.graphics.Rect;
2016-07-19 11:31:58 +02:00
import android.os.Bundle;
2018-06-03 23:28:04 +02:00
import android.os.PersistableBundle;
2018-02-10 14:53:43 +01:00
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
2018-06-04 21:34:21 +02:00
import android.util.TypedValue;
2018-06-03 23:28:04 +02:00
import android.view.Menu;
2017-05-04 19:13:56 +02:00
import android.view.MenuItem;
2017-02-10 14:24:56 +01:00
import android.view.MotionEvent;
import android.view.View;
2019-10-22 23:01:38 +02:00
import android.view.WindowManager;
2017-02-10 14:24:56 +01:00
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
2018-06-04 21:34:21 +02:00
import android.widget.LinearLayout;
2018-02-10 14:53:43 +01:00
import android.widget.TextView;
2016-06-04 17:28:05 +02:00
2019-10-15 20:44:03 +02:00
import androidx.annotation.NonNull;
2019-08-08 18:39:56 +02:00
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;
2016-07-08 00:17:02 +02:00
import com.joanzapata.iconify.Iconify;
import com.joanzapata.iconify.fonts.FontAwesomeModule;
2016-06-13 22:53:41 +02:00
2016-06-05 01:40:35 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-29 00:08:28 +02:00
import info.nightscout.androidaps.activities.AgreementActivity;
import info.nightscout.androidaps.activities.HistoryBrowseActivity;
2019-08-08 18:39:56 +02:00
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
2018-07-29 00:08:28 +02:00
import info.nightscout.androidaps.activities.PreferencesActivity;
import info.nightscout.androidaps.activities.SingleFragmentActivity;
2017-08-05 18:09:19 +02:00
import info.nightscout.androidaps.data.Profile;
2017-05-04 19:13:56 +02:00
import info.nightscout.androidaps.events.EventAppExit;
2019-10-22 23:01:38 +02:00
import info.nightscout.androidaps.events.EventPreferenceChange;
2019-10-15 20:44:03 +02:00
import info.nightscout.androidaps.events.EventRebuildTabs;
2016-06-14 23:45:55 +02:00
import info.nightscout.androidaps.interfaces.PluginBase;
2019-04-17 22:45:21 +02:00
import info.nightscout.androidaps.interfaces.PluginType;
2018-07-29 15:37:28 +02:00
import info.nightscout.androidaps.logging.L;
2019-04-17 22:45:21 +02:00
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
2019-10-14 23:16:51 +02:00
import info.nightscout.androidaps.plugins.bus.RxBus;
2019-02-28 23:16:50 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
2019-10-22 15:24:37 +02:00
import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt;
2019-10-22 23:01:38 +02:00
import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus;
2018-05-20 21:55:53 +02:00
import info.nightscout.androidaps.setupwizard.SetupWizardActivity;
2016-07-19 11:31:58 +02:00
import info.nightscout.androidaps.tabs.TabPageAdapter;
2019-02-26 20:38:27 +01:00
import info.nightscout.androidaps.utils.AndroidPermission;
2019-04-05 00:32:26 +02:00
import info.nightscout.androidaps.utils.FabricPrivacy;
2019-02-26 20:38:27 +01:00
import info.nightscout.androidaps.utils.LocaleHelper;
import info.nightscout.androidaps.utils.OKDialog;
import info.nightscout.androidaps.utils.PasswordProtection;
import info.nightscout.androidaps.utils.SP;
2019-10-14 23:16:51 +02:00
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
2016-06-04 17:28:05 +02:00
2019-08-08 18:39:56 +02:00
public class MainActivity extends NoSplashAppCompatActivity {
2018-07-29 15:37:28 +02:00
private static Logger log = LoggerFactory.getLogger(L.CORE);
2019-10-14 23:16:51 +02:00
private CompositeDisposable disposable = new CompositeDisposable();
2016-06-04 17:28:05 +02:00
2018-06-03 23:28:04 +02:00
private ActionBarDrawerToggle actionBarDrawerToggle;
2018-06-04 21:56:31 +02:00
private MenuItem pluginPreferencesMenuItem;
2016-06-04 17:28:05 +02:00
@Override
2019-08-08 18:39:56 +02:00
public void onCreate(Bundle savedInstanceState) {
2016-06-04 17:28:05 +02:00
super.onCreate(savedInstanceState);
2018-05-22 20:02:38 +02:00
2016-07-08 00:17:02 +02:00
Iconify.with(new FontAwesomeModule());
LocaleHelper.INSTANCE.update(getApplicationContext());
2018-06-03 23:28:04 +02:00
2016-06-04 17:28:05 +02:00
setContentView(R.layout.activity_main);
2018-06-03 23:28:04 +02:00
setSupportActionBar(findViewById(R.id.toolbar));
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_navigation, R.string.close_navigation);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
2017-05-04 19:13:56 +02:00
2019-10-22 23:01:38 +02:00
// initialize screen wake lock
processPreferenceChange(new EventPreferenceChange(R.string.key_keep_screen_on));
2018-05-22 23:06:17 +02:00
doMigrations();
2018-06-04 21:56:31 +02:00
final ViewPager viewPager = findViewById(R.id.pager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
checkPluginPreferences(viewPager);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
2019-04-17 22:45:21 +02:00
//Check here if loop plugin is disabled. Else check via constraints
if (!LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))
2019-04-22 14:10:12 +02:00
VersionCheckerUtilsKt.triggerCheckVersion();
2019-04-17 22:45:21 +02:00
2019-04-05 00:32:26 +02:00
FabricPrivacy.setUserStats();
2019-10-15 20:44:03 +02:00
setupTabs();
setupViews();
2019-10-14 23:16:51 +02:00
disposable.add(RxBus.INSTANCE
2019-10-15 20:44:03 +02:00
.toObservable(EventRebuildTabs.class)
2019-10-14 23:16:51 +02:00
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
LocaleHelper.INSTANCE.update(getApplicationContext());
2019-10-14 23:16:51 +02:00
if (event.getRecreate()) {
recreate();
} else {
2019-10-15 20:44:03 +02:00
setupTabs();
setupViews();
2019-10-14 23:16:51 +02:00
}
2019-10-22 23:01:38 +02:00
setWakeLock();
2019-10-14 23:16:51 +02:00
}, FabricPrivacy::logException)
);
2019-10-22 23:01:38 +02:00
disposable.add(RxBus.INSTANCE
.toObservable(EventPreferenceChange.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::processPreferenceChange, FabricPrivacy::logException)
);
2018-05-11 13:14:59 +02:00
if (!SP.getBoolean(R.string.key_setupwizard_processed, false)) {
Intent intent = new Intent(this, SetupWizardActivity.class);
startActivity(intent);
2018-05-22 23:06:17 +02:00
} else {
checkEula();
2018-05-11 13:14:59 +02:00
}
2018-05-22 23:06:17 +02:00
AndroidPermission.notifyForStoragePermission(this);
AndroidPermission.notifyForBatteryOptimizationPermission(this);
2018-08-07 21:07:43 +02:00
if (Config.PUMPDRIVERS) {
2018-06-06 22:58:50 +02:00
AndroidPermission.notifyForLocationPermissions(this);
AndroidPermission.notifyForSMSPermissions(this);
}
2019-10-15 20:44:03 +02:00
}
2018-05-22 23:06:17 +02:00
private void checkPluginPreferences(ViewPager viewPager) {
if (pluginPreferencesMenuItem == null) return;
if (((TabPageAdapter) viewPager.getAdapter()).getPluginAt(viewPager.getCurrentItem()).getPreferencesId() != -1)
pluginPreferencesMenuItem.setEnabled(true);
else pluginPreferencesMenuItem.setEnabled(false);
}
@Override
public void onPostCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
actionBarDrawerToggle.syncState();
}
2019-10-14 23:16:51 +02:00
@Override
public void onDestroy() {
super.onDestroy();
2019-10-14 23:16:51 +02:00
disposable.clear();
}
2019-10-22 23:01:38 +02:00
private void setWakeLock() {
boolean keepScreenOn = SP.getBoolean(R.string.key_keep_screen_on, false);
if (keepScreenOn)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
else
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
public void processPreferenceChange(final EventPreferenceChange ev) {
if (ev.isChanged(R.string.key_keep_screen_on))
setWakeLock();
}
2019-10-15 20:44:03 +02:00
private void setupViews() {
2016-08-21 22:14:33 +02:00
TabPageAdapter pageAdapter = new TabPageAdapter(getSupportFragmentManager(), this);
2018-06-03 23:28:04 +02:00
NavigationView navigationView = findViewById(R.id.navigation_view);
2019-10-15 20:44:03 +02:00
navigationView.setNavigationItemSelectedListener(menuItem -> true);
2018-06-03 23:28:04 +02:00
Menu menu = navigationView.getMenu();
menu.clear();
2016-08-05 23:54:03 +02:00
for (PluginBase p : MainApp.getPluginsList()) {
pageAdapter.registerNewFragment(p);
2018-06-06 22:58:50 +02:00
if (p.hasFragment() && !p.isFragmentVisible() && p.isEnabled(p.pluginDescription.getType()) && !p.pluginDescription.neverVisible) {
2018-06-03 23:28:04 +02:00
MenuItem menuItem = menu.add(p.getName());
menuItem.setCheckable(true);
menuItem.setOnMenuItemClickListener(item -> {
Intent intent = new Intent(this, SingleFragmentActivity.class);
intent.putExtra("plugin", MainApp.getPluginsList().indexOf(p));
startActivity(intent);
2018-06-09 13:17:26 +02:00
((DrawerLayout) findViewById(R.id.drawer_layout)).closeDrawers();
2018-06-03 23:28:04 +02:00
return true;
});
}
2016-06-13 22:53:41 +02:00
}
2018-06-03 23:28:04 +02:00
ViewPager mPager = findViewById(R.id.pager);
2016-06-13 22:53:41 +02:00
mPager.setAdapter(pageAdapter);
2019-10-15 20:44:03 +02:00
//if (switchToLast)
// mPager.setCurrentItem(pageAdapter.getCount() - 1, false);
2018-06-04 21:56:31 +02:00
checkPluginPreferences(mPager);
2016-06-04 17:28:05 +02:00
}
2016-06-05 01:40:35 +02:00
2018-06-04 21:34:21 +02:00
private void setupTabs() {
ViewPager viewPager = findViewById(R.id.pager);
TabLayout normalTabs = findViewById(R.id.tabs_normal);
normalTabs.setupWithViewPager(viewPager, true);
TabLayout compactTabs = findViewById(R.id.tabs_compact);
compactTabs.setupWithViewPager(viewPager, true);
Toolbar toolbar = findViewById(R.id.toolbar);
if (SP.getBoolean("short_tabtitles", false)) {
normalTabs.setVisibility(View.GONE);
compactTabs.setVisibility(View.VISIBLE);
toolbar.setLayoutParams(new LinearLayout.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.compact_height)));
} else {
normalTabs.setVisibility(View.VISIBLE);
compactTabs.setVisibility(View.GONE);
TypedValue typedValue = new TypedValue();
if (getTheme().resolveAttribute(R.attr.actionBarSize, typedValue, true)) {
toolbar.setLayoutParams(new LinearLayout.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT,
TypedValue.complexToDimensionPixelSize(typedValue.data, getResources().getDisplayMetrics())));
}
}
}
2016-07-07 18:04:36 +02:00
private void checkEula() {
2017-02-22 20:29:41 +01:00
//SP.removeBoolean(R.string.key_i_understand);
boolean IUnderstand = SP.getBoolean(R.string.key_i_understand, false);
2016-07-07 18:04:36 +02:00
if (!IUnderstand) {
Intent intent = new Intent(getApplicationContext(), AgreementActivity.class);
startActivity(intent);
finish();
}
}
private void doMigrations() {
checkUpgradeToProfileTarget();
// guarantee that the unreachable threshold is at least 30 and of type String
// Added in 1.57 at 21.01.2018
2019-10-15 20:44:03 +02:00
int unreachable_threshold = SP.getInt(R.string.key_pump_unreachable_threshold, 30);
SP.remove(R.string.key_pump_unreachable_threshold);
2018-01-29 22:48:47 +01:00
if (unreachable_threshold < 30) unreachable_threshold = 30;
2019-10-15 20:44:03 +02:00
SP.putString(R.string.key_pump_unreachable_threshold, Integer.toString(unreachable_threshold));
}
2017-08-05 18:09:19 +02:00
private void checkUpgradeToProfileTarget() { // TODO: can be removed in the future
boolean oldKeyExists = SP.contains("openapsma_min_bg");
if (oldKeyExists) {
Profile profile = ProfileFunctions.getInstance().getProfile();
2017-08-05 18:09:19 +02:00
String oldRange = SP.getDouble("openapsma_min_bg", 0d) + " - " + SP.getDouble("openapsma_max_bg", 0d);
String newRange = "";
if (profile != null) {
newRange = profile.getTargetLow() + " - " + profile.getTargetHigh();
}
String message = "Target range is changed in current version.\n\nIt's not taken from preferences but from profile.\n\n!!! REVIEW YOUR SETTINGS !!!";
message += "\n\nOld settings: " + oldRange;
message += "\nProfile settings: " + newRange;
2019-10-15 20:44:03 +02:00
OKDialog.show(this, "Target range change", message, () -> {
SP.remove("openapsma_min_bg");
SP.remove("openapsma_max_bg");
SP.remove("openapsma_target_bg");
2017-08-05 18:09:19 +02:00
});
}
}
2016-11-24 18:46:16 +01:00
@Override
2019-10-15 20:44:03 +02:00
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
2016-11-25 01:47:59 +01:00
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (permissions.length != 0) {
if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) {
2016-11-25 11:24:56 +01:00
switch (requestCode) {
2018-05-22 17:34:09 +02:00
case AndroidPermission.CASE_STORAGE:
2016-11-25 11:24:56 +01:00
//show dialog after permission is granted
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage(R.string.alert_dialog_storage_permission_text);
alert.setPositiveButton(R.string.ok, null);
2016-11-25 11:24:56 +01:00
alert.show();
break;
2018-05-22 17:34:09 +02:00
case AndroidPermission.CASE_LOCATION:
case AndroidPermission.CASE_SMS:
case AndroidPermission.CASE_BATTERY:
case AndroidPermission.CASE_PHONE_STATE:
2016-11-25 11:24:56 +01:00
break;
}
2016-11-25 01:47:59 +01:00
}
2016-11-24 18:46:16 +01:00
}
}
2017-02-10 14:24:56 +01:00
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
2017-02-22 14:11:05 +01:00
if (v instanceof EditText) {
2017-02-10 14:24:56 +01:00
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
2017-02-22 14:11:05 +01:00
if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
2017-02-10 14:24:56 +01:00
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent(event);
}
2017-05-04 19:13:56 +02:00
@Override
2018-06-03 23:28:04 +02:00
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
2018-06-04 21:56:31 +02:00
pluginPreferencesMenuItem = menu.findItem(R.id.nav_plugin_preferences);
checkPluginPreferences(findViewById(R.id.pager));
2018-06-03 23:28:04 +02:00
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.nav_preferences:
PasswordProtection.QueryPassword(this, R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(this, PreferencesActivity.class);
i.putExtra("id", -1);
startActivity(i);
}, null);
return true;
case R.id.nav_historybrowser:
startActivity(new Intent(this, HistoryBrowseActivity.class));
return true;
case R.id.nav_setupwizard:
startActivity(new Intent(this, SetupWizardActivity.class));
return true;
case R.id.nav_about:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(MainApp.gs(R.string.app_name) + " " + BuildConfig.VERSION);
2019-03-02 14:37:53 +01:00
builder.setIcon(MainApp.getIcon());
2018-06-03 23:28:04 +02:00
String message = "Build: " + BuildConfig.BUILDVERSION + "\n";
message += "Flavor: " + BuildConfig.FLAVOR + BuildConfig.BUILD_TYPE + "\n";
2018-07-28 16:26:36 +02:00
message += MainApp.gs(R.string.configbuilder_nightscoutversion_label) + " " + NSSettingsStatus.getInstance().nightscoutVersionName;
2018-06-03 23:28:04 +02:00
if (MainApp.engineeringMode)
message += "\n" + MainApp.gs(R.string.engineering_mode_enabled);
message += MainApp.gs(R.string.about_link_urls);
final SpannableString messageSpanned = new SpannableString(message);
Linkify.addLinks(messageSpanned, Linkify.WEB_URLS);
builder.setMessage(messageSpanned);
builder.setPositiveButton(MainApp.gs(R.string.ok), null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
((TextView) alertDialog.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
return true;
case R.id.nav_exit:
log.debug("Exiting");
MainApp.instance().stopKeepAliveService();
2019-10-14 23:16:51 +02:00
RxBus.INSTANCE.send(new EventAppExit());
2018-06-03 23:28:04 +02:00
MainApp.closeDbHelper();
finish();
System.runFinalization();
System.exit(0);
return true;
2018-06-04 21:56:31 +02:00
case R.id.nav_plugin_preferences:
ViewPager viewPager = findViewById(R.id.pager);
final PluginBase plugin = ((TabPageAdapter) viewPager.getAdapter()).getPluginAt(viewPager.getCurrentItem());
PasswordProtection.QueryPassword(this, R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(this, PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId());
startActivity(i);
}, null);
return true;
2017-05-04 19:13:56 +02:00
}
2018-06-03 23:28:04 +02:00
return actionBarDrawerToggle.onOptionsItemSelected(item);
2017-05-04 19:13:56 +02:00
}
2016-11-25 12:00:15 +01:00
}