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;
|
2017-03-31 09:21:40 +02:00
|
|
|
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;
|
2016-06-19 20:06:00 +02:00
|
|
|
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;
|
2017-03-31 09:21:40 +02:00
|
|
|
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;
|
2017-03-31 09:21:40 +02:00
|
|
|
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;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2017-02-10 14:24:56 +01:00
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
|
|
|
import android.widget.EditText;
|
2017-05-04 19:13:56 +02:00
|
|
|
import android.widget.ImageButton;
|
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-06-11 17:22:54 +02:00
|
|
|
import info.nightscout.androidaps.Services.AlarmSoundService;
|
2017-05-04 19:13:56 +02:00
|
|
|
import info.nightscout.androidaps.events.EventAppExit;
|
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;
|
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;
|
2017-03-31 09:21:40 +02:00
|
|
|
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;
|
2017-03-31 09:21:40 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
private boolean askForSMS = false;
|
2016-11-25 01:47:59 +01:00
|
|
|
|
2017-05-04 19:13:56 +02:00
|
|
|
ImageButton menuButton;
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-03-31 09:21:40 +02:00
|
|
|
askForBatteryOptimizationPermission();
|
2016-06-14 23:45:55 +02:00
|
|
|
if (Config.logFunctionCalls)
|
|
|
|
log.debug("onCreate");
|
2016-06-04 17:28:05 +02:00
|
|
|
|
2016-07-18 20:19:55 +02:00
|
|
|
registerBus();
|
2016-06-19 13:17:16 +02:00
|
|
|
setUpTabs(false);
|
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() {
|
|
|
|
recreate();
|
|
|
|
try { // activity may be destroyed
|
2017-06-16 08:37:07 +02:00
|
|
|
setUpTabs(true);
|
2016-12-03 17:42:08 +01:00
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-31 09:21:40 +02:00
|
|
|
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");
|
|
|
|
|
2017-06-12 11:15:03 +02:00
|
|
|
OKDialog.show(this, getString(R.string.pleaseallowpermission), String.format(getString(R.string.needwhitelisting), getString(R.string.app_name)), new Runnable() {
|
2017-03-31 09:21:40 +02:00
|
|
|
|
|
|
|
@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 = getString(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2016-11-25 12:49:41 +01:00
|
|
|
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);
|
2016-11-25 12:49:41 +01:00
|
|
|
alert.setPositiveButton(R.string.ok, null);
|
2016-11-25 11:24:56 +01:00
|
|
|
alert.show();
|
|
|
|
break;
|
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);
|
|
|
|
startActivity(i);
|
|
|
|
}
|
|
|
|
}, null);
|
|
|
|
break;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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(getString(R.string.app_name) + " " + BuildConfig.VERSION);
|
2017-06-04 00:46:44 +02:00
|
|
|
if (Config.NSCLIENT)
|
2017-05-04 19:13:56 +02:00
|
|
|
builder.setIcon(R.mipmap.yellowowl);
|
|
|
|
else
|
|
|
|
builder.setIcon(R.mipmap.blueowl);
|
|
|
|
builder.setMessage("Build: " + BuildConfig.BUILDVERSION);
|
|
|
|
builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null);
|
|
|
|
AlertDialog alertDialog = builder.create();
|
|
|
|
alertDialog.show();
|
|
|
|
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
|
|
|
}
|