2016-06-04 17:28:05 +02:00
|
|
|
package info.nightscout.androidaps;
|
|
|
|
|
2016-11-24 18:46:16 +01:00
|
|
|
import android.Manifest;
|
2016-11-08 22:35:57 +01:00
|
|
|
import android.content.DialogInterface;
|
2016-06-19 20:06:00 +02:00
|
|
|
import android.content.Intent;
|
2016-07-06 22:50:25 +02:00
|
|
|
import android.content.SharedPreferences;
|
2016-07-03 19:21:41 +02:00
|
|
|
import android.content.pm.PackageManager;
|
2016-11-24 18:46:16 +01:00
|
|
|
import android.os.Build;
|
2016-07-19 11:31:58 +02:00
|
|
|
import android.os.Bundle;
|
2016-07-06 22:50:25 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2016-11-24 18:46:16 +01:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
2016-06-13 22:53:41 +02:00
|
|
|
import android.support.v4.app.Fragment;
|
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;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
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;
|
|
|
|
|
2016-07-07 10:34:20 +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-18 20:19:55 +02:00
|
|
|
import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService;
|
2016-07-07 00:56:31 +02:00
|
|
|
import info.nightscout.androidaps.receivers.KeepAliveReceiver;
|
2016-07-19 11:31:58 +02:00
|
|
|
import info.nightscout.androidaps.tabs.SlidingTabLayout;
|
|
|
|
import info.nightscout.androidaps.tabs.TabPageAdapter;
|
2016-07-03 20:37:42 +02:00
|
|
|
import info.nightscout.utils.ImportExportPrefs;
|
2016-06-27 20:14:31 +02:00
|
|
|
import info.nightscout.utils.LocaleHelper;
|
2016-06-04 17:28:05 +02:00
|
|
|
|
2016-06-09 16:33:44 +02:00
|
|
|
public class MainActivity extends AppCompatActivity {
|
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-07-07 00:56:31 +02:00
|
|
|
private static KeepAliveReceiver keepAliveReceiver;
|
2016-06-13 22:53:41 +02:00
|
|
|
|
2016-11-25 10:13:27 +01:00
|
|
|
static final Integer CASE_STORAGE = 0x1;
|
|
|
|
static final Integer CASE_SMS = 0x2;
|
2016-11-25 01:47:59 +01: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);
|
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 10:13:27 +01:00
|
|
|
askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, CASE_STORAGE);
|
2016-11-25 01:47:59 +01:00
|
|
|
}
|
2016-06-14 23:45:55 +02:00
|
|
|
if (Config.logFunctionCalls)
|
|
|
|
log.debug("onCreate");
|
2016-06-04 17:28:05 +02:00
|
|
|
|
2016-07-03 19:21:41 +02:00
|
|
|
// show version in toolbar
|
|
|
|
try {
|
2016-08-21 22:14:33 +02:00
|
|
|
setTitle(getString(R.string.app_name) + " " + getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
|
2016-07-03 19:21:41 +02:00
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-08-21 22:14:33 +02:00
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
2016-06-27 20:23:07 +02:00
|
|
|
setSupportActionBar(toolbar);
|
2016-07-18 20:19:55 +02:00
|
|
|
registerBus();
|
2016-06-27 20:23:07 +02:00
|
|
|
|
2016-09-10 16:08:29 +02:00
|
|
|
try {
|
|
|
|
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
|
|
|
getSupportActionBar().setIcon(R.mipmap.ic_launcher);
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
// no action
|
|
|
|
}
|
|
|
|
|
2016-07-18 20:19:55 +02:00
|
|
|
if (keepAliveReceiver == null) {
|
2016-07-07 00:56:31 +02:00
|
|
|
keepAliveReceiver = new KeepAliveReceiver();
|
2016-07-18 20:19:55 +02:00
|
|
|
startService(new Intent(this, ExecutionService.class));
|
2016-07-07 00:56:31 +02:00
|
|
|
keepAliveReceiver.setAlarm(this);
|
2016-06-17 14:42:02 +02:00
|
|
|
}
|
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
|
|
|
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
|
|
|
String lang = SP.getString("language", "en");
|
|
|
|
LocaleHelper.setLocale(getApplicationContext(), lang);
|
|
|
|
recreate();
|
|
|
|
try { // activity may be destroyed
|
2016-11-07 01:01:33 +01:00
|
|
|
setUpTabs(ev.isSwitchToLast());
|
2016-07-06 22:50:25 +02:00
|
|
|
} catch (IllegalStateException e) {
|
2016-08-21 22:14:33 +02:00
|
|
|
e.printStackTrace();
|
2016-07-06 22:50:25 +02: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
|
|
|
|
2016-06-04 17:28:05 +02:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
int id = item.getItemId();
|
2016-06-19 13:17:16 +02:00
|
|
|
switch (id) {
|
2016-07-06 22:05:28 +02:00
|
|
|
case R.id.nav_preferences:
|
2016-06-19 20:06:00 +02:00
|
|
|
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
|
|
|
|
startActivity(i);
|
|
|
|
break;
|
2016-06-19 13:17:16 +02:00
|
|
|
case R.id.nav_resetdb:
|
2016-11-08 22:57:56 +01:00
|
|
|
new AlertDialog.Builder(this)
|
2016-11-08 22:35:57 +01:00
|
|
|
.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();
|
2016-06-19 13:17:16 +02:00
|
|
|
break;
|
2016-07-06 22:05:28 +02:00
|
|
|
case R.id.nav_export:
|
2016-07-04 10:05:21 +02:00
|
|
|
ImportExportPrefs.verifyStoragePermissions(this);
|
2016-07-03 20:37:42 +02:00
|
|
|
ImportExportPrefs.exportSharedPreferences(this);
|
|
|
|
break;
|
2016-07-06 22:05:28 +02:00
|
|
|
case R.id.nav_import:
|
2016-07-04 10:05:21 +02:00
|
|
|
ImportExportPrefs.verifyStoragePermissions(this);
|
2016-07-03 20:37:42 +02:00
|
|
|
ImportExportPrefs.importSharedPreferences(this);
|
|
|
|
break;
|
2016-07-19 11:31:58 +02:00
|
|
|
// case R.id.nav_test_alarm:
|
|
|
|
// final int REQUEST_CODE_ASK_PERMISSIONS = 2355;
|
|
|
|
// int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.SYSTEM_ALERT_WINDOW);
|
|
|
|
// if (permission != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
// // We don't have permission so prompt the user
|
|
|
|
// // On Android 6 give permission for alarming in Settings -> Apps -> Draw over other apps
|
|
|
|
// ActivityCompat.requestPermissions(
|
|
|
|
// this,
|
|
|
|
// new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW},
|
|
|
|
// REQUEST_CODE_ASK_PERMISSIONS
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// Intent alertServiceIntent = new Intent(getApplicationContext(), AlertService.class);
|
|
|
|
// alertServiceIntent.putExtra("alertText", getString(R.string.nav_test_alert));
|
|
|
|
// getApplicationContext().startService(alertServiceIntent);
|
|
|
|
// break;
|
2016-06-19 13:17:16 +02:00
|
|
|
case R.id.nav_exit:
|
|
|
|
log.debug("Exiting");
|
2016-07-07 00:56:31 +02:00
|
|
|
keepAliveReceiver.cancelAlarm(this);
|
2016-06-19 13:17:16 +02:00
|
|
|
|
2016-07-07 10:34:20 +02:00
|
|
|
MainApp.bus().post(new EventAppExit());
|
2016-06-19 13:17:16 +02:00
|
|
|
MainApp.closeDbHelper();
|
|
|
|
finish();
|
|
|
|
System.runFinalization();
|
|
|
|
System.exit(0);
|
|
|
|
break;
|
|
|
|
}
|
2016-06-04 17:28:05 +02:00
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
boolean IUnderstand = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("I_understand", false);
|
|
|
|
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) {
|
|
|
|
SharedPreferences smssettings = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
if (smssettings.getBoolean("smscommunicator_remotecommandsallowed", false)) {
|
|
|
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
|
2016-11-25 10:13:27 +01:00
|
|
|
askForPermission(Manifest.permission.RECEIVE_SMS, CASE_SMS);
|
2016-11-25 01:47:59 +01:00
|
|
|
}
|
2016-11-24 18:46:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 01:47:59 +01:00
|
|
|
private void askForPermission(String permission, Integer requestCode) {
|
|
|
|
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
ActivityCompat.requestPermissions(this, new String[]{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(ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED){
|
|
|
|
switch (requestCode) {
|
|
|
|
case 1:
|
2016-11-25 10:13:27 +01:00
|
|
|
//show dialog after permission is granted
|
2016-11-25 01:47:59 +01:00
|
|
|
AlertDialog.Builder alert = new AlertDialog.Builder(this);
|
2016-11-25 10:13:27 +01:00
|
|
|
alert.setMessage(R.string.alert_dialog_storage_permission_text);
|
|
|
|
alert.setPositiveButton(R.string.alert_dialog_positive_button,null);
|
2016-11-25 01:47:59 +01:00
|
|
|
alert.show();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
}
|
2016-11-24 18:46:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|