diff --git a/app/src/main/java/org/secuso/privacyfriendly2048/activities/helper/BaseActivity.kt b/app/src/main/java/org/secuso/privacyfriendly2048/activities/helper/BaseActivity.kt
index f2e3f31..2a39a46 100644
--- a/app/src/main/java/org/secuso/privacyfriendly2048/activities/helper/BaseActivity.kt
+++ b/app/src/main/java/org/secuso/privacyfriendly2048/activities/helper/BaseActivity.kt
@@ -15,143 +15,121 @@
You should have received a copy of the GNU General Public License
along with Privacy Friendly App Example. If not, see .
*/
-
-package org.secuso.privacyfriendly2048.activities.helper;
-
-import android.content.Intent;
-import android.content.SharedPreferences;
-import android.os.Build;
-import android.os.Bundle;
-import android.os.Handler;
-import android.preference.PreferenceActivity;
-import android.preference.PreferenceManager;
-import android.view.MenuItem;
-import android.view.View;
-
-import androidx.appcompat.app.ActionBarDrawerToggle;
-import androidx.appcompat.app.AppCompatActivity;
-import androidx.appcompat.widget.Toolbar;
-import androidx.core.app.TaskStackBuilder;
-import androidx.core.view.GravityCompat;
-import androidx.drawerlayout.widget.DrawerLayout;
-
-import com.google.android.material.navigation.NavigationView;
-import com.google.android.material.navigation.NavigationView.OnNavigationItemSelectedListener;
-
-import org.secuso.privacyfriendly2048.R;
-import org.secuso.privacyfriendly2048.activities.MainActivity;
-import org.secuso.privacyfriendly2048.activities.StatsActivity;
+package org.secuso.privacyfriendly2048.activities.helper
+
+import android.content.Intent
+import android.content.SharedPreferences
+import android.os.Build
+import android.os.Bundle
+import android.os.Handler
+import android.preference.PreferenceActivity
+import android.preference.PreferenceManager
+import android.view.MenuItem
+import android.view.View
+import androidx.appcompat.app.ActionBarDrawerToggle
+import androidx.appcompat.app.AppCompatActivity
+import androidx.appcompat.widget.Toolbar
+import androidx.core.app.TaskStackBuilder
+import androidx.core.view.GravityCompat
+import androidx.drawerlayout.widget.DrawerLayout
+import com.google.android.material.navigation.NavigationView
+import org.secuso.privacyfriendly2048.R
+import org.secuso.privacyfriendly2048.activities.MainActivity
+import org.secuso.privacyfriendly2048.activities.StatsActivity
/**
* @author Christopher Beckmann, Karola Marky
* @version 20171017
* This class is a parent class of all activities that can be accessed from the
* Navigation Drawer (example see MainActivity.java)
- *
+ *
+ *
* The default NavigationDrawer functionality is implemented in this class. If you wish to inherit
* the default behaviour, make sure the content view has a NavigationDrawer with the id 'nav_view',
* the header should point to 'nav_header_main' and the menu should be loaded from 'main_drawer'.
- *
+ *
+ *
* Also the main layout that holds the content of the activity should have the id 'main_content'.
* This way it will automatically fade in and out every time a transition is happening.
*/
-public abstract class BaseActivity extends AppCompatActivity implements OnNavigationItemSelectedListener {
-
- // delay to launch nav drawer item, to allow close animation to play
- public static final int NAVDRAWER_LAUNCH_DELAY = 250;
- // fade in and fade out durations for the main content when switching between
- // different Activities of the app through the Nav Drawer
- public static final int MAIN_CONTENT_FADEOUT_DURATION = 150;
- public static final int MAIN_CONTENT_FADEIN_DURATION = 250;
-
+abstract class BaseActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
// Navigation drawer:
- public DrawerLayout mDrawerLayout;
- private NavigationView mNavigationView;
+ var mDrawerLayout: DrawerLayout? = null
+ private var mNavigationView: NavigationView? = null
// Helper
- private Handler mHandler;
- protected SharedPreferences mSharedPreferences;
+ private var mHandler: Handler? = null
+ protected var mSharedPreferences: SharedPreferences? = null
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
- mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
- mHandler = new Handler();
+ mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
+ mHandler = Handler()
- overridePendingTransition(0, 0);
+ overridePendingTransition(0, 0)
}
- @Override
- public void onBackPressed() {
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
+ override fun onBackPressed() {
+ val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
if (drawer.isDrawerOpen(GravityCompat.START)) {
- drawer.closeDrawer(GravityCompat.START);
+ drawer.closeDrawer(GravityCompat.START)
} else {
- super.onBackPressed();
+ super.onBackPressed()
}
}
- protected abstract int getNavigationDrawerID();
+ protected abstract val navigationDrawerID: Int
- @Override
- public boolean onNavigationItemSelected(MenuItem item) {
- final int itemId = item.getItemId();
+ override fun onNavigationItemSelected(item: MenuItem): Boolean {
+ val itemId = item.itemId
- return goToNavigationItem(itemId);
+ return goToNavigationItem(itemId)
}
- protected boolean goToNavigationItem(final int itemId) {
-
- if (itemId == getNavigationDrawerID()) {
+ protected fun goToNavigationItem(itemId: Int): Boolean {
+ if (itemId == navigationDrawerID) {
// just close drawer because we are already in this activity
- mDrawerLayout.closeDrawer(GravityCompat.START);
- return true;
+ mDrawerLayout!!.closeDrawer(GravityCompat.START)
+ return true
}
// delay transition so the drawer can close
- mHandler.postDelayed(new Runnable() {
- @Override
- public void run() {
- callDrawerItem(itemId);
- }
- }, NAVDRAWER_LAUNCH_DELAY);
+ mHandler!!.postDelayed({ callDrawerItem(itemId) }, NAVDRAWER_LAUNCH_DELAY.toLong())
- mDrawerLayout.closeDrawer(GravityCompat.START);
+ mDrawerLayout!!.closeDrawer(GravityCompat.START)
- selectNavigationItem(itemId);
+ selectNavigationItem(itemId)
// fade out the active activity
- View mainContent = findViewById(R.id.main_content);
- if (mainContent != null) {
- mainContent.animate().alpha(0).setDuration(MAIN_CONTENT_FADEOUT_DURATION);
- }
- return true;
+ val mainContent = findViewById(R.id.main_content)
+ mainContent?.animate()?.alpha(0f)?.setDuration(MAIN_CONTENT_FADEOUT_DURATION.toLong())
+ return true
}
// set active navigation item
- private void selectNavigationItem(int itemId) {
- for (int i = 0; i < mNavigationView.getMenu().size(); i++) {
- boolean b = itemId == mNavigationView.getMenu().getItem(i).getItemId();
- mNavigationView.getMenu().getItem(i).setChecked(b);
+ private fun selectNavigationItem(itemId: Int) {
+ for (i in 0 until mNavigationView!!.menu.size()) {
+ val b = itemId == mNavigationView!!.menu.getItem(i).itemId
+ mNavigationView!!.menu.getItem(i).setChecked(b)
}
}
/**
* Enables back navigation for activities that are launched from the NavBar. See
- * {@code AndroidManifest.xml} to find out the parent activity names for each activity.
+ * `AndroidManifest.xml` to find out the parent activity names for each activity.
*
* @param intent
*/
- private void createBackStack(Intent intent) {
+ private fun createBackStack(intent: Intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
- TaskStackBuilder builder = TaskStackBuilder.create(this);
- builder.addNextIntentWithParentStack(intent);
- builder.startActivities();
+ val builder = TaskStackBuilder.create(this)
+ builder.addNextIntentWithParentStack(intent)
+ builder.startActivities()
} else {
- startActivity(intent);
- finish();
+ startActivity(intent)
+ finish()
}
}
@@ -161,69 +139,83 @@ public abstract class BaseActivity extends AppCompatActivity implements OnNaviga
*
* @param itemId Item that has been clicked by the user
*/
- private void callDrawerItem(final int itemId) {
- Intent intent;
-
- switch (itemId) {
- case R.id.nav_example:
-
- intent = new Intent(this, MainActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- break;
- case R.id.nav_tutorial:
- intent = new Intent(this, TutorialActivity.class);
- createBackStack(intent);
- break;
- case R.id.nav_about:
- intent = new Intent(this, AboutActivity.class);
- createBackStack(intent);
- break;
- case R.id.nav_help:
- intent = new Intent(this, HelpActivity.class);
- createBackStack(intent);
- break;
- case R.id.nav_statistics:
- intent = new Intent(this, StatsActivity.class);
- createBackStack(intent);
- break;
- case R.id.nav_settings:
- intent = new Intent(this, SettingsActivity.class);
- intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, SettingsActivity.GeneralPreferenceFragment.class.getName());
- intent.putExtra(PreferenceActivity.EXTRA_NO_HEADERS, true);
- createBackStack(intent);
- break;
- default:
+ private fun callDrawerItem(itemId: Int) {
+ val intent: Intent
+
+ when (itemId) {
+ R.id.nav_example -> {
+ intent = Intent(this, MainActivity::class.java)
+ intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
+ startActivity(intent)
+ }
+
+ R.id.nav_tutorial -> {
+ intent = Intent(this, TutorialActivity::class.java)
+ createBackStack(intent)
+ }
+
+ R.id.nav_about -> {
+ intent = Intent(this, AboutActivity::class.java)
+ createBackStack(intent)
+ }
+
+ R.id.nav_help -> {
+ intent = Intent(this, HelpActivity::class.java)
+ createBackStack(intent)
+ }
+
+ R.id.nav_statistics -> {
+ intent = Intent(this, StatsActivity::class.java)
+ createBackStack(intent)
+ }
+
+ R.id.nav_settings -> {
+ intent = Intent(this, SettingsActivity::class.java)
+ intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, SettingsActivity.GeneralPreferenceFragment::class.java.getName())
+ intent.putExtra(PreferenceActivity.EXTRA_NO_HEADERS, true)
+ createBackStack(intent)
+ }
+
+ else -> {}
}
}
- @Override
- protected void onPostCreate(Bundle savedInstanceState) {
- super.onPostCreate(savedInstanceState);
+ override fun onPostCreate(savedInstanceState: Bundle?) {
+ super.onPostCreate(savedInstanceState)
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- if (getSupportActionBar() == null) {
- setSupportActionBar(toolbar);
+ val toolbar = findViewById(R.id.toolbar) as Toolbar
+ if (supportActionBar == null) {
+ setSupportActionBar(toolbar)
}
- mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
- mDrawerLayout.addDrawerListener(toggle);
+ mDrawerLayout = findViewById(R.id.drawer_layout) as DrawerLayout
+ val toggle = ActionBarDrawerToggle(
+ this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
+ )
+ mDrawerLayout!!.addDrawerListener(toggle)
- toggle.syncState();
+ toggle.syncState()
- mNavigationView = (NavigationView) findViewById(R.id.nav_view);
- mNavigationView.setNavigationItemSelectedListener(this);
+ mNavigationView = findViewById(R.id.nav_view) as NavigationView
+ mNavigationView!!.setNavigationItemSelectedListener(this)
- selectNavigationItem(getNavigationDrawerID());
+ selectNavigationItem(navigationDrawerID)
- View mainContent = findViewById(R.id.main_content);
+ val mainContent = findViewById(R.id.main_content)
if (mainContent != null) {
- mainContent.setAlpha(0);
- mainContent.animate().alpha(1).setDuration(MAIN_CONTENT_FADEIN_DURATION);
+ mainContent.alpha = 0f
+ mainContent.animate().alpha(1f).setDuration(MAIN_CONTENT_FADEIN_DURATION.toLong())
}
}
+ companion object {
+ // delay to launch nav drawer item, to allow close animation to play
+ const val NAVDRAWER_LAUNCH_DELAY: Int = 250
+
+ // fade in and fade out durations for the main content when switching between
+ // different Activities of the app through the Nav Drawer
+ const val MAIN_CONTENT_FADEOUT_DURATION: Int = 150
+ const val MAIN_CONTENT_FADEIN_DURATION: Int = 250
+ }
}