Java tutorial
/********************************************************************************** * This file is part of Pusher. * * <p/> * * Copyright (C) 2016 Bertrand Martel * * <p/> * * Pusher is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * <p/> * * Pusher is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * <p/> * * You should have received a copy of the GNU General Public License * * along with Pusher. If not, see <http://www.gnu.org/licenses/>. * */ package com.github.akinaru.roboticbuttonpusher.activity; import android.bluetooth.BluetoothAdapter; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.animation.Animation; import android.widget.TextView; import android.widget.Toast; import com.github.akinaru.roboticbuttonpusher.R; import com.github.akinaru.roboticbuttonpusher.inter.IButtonPusher; import com.github.akinaru.roboticbuttonpusher.menu.MenuUtils; import com.github.silvestrpredko.dotprogressbar.DotProgressBar; /** * Abstract activity for all activities in Bluetooth LE Analyzer * * @author Bertrand Martel */ public abstract class BaseActivity extends AppCompatActivity implements IButtonPusher { private final static String TAG = BaseActivity.class.getSimpleName(); /** * application toolbar */ protected Toolbar toolbar = null; /** * navigationdrawer */ protected DrawerLayout mDrawer = null; /** * toggle on the hamburger button */ protected ActionBarDrawerToggle drawerToggle; /** * navigation view */ protected NavigationView nvDrawer; /** * activity layout ressource id */ private int layoutId; /** * Bluetooth adapter */ protected BluetoothAdapter mBluetoothAdapter = null; /** * set activity ressource id * * @param resId */ protected void setLayout(int resId) { layoutId = resId; } protected TextView debugTv; protected FloatingActionButton mImgSelection; protected DotProgressBar dotProgressBar; protected Animation mAnimationScaleUp; protected Animation mAnimationScaleDown; protected Animation mAnimationDefaultScaleUp; protected FloatingActionButton mFailureButton; protected boolean mFailure = false; protected boolean mAssociated = false; protected MenuItem mDisassociateMenuItem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(layoutId); debugTv = (TextView) findViewById(R.id.debug_mode_tv); // Set a Toolbar to replace the ActionBar. toolbar = (Toolbar) findViewById(R.id.toolbar_item); setSupportActionBar(toolbar); getSupportActionBar().setTitle(getResources().getString(R.string.bt_device_title)); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); toolbar.inflateMenu(R.menu.toolbar_menu); // Find our drawer view mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawerToggle = setupDrawerToggle(); mDrawer.setDrawerListener(drawerToggle); nvDrawer = (NavigationView) findViewById(R.id.nvView); // Setup drawer view setupDrawerContent(nvDrawer); mDisassociateMenuItem = nvDrawer.getMenu().findItem(R.id.exit_item); //setup bluetooth if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, getResources().getString(R.string.ble_not_supported), Toast.LENGTH_SHORT).show(); finish(); } } /** * setup navigation view * * @param navigationView */ private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { MenuUtils.selectDrawerItem(menuItem, mDrawer, BaseActivity.this, BaseActivity.this); return true; } }); } /** * setup action drawer * * @return */ protected ActionBarDrawerToggle setupDrawerToggle() { return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: mDrawer.openDrawer(GravityCompat.START); return true; } return super.onOptionsItemSelected(item); } // Make sure this is the method with just `Bundle` as the signature @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); drawerToggle.syncState(); } @Override public void onBackPressed() { if (this.mDrawer.isDrawerOpen(GravityCompat.START)) { this.mDrawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } protected void showFailure() { runOnUiThread(new Runnable() { @Override public void run() { mImgSelection.setVisibility(View.GONE); dotProgressBar.setVisibility(View.GONE); mFailureButton.setVisibility(View.VISIBLE); mFailureButton.clearAnimation(); mFailure = true; mFailureButton.startAnimation(mAnimationScaleUp); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { this.getMenuInflater().inflate(R.menu.toolbar_menu, menu); //clear button MenuItem item = menu.findItem(R.id.clear_btn); if (item != null) { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { runOnUiThread(new Runnable() { @Override public void run() { debugTv.setText(""); } }); return true; } }); } return super.onCreateOptionsMenu(menu); } protected void appendDebugTv(final String text) { runOnUiThread(new Runnable() { @Override public void run() { debugTv.append(text + System.getProperty("line.separator")); } }); } }