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

449 lines
20 KiB
Java
Raw Normal View History

2016-06-04 17:28:05 +02:00
package info.nightscout.androidaps;
2016-11-24 18:46:16 +01:00
import android.Manifest;
2017-05-04 19:13:56 +02:00
import android.app.Activity;
import android.content.ActivityNotFoundException;
2017-02-10 14:24:56 +01:00
import android.content.Context;
2017-05-04 19:13:56 +02:00
import android.content.DialogInterface;
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;
import android.net.Uri;
2016-11-24 18:46:16 +01:00
import android.os.Build;
2016-07-19 11:31:58 +02:00
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
2016-11-24 18:46:16 +01:00
import android.support.v4.app.ActivityCompat;
2016-11-25 01:47:59 +01:00
import android.support.v4.content.ContextCompat;
2016-06-04 17:28:05 +02:00
import android.support.v4.view.ViewPager;
2016-11-08 22:35:57 +01:00
import android.support.v7.app.AlertDialog;
2016-06-04 17:28:05 +02:00
import android.support.v7.app.AppCompatActivity;
2017-05-04 19:13:56 +02:00
import android.support.v7.widget.PopupMenu;
2018-02-10 14:53:43 +01:00
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
2017-05-04 19:13:56 +02:00
import android.view.MenuInflater;
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;
2017-05-04 19:13:56 +02:00
import android.widget.ImageButton;
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;
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;
2016-11-25 01:47:59 +01: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;
2017-06-30 09:55:47 +02:00
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
2018-01-30 19:33:10 +01:00
import info.nightscout.androidaps.plugins.Food.FoodPlugin;
2017-07-03 00:02:54 +02:00
import info.nightscout.androidaps.plugins.Overview.events.EventSetWakeLock;
2018-04-05 09:39:18 +02:00
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
2018-04-20 17:27:31 +02:00
import info.nightscout.androidaps.startupwizard.SetupWizardActivity;
2016-07-19 11:31:58 +02:00
import info.nightscout.androidaps.tabs.SlidingTabLayout;
import info.nightscout.androidaps.tabs.TabPageAdapter;
2017-05-04 19:13:56 +02:00
import info.nightscout.utils.ImportExportPrefs;
2016-06-27 20:14:31 +02:00
import info.nightscout.utils.LocaleHelper;
2017-05-04 19:13:56 +02:00
import info.nightscout.utils.LogDialog;
import info.nightscout.utils.OKDialog;
2017-05-04 19:13:56 +02:00
import info.nightscout.utils.PasswordProtection;
2017-02-17 16:16:20 +01:00
import info.nightscout.utils.SP;
import info.nightscout.utils.ToastUtils;
2016-06-04 17:28:05 +02:00
2017-05-04 19:13:56 +02:00
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
2016-06-05 01:40:35 +02:00
private static Logger log = LoggerFactory.getLogger(MainActivity.class);
2016-06-04 17:28:05 +02:00
2016-11-25 12:00:15 +01:00
static final int CASE_STORAGE = 0x1;
static final int CASE_SMS = 0x2;
2017-10-14 13:36:38 +02:00
static final int CASE_LOCATION = 0x3;
2016-11-25 12:00:15 +01:00
private boolean askForSMS = false;
2017-10-14 13:36:38 +02:00
private boolean askForLocation = true;
2016-11-25 01:47:59 +01:00
2017-05-04 19:13:56 +02:00
ImageButton menuButton;
2017-07-03 00:02:54 +02:00
protected PowerManager.WakeLock mWakeLock;
2017-05-04 19:13:56 +02:00
2016-06-04 17:28:05 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2016-07-08 00:17:02 +02:00
Iconify.with(new FontAwesomeModule());
2016-07-06 22:08:00 +02:00
LocaleHelper.onCreate(this, "en");
2016-06-04 17:28:05 +02:00
setContentView(R.layout.activity_main);
2017-05-04 19:13:56 +02:00
menuButton = (ImageButton) findViewById(R.id.overview_menuButton);
menuButton.setOnClickListener(this);
2016-09-13 22:34:40 +02:00
checkEula();
2016-11-25 01:47:59 +01:00
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
2016-11-25 11:24:56 +01:00
askForPermission(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, CASE_STORAGE);
2016-11-25 01:47:59 +01:00
}
askForBatteryOptimizationPermission();
doMigrations();
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);
}
2016-06-14 23:45:55 +02:00
if (Config.logFunctionCalls)
log.debug("onCreate");
2016-06-04 17:28:05 +02:00
2017-07-03 00:02:54 +02:00
onStatusEvent(new EventSetWakeLock(SP.getBoolean("lockscreen", false)));
registerBus();
2016-06-19 13:17:16 +02:00
setUpTabs(false);
2016-06-13 22:53:41 +02:00
}
2017-07-03 00:02:54 +02:00
@Subscribe
public void onStatusEvent(final EventSetWakeLock ev) {
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (ev.lock) {
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "AAPS");
2017-08-27 11:57:06 +02:00
if (!mWakeLock.isHeld())
mWakeLock.acquire();
2017-07-03 00:02:54 +02:00
} else {
2017-08-27 11:57:06 +02:00
if (mWakeLock != null && mWakeLock.isHeld())
2017-07-03 00:02:54 +02:00
mWakeLock.release();
}
}
2016-06-13 22:53:41 +02:00
@Subscribe
public void onStatusEvent(final EventRefreshGui ev) {
2016-07-06 22:50:25 +02:00
String lang = SP.getString("language", "en");
LocaleHelper.setLocale(getApplicationContext(), lang);
2016-12-03 17:42:08 +01:00
runOnUiThread(new Runnable() {
@Override
public void run() {
2018-01-29 22:48:47 +01:00
if (ev.recreate) {
2017-12-31 16:44:13 +01:00
recreate();
2018-01-29 22:48:47 +01:00
} else {
2017-12-31 16:44:13 +01:00
try { // activity may be destroyed
setUpTabs(true);
} catch (IllegalStateException e) {
log.error("Unhandled exception", e);
}
2016-12-03 17:42:08 +01:00
}
2017-12-31 16:44:13 +01:00
2017-07-03 00:02:54 +02:00
boolean lockScreen = BuildConfig.NSCLIENTOLNY && SP.getBoolean("lockscreen", false);
if (lockScreen)
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
}
private void setUpTabs(boolean switchToLast) {
2016-08-21 22:14:33 +02:00
TabPageAdapter pageAdapter = new TabPageAdapter(getSupportFragmentManager(), this);
2016-08-05 23:54:03 +02:00
for (PluginBase p : MainApp.getPluginsList()) {
pageAdapter.registerNewFragment(p);
2016-06-13 22:53:41 +02:00
}
2016-08-21 22:14:33 +02:00
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
2016-06-13 22:53:41 +02:00
mPager.setAdapter(pageAdapter);
2016-08-21 22:14:33 +02:00
SlidingTabLayout mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
2016-06-13 22:53:41 +02:00
mTabs.setViewPager(mPager);
if (switchToLast)
2016-06-14 23:45:55 +02:00
mPager.setCurrentItem(pageAdapter.getCount() - 1, false);
2016-06-04 17:28:05 +02:00
}
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 = MainApp.getConfigBuilder().getProfile();
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-25 10:13:27 +01:00
//check for sms permission if enable in prefernces
2016-11-25 01:47:59 +01:00
@Subscribe
public void onStatusEvent(final EventPreferenceChange ev) {
2017-02-22 14:11:05 +01:00
if (ev.isChanged(R.string.key_smscommunicator_remotecommandsallowed)) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
synchronized (this) {
if (SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)) {
setAskForSMS();
}
2016-11-25 12:00:15 +01:00
}
}
}
}
private synchronized void setAskForSMS() {
askForSMS = true;
}
@Override
2017-02-22 14:11:05 +01:00
protected void onResume() {
2016-11-25 12:00:15 +01:00
super.onResume();
askForSMSPermissions();
2017-10-14 13:36:38 +02:00
askForLocationPermissions();
MainApp.bus().post(new EventFeatureRunning(EventFeatureRunning.Feature.MAIN));
2016-11-25 12:00:15 +01:00
}
2017-07-03 00:02:54 +02:00
@Override
public void onDestroy() {
if (mWakeLock != null)
2017-08-27 11:53:36 +02:00
if (mWakeLock.isHeld())
mWakeLock.release();
2017-07-03 00:02:54 +02:00
super.onDestroy();
}
private void askForBatteryOptimizationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final String packageName = getPackageName();
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
log.debug("Requesting ignore battery optimization");
OKDialog.show(this, MainApp.gs(R.string.pleaseallowpermission), String.format(MainApp.gs(R.string.needwhitelisting), MainApp.gs(R.string.app_name)), new Runnable() {
@Override
public void run() {
try {
final Intent intent = new Intent();
// ignoring battery optimizations required for constant connection
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
} catch (ActivityNotFoundException e) {
final String msg = MainApp.gs(R.string.batteryoptimalizationerror);
ToastUtils.showToastInUiThread(getApplicationContext(), msg);
log.error(msg);
}
}
});
}
}
}
2017-02-22 14:11:05 +01:00
private synchronized void askForSMSPermissions() {
2016-11-25 12:00:15 +01:00
if (askForSMS) { //only when settings were changed an MainActivity resumes.
askForSMS = false;
2017-02-22 14:11:05 +01:00
if (SP.getBoolean(R.string.smscommunicator_remotecommandsallowed, false)) {
2016-11-25 12:00:15 +01:00
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
askForPermission(new String[]{Manifest.permission.RECEIVE_SMS,
Manifest.permission.SEND_SMS,
Manifest.permission.RECEIVE_MMS}, CASE_SMS);
}
2016-11-25 01:47:59 +01:00
}
2016-11-24 18:46:16 +01:00
}
}
2017-10-14 13:36:38 +02:00
private synchronized void askForLocationPermissions() {
if (askForLocation) { //only when settings were changed an MainActivity resumes.
askForLocation = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
askForPermission(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, CASE_LOCATION);
}
}
}
2016-11-25 11:24:56 +01:00
private void askForPermission(String[] permission, Integer requestCode) {
boolean test = false;
2017-02-22 14:11:05 +01:00
for (int i = 0; i < permission.length; i++) {
2016-11-25 11:24:56 +01:00
test = test || (ContextCompat.checkSelfPermission(this, permission[i]) != PackageManager.PERMISSION_GRANTED);
}
if (test) {
ActivityCompat.requestPermissions(this, permission, requestCode);
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) {
2016-11-25 12:00:15 +01:00
case 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;
2017-10-14 13:36:38 +02:00
case CASE_LOCATION:
2016-11-25 12:00:15 +01:00
case CASE_SMS:
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
public void onClick(final View v) {
final Activity activity = this;
switch (v.getId()) {
case R.id.overview_menuButton:
PopupMenu popup = new PopupMenu(v.getContext(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_main, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.nav_preferences:
PasswordProtection.QueryPassword(v.getContext(), R.string.settings_password, "settings_password", new Runnable() {
@Override
public void run() {
Intent i = new Intent(v.getContext(), PreferencesActivity.class);
2017-11-01 20:43:59 +01:00
i.putExtra("id", -1);
2017-05-04 19:13:56 +02:00
startActivity(i);
}
}, null);
break;
2018-01-29 22:48:47 +01:00
case R.id.nav_historybrowser:
startActivity(new Intent(v.getContext(), HistoryBrowseActivity.class));
break;
2018-04-20 17:27:31 +02:00
case R.id.nav_setupwizard:
startActivity(new Intent(v.getContext(), SetupWizardActivity.class));
break;
2017-05-04 19:13:56 +02:00
case R.id.nav_resetdb:
new AlertDialog.Builder(v.getContext())
.setTitle(R.string.nav_resetdb)
.setMessage(R.string.reset_db_confirm)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainApp.getDbHelper().resetDatabases();
2018-01-30 19:33:10 +01:00
// should be handled by Plugin-Interface and
// additional service interface and plugin registry
2018-04-05 09:39:18 +02:00
FoodPlugin.getPlugin().getService().resetFood();
TreatmentsPlugin.getPlugin().getService().resetTreatments();
2017-05-04 19:13:56 +02:00
}
})
.create()
.show();
break;
case R.id.nav_export:
ImportExportPrefs.verifyStoragePermissions(activity);
ImportExportPrefs.exportSharedPreferences(activity);
break;
case R.id.nav_import:
ImportExportPrefs.verifyStoragePermissions(activity);
ImportExportPrefs.importSharedPreferences(activity);
break;
case R.id.nav_show_logcat:
LogDialog.showLogcat(v.getContext());
break;
case R.id.nav_about:
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle(MainApp.gs(R.string.app_name) + " " + BuildConfig.VERSION);
2018-01-29 22:48:47 +01:00
if (Config.NSCLIENT || Config.G5UPLOADER)
2017-05-04 19:13:56 +02:00
builder.setIcon(R.mipmap.yellowowl);
else
builder.setIcon(R.mipmap.blueowl);
2017-06-30 09:55:47 +02:00
String message = "Build: " + BuildConfig.BUILDVERSION + "\n";
2018-04-05 00:08:59 +02:00
message += "Flavor: " + BuildConfig.FLAVOR + BuildConfig.BUILD_TYPE + "\n";
message += MainApp.gs(R.string.configbuilder_nightscoutversion_label) + " " + ConfigBuilderPlugin.nightscoutVersionName;
if (MainApp.engineeringMode)
message += "\n" + MainApp.gs(R.string.engineering_mode_enabled);
message += MainApp.gs(R.string.about_link_urls);
2018-02-10 14:53:43 +01:00
final SpannableString messageSpanned = new SpannableString(message);
Linkify.addLinks(messageSpanned, Linkify.WEB_URLS);
builder.setMessage(messageSpanned);
2018-05-02 13:49:24 +02:00
builder.setPositiveButton(MainApp.gs(R.string.ok), null);
2017-05-04 19:13:56 +02:00
AlertDialog alertDialog = builder.create();
alertDialog.show();
2018-02-10 14:53:43 +01:00
((TextView)alertDialog.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
2017-05-04 19:13:56 +02:00
break;
case R.id.nav_exit:
log.debug("Exiting");
MainApp.instance().stopKeepAliveService();
MainApp.bus().post(new EventAppExit());
MainApp.closeDbHelper();
finish();
System.runFinalization();
System.exit(0);
break;
}
return false;
}
});
popup.show();
break;
}
}
2016-11-25 12:00:15 +01:00
}