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

426 lines
18 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;
import android.os.PowerManager;
2019-05-16 13:57:37 +02:00
import androidx.annotation.Nullable;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;
import androidx.core.app.ActivityCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
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;
2017-07-03 00:02:54 +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
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
import com.squareup.otto.Subscribe;
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;
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;
import info.nightscout.androidaps.events.EventFeatureRunning;
2018-06-23 18:24:04 +02:00
import info.nightscout.androidaps.events.EventPreferenceChange;
2016-06-13 22:53:41 +02:00
import info.nightscout.androidaps.events.EventRefreshGui;
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-02-28 23:16:50 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus;
2019-04-10 00:36:02 +02:00
import info.nightscout.androidaps.plugins.general.versionChecker.VersionCheckerUtilsKt;
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;
2016-06-04 17:28:05 +02:00
2018-06-03 23:28:04 +02:00
public class MainActivity extends AppCompatActivity {
2018-07-29 15:37:28 +02:00
private static Logger log = LoggerFactory.getLogger(L.CORE);
2016-06-04 17:28:05 +02:00
2017-07-03 00:02:54 +02:00
protected PowerManager.WakeLock mWakeLock;
2017-05-04 19:13:56 +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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2018-05-22 20:02:38 +02:00
2018-07-29 15:37:28 +02:00
if (L.isEnabled(L.CORE))
2018-05-22 20:02:38 +02:00
log.debug("onCreate");
2016-07-08 00:17:02 +02:00
Iconify.with(new FontAwesomeModule());
2016-07-06 22:08:00 +02:00
LocaleHelper.onCreate(this, "en");
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
2018-06-23 18:24:04 +02:00
// initialize screen wake lock
onEventPreferenceChange(new EventPreferenceChange(R.string.key_keep_screen_on));
2018-05-22 23:06:17 +02:00
doMigrations();
registerBus();
2018-06-04 21:34:21 +02:00
setupTabs();
setupViews(false);
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();
2018-06-04 21:56:31 +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);
2018-05-22 23:06:17 +02:00
}
2018-06-03 23:28:04 +02:00
@Override
public void onPostCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
actionBarDrawerToggle.syncState();
}
2018-05-22 23:06:17 +02:00
@Override
protected void onResume() {
super.onResume();
2018-07-29 15:37:28 +02:00
if (L.isEnabled(L.CORE))
log.debug("onResume");
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);
}
2017-07-03 00:02:54 +02:00
2018-05-22 23:06:17 +02:00
MainApp.bus().post(new EventFeatureRunning(EventFeatureRunning.Feature.MAIN));
}
@Override
public void onDestroy() {
2018-07-29 15:37:28 +02:00
if (L.isEnabled(L.CORE))
log.debug("onDestroy");
2018-05-22 23:06:17 +02:00
if (mWakeLock != null)
if (mWakeLock.isHeld())
mWakeLock.release();
super.onDestroy();
2016-06-13 22:53:41 +02:00
}
2017-07-03 00:02:54 +02:00
@Subscribe
2018-06-23 18:24:04 +02:00
public void onEventPreferenceChange(final EventPreferenceChange ev) {
if (ev.isChanged(R.string.key_keep_screen_on)) {
boolean keepScreenOn = SP.getBoolean(R.string.key_keep_screen_on, false);
2018-06-23 18:24:04 +02:00
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (keepScreenOn) {
2019-04-08 16:48:19 +02:00
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "AndroidAPS:MainActivity_onEventPreferenceChange");
2018-06-23 18:24:04 +02:00
if (!mWakeLock.isHeld())
mWakeLock.acquire();
} else {
if (mWakeLock != null && mWakeLock.isHeld())
mWakeLock.release();
}
2017-07-03 00:02:54 +02:00
}
}
2016-06-13 22:53:41 +02:00
@Subscribe
public void onStatusEvent(final EventRefreshGui ev) {
2018-10-04 23:04:32 +02:00
String lang = SP.getString(R.string.key_language, "en");
2016-07-06 22:50:25 +02:00
LocaleHelper.setLocale(getApplicationContext(), lang);
2018-06-04 21:34:21 +02:00
runOnUiThread(() -> {
if (ev.recreate) {
recreate();
} else {
try { // activity may be destroyed
setupTabs();
setupViews(true);
} catch (IllegalStateException e) {
log.error("Unhandled exception", e);
2016-12-03 17:42:08 +01:00
}
}
2018-06-04 21:34:21 +02:00
2018-08-07 21:07:43 +02:00
boolean keepScreenOn = Config.NSCLIENT && SP.getBoolean(R.string.key_keep_screen_on, false);
if (keepScreenOn)
2018-06-04 21:34:21 +02:00
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
else
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2016-12-03 17:42:08 +01:00
});
2016-06-13 22:53:41 +02:00
}
2018-06-04 21:34:21 +02:00
private void setupViews(boolean switchToLast) {
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);
navigationView.setNavigationItemSelectedListener(menuItem -> {
return true;
});
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);
if (switchToLast)
2016-06-14 23:45:55 +02:00
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-06-05 01:40:35 +02:00
private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
}
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
Integer 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;
SP.putString(R.string.key_pump_unreachable_threshold, unreachable_threshold.toString());
}
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;
OKDialog.show(this, "Target range change", message, new Runnable() {
@Override
public void run() {
SP.remove("openapsma_min_bg");
SP.remove("openapsma_max_bg");
SP.remove("openapsma_target_bg");
}
});
}
}
2016-11-24 18:46:16 +01:00
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, 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:
2018-11-12 19:00:06 +01:00
case AndroidPermission.CASE_PHONESTATE:
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();
MainApp.bus().post(new EventAppExit());
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
}