예제 #1
0
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
   return Utils.createOptionsMenu(this, menu);
 }
예제 #2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate(" + savedInstanceState + "," + getIntent().getExtras() + ")");
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
      currentFragment = savedInstanceState.getInt(CURRENT_FRAGMENT, R.id.nav_info);
    } else if (getIntent().getExtras() != null) {
      currentFragment = getIntent().getExtras().getInt(CURRENT_FRAGMENT, R.id.nav_info);
    }

    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            if (!ecm.isConnected()) {
              connect();
            } else {
              disconnect();
            }
          }
        });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle =
        new ActionBarDrawerToggle(
            this,
            drawer,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    ((TextView) navigationView.getHeaderView(0).findViewById(R.id.headerTitle))
        .setText(Utils.getAppVersion(this));

    // Bind to our service and setup the connect button
    bindService(
        new Intent(this, EcmDroidService.class), serviceConnection, Context.BIND_AUTO_CREATE);
    startService(new Intent(this, EcmDroidService.class));

    switchToFragment(currentFragment);

    // Install the database
    dbHelper = new DBHelper(this);
    if (!dbHelper.isDbInstalled()) {
      ProgressDialogTask installTask =
          new ProgressDialogTask(this, "Installing Database") {
            @Override
            protected Exception doInBackground(Void... args) {
              try {
                dbHelper.installDB(MainActivity.this);
              } catch (IOException e) {
                Log.e(TAG, "DB Install failed.", e);
                return e;
              }
              return null;
            }

            @Override
            protected void onPostExecute(Exception result) {
              super.onPostExecute(result);
              if (result != null) {
                Toast.makeText(
                        MainActivity.this, "FATAL: DB Installation failed.", Toast.LENGTH_LONG)
                    .show();
                System.exit(0);
              }
            }
          };
      installTask.execute();
    }
  }