2016-06-04 17:28:05 +02:00
|
|
|
package info.nightscout.androidaps;
|
|
|
|
|
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;
|
2016-06-27 23:34:33 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2016-06-13 22:53:41 +02:00
|
|
|
import android.support.v4.app.Fragment;
|
2016-06-04 17:28:05 +02:00
|
|
|
import android.support.v4.view.ViewPager;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
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-06-13 22:53:41 +02:00
|
|
|
import java.util.ArrayList;
|
2016-06-14 23:45:55 +02:00
|
|
|
import java.util.Iterator;
|
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-02 23:58:57 +02:00
|
|
|
import info.nightscout.androidaps.plugins.Careportal.CareportalFragment;
|
2016-06-13 22:53:41 +02:00
|
|
|
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
|
2016-06-19 13:17:16 +02:00
|
|
|
import info.nightscout.androidaps.plugins.Loop.LoopFragment;
|
2016-06-10 18:50:46 +02:00
|
|
|
import info.nightscout.androidaps.plugins.LowSuspend.LowSuspendFragment;
|
2016-06-14 13:01:37 +02:00
|
|
|
import info.nightscout.androidaps.plugins.NSProfileViewer.NSProfileViewerFragment;
|
2016-06-10 18:50:46 +02:00
|
|
|
import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAFragment;
|
2016-06-07 23:36:22 +02:00
|
|
|
import info.nightscout.androidaps.plugins.Overview.OverviewFragment;
|
2016-06-19 13:17:16 +02:00
|
|
|
import info.nightscout.androidaps.plugins.SafetyFragment.SafetyFragment;
|
2016-06-14 23:45:55 +02:00
|
|
|
import info.nightscout.androidaps.plugins.SimpleProfile.SimpleProfileFragment;
|
2016-06-21 00:34:36 +02:00
|
|
|
import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment;
|
|
|
|
import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment;
|
2016-06-06 10:42:46 +02:00
|
|
|
import info.nightscout.androidaps.plugins.TempBasals.TempBasalsFragment;
|
2016-06-05 14:53:03 +02:00
|
|
|
import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
|
2016-06-11 20:45:40 +02:00
|
|
|
import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment;
|
2016-06-04 17:28:05 +02:00
|
|
|
import info.nightscout.androidaps.tabs.*;
|
|
|
|
import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment;
|
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
|
|
|
|
|
|
|
private Toolbar toolbar;
|
|
|
|
private SlidingTabLayout mTabs;
|
|
|
|
private ViewPager mPager;
|
2016-06-13 22:53:41 +02:00
|
|
|
private static TabPageAdapter pageAdapter;
|
|
|
|
|
2016-06-17 14:42:02 +02:00
|
|
|
private static ArrayList<PluginBase> pluginsList = null;
|
2016-06-04 17:28:05 +02:00
|
|
|
|
2016-06-14 23:45:55 +02:00
|
|
|
private static ConfigBuilderFragment configBuilderFragment;
|
2016-06-07 21:48:17 +02:00
|
|
|
|
2016-06-04 17:28:05 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_main);
|
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 {
|
|
|
|
setTitle(getString(R.string.app_name) + " v" + getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-06-27 20:23:07 +02:00
|
|
|
toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
2016-06-17 14:42:02 +02:00
|
|
|
if (pluginsList == null) {
|
|
|
|
pluginsList = new ArrayList<PluginBase>();
|
|
|
|
// Register all tabs in app here
|
|
|
|
pluginsList.add(OverviewFragment.newInstance());
|
|
|
|
pluginsList.add(VirtualPumpFragment.newInstance());
|
2016-07-02 23:58:57 +02:00
|
|
|
if (Config.CAREPORTALENABLED) pluginsList.add(CareportalFragment.newInstance());
|
2016-06-26 14:56:43 +02:00
|
|
|
if (Config.LOOPENABLED) pluginsList.add(LoopFragment.newInstance());
|
|
|
|
if (Config.LOWSUSPEDENABLED) pluginsList.add(LowSuspendFragment.newInstance());
|
|
|
|
if (Config.OPENAPSMAENABLED) pluginsList.add(OpenAPSMAFragment.newInstance());
|
2016-06-17 14:42:02 +02:00
|
|
|
pluginsList.add(NSProfileViewerFragment.newInstance());
|
|
|
|
pluginsList.add(SimpleProfileFragment.newInstance());
|
|
|
|
pluginsList.add(TreatmentsFragment.newInstance());
|
|
|
|
pluginsList.add(TempBasalsFragment.newInstance());
|
2016-06-19 13:17:16 +02:00
|
|
|
pluginsList.add(SafetyFragment.newInstance());
|
2016-07-02 23:58:57 +02:00
|
|
|
if (Config.OBJECTIVESENABLED) pluginsList.add(ObjectivesFragment.newInstance());
|
2016-06-21 00:34:36 +02:00
|
|
|
pluginsList.add(SourceXdripFragment.newInstance());
|
|
|
|
pluginsList.add(SourceNSClientFragment.newInstance());
|
2016-06-17 14:42:02 +02:00
|
|
|
pluginsList.add(configBuilderFragment = ConfigBuilderFragment.newInstance());
|
|
|
|
|
|
|
|
registerBus();
|
|
|
|
|
|
|
|
configBuilderFragment.initialize();
|
2016-06-21 23:24:54 +02:00
|
|
|
MainApp.setConfigBuilder(configBuilderFragment);
|
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) {
|
|
|
|
setUpTabs(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setUpTabs(boolean switchToLast) {
|
|
|
|
pageAdapter = new TabPageAdapter(getSupportFragmentManager());
|
2016-06-14 23:45:55 +02:00
|
|
|
for (PluginBase f : pluginsList) {
|
|
|
|
pageAdapter.registerNewFragment((Fragment) f);
|
2016-06-13 22:53:41 +02:00
|
|
|
}
|
|
|
|
mPager = (ViewPager) findViewById(R.id.pager);
|
|
|
|
mPager.setAdapter(pageAdapter);
|
|
|
|
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
|
|
|
|
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-06-19 20:06:00 +02:00
|
|
|
case R.id.nav_preferences: {
|
|
|
|
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
|
|
|
|
startActivity(i);
|
|
|
|
break;
|
|
|
|
}
|
2016-06-19 13:17:16 +02:00
|
|
|
case R.id.nav_resetdb:
|
|
|
|
MainApp.getDbHelper().resetDatabases();
|
|
|
|
break;
|
2016-06-27 20:14:31 +02:00
|
|
|
case R.id.en_lang: {
|
|
|
|
LocaleHelper.setLocale(this, "en");
|
2016-06-27 20:23:07 +02:00
|
|
|
recreate();
|
2016-06-27 20:14:31 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R.id.cs_lang: {
|
|
|
|
LocaleHelper.setLocale(this, "cs");
|
2016-06-27 20:23:07 +02:00
|
|
|
recreate();
|
2016-06-27 20:14:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-07-03 20:54:00 +02:00
|
|
|
case R.id.de_lang: {
|
|
|
|
LocaleHelper.setLocale(this, "de");
|
|
|
|
recreate();
|
|
|
|
break;
|
|
|
|
}
|
2016-07-03 20:37:42 +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;
|
|
|
|
}
|
|
|
|
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-06-19 13:17:16 +02:00
|
|
|
case R.id.nav_exit:
|
|
|
|
log.debug("Exiting");
|
|
|
|
//chancelAlarmManager();
|
|
|
|
|
|
|
|
//MainApp.bus().post(new StopEvent());
|
|
|
|
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-06-14 23:45:55 +02:00
|
|
|
public static ArrayList<PluginBase> getPluginsList() {
|
|
|
|
return pluginsList;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ArrayList<PluginBase> getSpecificPluginsList(int type) {
|
|
|
|
ArrayList<PluginBase> newList = new ArrayList<PluginBase>();
|
|
|
|
|
2016-07-02 23:58:57 +02:00
|
|
|
if (pluginsList != null) {
|
|
|
|
Iterator<PluginBase> it = pluginsList.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
PluginBase p = it.next();
|
|
|
|
if (p.getType() == type)
|
|
|
|
newList.add(p);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.error("pluginsList=null");
|
2016-06-14 23:45:55 +02:00
|
|
|
}
|
|
|
|
return newList;
|
|
|
|
}
|
2016-06-26 14:56:43 +02:00
|
|
|
|
2016-06-27 18:48:48 +02:00
|
|
|
public static ArrayList<PluginBase> getSpecificPluginsListByInterface(Class interfaceClass) {
|
|
|
|
ArrayList<PluginBase> newList = new ArrayList<PluginBase>();
|
|
|
|
|
2016-07-02 23:58:57 +02:00
|
|
|
if (pluginsList != null) {
|
|
|
|
Iterator<PluginBase> it = pluginsList.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
PluginBase p = it.next();
|
|
|
|
if (p.getClass() != ConfigBuilderFragment.class && interfaceClass.isAssignableFrom(p.getClass()))
|
|
|
|
newList.add(p);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.error("pluginsList=null");
|
2016-06-27 18:48:48 +02:00
|
|
|
}
|
|
|
|
return newList;
|
|
|
|
}
|
|
|
|
|
2016-06-27 23:34:33 +02:00
|
|
|
@Nullable
|
2016-06-26 14:56:43 +02:00
|
|
|
public static PluginBase getSpecificPlugin(Class pluginClass) {
|
2016-06-27 23:34:33 +02:00
|
|
|
if (pluginsList != null) {
|
|
|
|
Iterator<PluginBase> it = pluginsList.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
PluginBase p = it.next();
|
|
|
|
if (p.getClass() == pluginClass)
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
} else {
|
2016-07-02 23:58:57 +02:00
|
|
|
log.error("pluginsList=null");
|
2016-06-26 14:56:43 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-06-04 17:28:05 +02:00
|
|
|
}
|