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

347 lines
15 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;
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
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.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;
2019-12-09 19:03:26 +01:00
import info.nightscout.androidaps.activities.StatsActivity;
2019-10-08 21:20:04 +02:00
import info.nightscout.androidaps.activities.SurveyActivity;
2017-05-04 19:13:56 +02:00
import info.nightscout.androidaps.events.EventAppExit;
2018-06-23 18:24:04 +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-10-22 15:24:37 +02:00
import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt;
2019-02-28 23:16:50 +01: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.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
2018-06-23 18:24:04 +02:00
// initialize screen wake lock
2019-10-15 20:44:03 +02:00
processPreferenceChange(new EventPreferenceChange(R.string.key_keep_screen_on));
2018-05-22 23:06:17 +02:00
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-05-22 23:06:17 +02:00
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)
);
disposable.add(RxBus.INSTANCE
.toObservable(EventPreferenceChange.class)
.observeOn(AndroidSchedulers.mainThread())
2019-10-15 20:44:03 +02:00
.subscribe(this::processPreferenceChange, FabricPrivacy::logException)
2019-10-14 23:16:51 +02:00
);
2018-07-29 15:37:28 +02:00
2019-12-13 13:12:36 +01:00
if (!SP.getBoolean(R.string.key_setupwizard_processed, false)) {
2018-05-11 13:14:59 +02:00
Intent intent = new Intent(this, SetupWizardActivity.class);
startActivity(intent);
}
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);
}
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);
}
2018-05-22 23:06:17 +02:00
@Override
public void onPostCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
actionBarDrawerToggle.syncState();
2016-06-13 22:53:41 +02:00
}
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);
}
2019-10-15 20:44:03 +02:00
public void processPreferenceChange(final EventPreferenceChange ev) {
2019-10-22 23:01:38 +02:00
if (ev.isChanged(R.string.key_keep_screen_on))
setWakeLock();
2017-07-03 00:02:54 +02:00
}
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-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;
2019-12-09 19:22:06 +01:00
/*
2019-10-08 21:20:04 +02:00
case R.id.nav_survey:
startActivity(new Intent(this, SurveyActivity.class));
return true;
2019-12-09 19:22:06 +01:00
*/
2019-12-09 19:03:26 +01:00
case R.id.nav_stats:
startActivity(new Intent(this, StatsActivity.class));
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
}