From d54e1aa47c1f796a657fbec4adde4882847a64f2 Mon Sep 17 00:00:00 2001 From: George Venios Date: Thu, 3 Sep 2015 20:19:29 +0300 Subject: [PATCH] Ditched deprecated actionbar APIs and moved to Toolbar. Also: Corrected styling of the tab layout to match previous implementation. App now follows material convention for side drawer overlapping the app bar. --- .../activities/FolderPickerActivity.java | 2 -- .../activities/LogActivity.java | 1 - .../activities/MainActivity.java | 22 ++++++------- .../activities/RestartActivity.java | 6 ---- .../activities/SettingsActivity.java | 4 +-- .../activities/SyncthingActivity.java | 4 +-- .../activities/ToolbarBindingActivity.java | 28 +++++++++++++++++ .../activities/WebGuiActivity.java | 1 - .../res/layout/activity_folder_picker.xml | 31 ++++++++++++------- src/main/res/layout/activity_log.xml | 12 ++++--- src/main/res/layout/activity_main.xml | 10 ++++-- src/main/res/layout/activity_settings.xml | 13 ++++++++ src/main/res/layout/activity_web_gui.xml | 15 ++++----- src/main/res/layout/widget_toolbar.xml | 10 ++++++ src/main/res/values/dimens.xml | 1 + src/main/res/values/styles.xml | 5 +++ src/main/res/values/themes.xml | 9 +++--- 17 files changed, 116 insertions(+), 58 deletions(-) create mode 100644 src/main/java/com/nutomic/syncthingandroid/activities/ToolbarBindingActivity.java create mode 100644 src/main/res/layout/activity_settings.xml create mode 100644 src/main/res/layout/widget_toolbar.xml diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/FolderPickerActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/FolderPickerActivity.java index 4e773eeb..7abafa92 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/FolderPickerActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/FolderPickerActivity.java @@ -61,8 +61,6 @@ public class FolderPickerActivity extends SyncthingActivity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - setContentView(R.layout.activity_folder_picker); mListView = (ListView) findViewById(android.R.id.list); mListView.setOnItemClickListener(this); diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/LogActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/LogActivity.java index 557c7133..11f848e7 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/LogActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/LogActivity.java @@ -41,7 +41,6 @@ public class LogActivity extends SyncthingActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_log); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); if (savedInstanceState != null) { mSyncthingLog = savedInstanceState.getBoolean("syncthingLog"); diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java index 38d3e6fa..54beff97 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java @@ -20,6 +20,7 @@ import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.GravityCompat; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; +import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarDrawerToggle; import android.util.Log; import android.view.KeyEvent; @@ -110,9 +111,6 @@ public class MainActivity extends SyncthingActivity mDisabledDialog = null; } mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); - mDrawerLayout.setDrawerListener(mDrawerToggle); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - getSupportActionBar().setHomeButtonEnabled(true); } /** @@ -190,8 +188,6 @@ public class MainActivity extends SyncthingActivity private ViewPager mViewPager; - private TabLayout mTabLayout; - private ActionBarDrawerToggle mDrawerToggle; private DrawerLayout mDrawerLayout; @@ -208,8 +204,8 @@ public class MainActivity extends SyncthingActivity mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); - mTabLayout = (TabLayout) findViewById(R.id.tabContainer); - mTabLayout.setupWithViewPager(mViewPager); + TabLayout tabLayout = (TabLayout) findViewById(R.id.tabContainer); + tabLayout.setupWithViewPager(mViewPager); if (savedInstanceState != null) { FragmentManager fm = getSupportFragmentManager(); @@ -231,6 +227,7 @@ public class MainActivity extends SyncthingActivity .replace(R.id.drawer, mDrawerFragment) .commit(); mDrawerToggle = new Toggle(this, mDrawerLayout); + mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); mDrawerLayout.setDrawerListener(mDrawerToggle); } @@ -274,7 +271,13 @@ public class MainActivity extends SyncthingActivity @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); + mDrawerToggle.syncState(); + + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setHomeButtonEnabled(true); + } } @Override @@ -288,7 +291,6 @@ public class MainActivity extends SyncthingActivity return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item); } - /** * Handles drawer opened and closed events, toggling option menu state. */ @@ -301,16 +303,12 @@ public class MainActivity extends SyncthingActivity public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); mDrawerFragment.onDrawerOpened(); - mFolderFragment.setHasOptionsMenu(false); - mDevicesFragment.setHasOptionsMenu(false); } @Override public void onDrawerClosed(View view) { super.onDrawerClosed(view); mDrawerFragment.onDrawerClosed(); - mFolderFragment.setHasOptionsMenu(true); - mDevicesFragment.setHasOptionsMenu(true); } } diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java index 9843f4e7..31a018e0 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java @@ -1,7 +1,6 @@ package com.nutomic.syncthingandroid.activities; import android.annotation.TargetApi; -import android.app.Activity; import android.app.AlertDialog; import android.app.Notification; import android.app.NotificationManager; @@ -9,12 +8,9 @@ import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.os.Build; import android.os.Bundle; import android.support.v4.app.NotificationCompat; -import android.support.v7.app.AppCompatActivity; -import com.nutomic.syncthingandroid.BuildConfig; import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.syncthing.SyncthingService; @@ -32,8 +28,6 @@ public class RestartActivity extends SyncthingActivity { @TargetApi(11) protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // Using `false` in style causes crash. - getSupportActionBar().hide(); final Intent intent = new Intent(this, SyncthingService.class) .setAction(SyncthingService.ACTION_RESTART); diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java index 34e0241d..17892e81 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -37,7 +37,7 @@ public class SettingsActivity extends SyncthingActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setContentView(R.layout.activity_settings); FragmentManager fm = getSupportFragmentManager(); if (savedInstanceState != null) { @@ -71,7 +71,7 @@ public class SettingsActivity extends SyncthingActivity { } fm.beginTransaction() - .replace(android.R.id.content, mFragment) + .replace(R.id.content, mFragment) .commit(); } diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/SyncthingActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/SyncthingActivity.java index fc7dd06f..2aee1937 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/SyncthingActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/SyncthingActivity.java @@ -6,8 +6,6 @@ import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; -import android.support.v7.app.ActionBarActivity; -import android.support.v7.app.AppCompatActivity; import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.SyncthingService; @@ -18,7 +16,7 @@ import java.util.LinkedList; /** * Connects to {@link SyncthingService} and provides access to it. */ -public abstract class SyncthingActivity extends AppCompatActivity implements ServiceConnection { +public abstract class SyncthingActivity extends ToolbarBindingActivity implements ServiceConnection { private SyncthingService mSyncthingService; diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/ToolbarBindingActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/ToolbarBindingActivity.java new file mode 100644 index 00000000..20d9ffa4 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/activities/ToolbarBindingActivity.java @@ -0,0 +1,28 @@ +package com.nutomic.syncthingandroid.activities; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; + +import com.nutomic.syncthingandroid.R; + +/** + * An activity that onPostCreate will look for a Toolbar in the layout + * and bind it as the activity's actionbar with reasonable defaults.
+ * The Toolbar must exist in the content view and have an id of R.id.toolbar.
+ * Trying to call getSupportActionBar before this Activity's onPostCreate will cause a crash. + */ +public class ToolbarBindingActivity extends AppCompatActivity { + + @Override + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + if (toolbar != null) { + setSupportActionBar(toolbar); + //noinspection ConstantConditions + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + } + } +} diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/WebGuiActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/WebGuiActivity.java index 9855b499..9a68f183 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/WebGuiActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/WebGuiActivity.java @@ -104,7 +104,6 @@ public class WebGuiActivity extends SyncthingActivity super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_gui); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); mLoadingView = findViewById(R.id.loading); loadCaCert(); diff --git a/src/main/res/layout/activity_folder_picker.xml b/src/main/res/layout/activity_folder_picker.xml index 43ad82b3..de41f8b7 100644 --- a/src/main/res/layout/activity_folder_picker.xml +++ b/src/main/res/layout/activity_folder_picker.xml @@ -1,17 +1,24 @@ - + android:layout_height="match_parent" + android:orientation="vertical"> - + + + android:layout_height="match_parent"> - + - + + + diff --git a/src/main/res/layout/activity_log.xml b/src/main/res/layout/activity_log.xml index 428e3af6..ee9f9655 100644 --- a/src/main/res/layout/activity_log.xml +++ b/src/main/res/layout/activity_log.xml @@ -1,13 +1,15 @@ - + + @@ -16,9 +18,9 @@ android:gravity="top" android:textSize="12sp" android:textIsSelectable="true" - android:layout_width="fill_parent" + android:layout_width="match_parent" android:layout_height="wrap_content" /> - + diff --git a/src/main/res/layout/activity_main.xml b/src/main/res/layout/activity_main.xml index 701502e1..6c3f2eb1 100644 --- a/src/main/res/layout/activity_main.xml +++ b/src/main/res/layout/activity_main.xml @@ -1,5 +1,6 @@ @@ -9,10 +10,15 @@ android:layout_height="match_parent" android:orientation="vertical"> + + + android:layout_height="wrap_content" + android:background="?colorPrimary" + android:elevation="@dimen/toolbar_elevation" + app:theme="@style/ThemeOverlay.Syncthing.Toolbar" /> - + \ No newline at end of file diff --git a/src/main/res/layout/activity_settings.xml b/src/main/res/layout/activity_settings.xml new file mode 100644 index 00000000..942e19e1 --- /dev/null +++ b/src/main/res/layout/activity_settings.xml @@ -0,0 +1,13 @@ + + + + + + + diff --git a/src/main/res/layout/activity_web_gui.xml b/src/main/res/layout/activity_web_gui.xml index 8e6c8a40..9a459cbc 100644 --- a/src/main/res/layout/activity_web_gui.xml +++ b/src/main/res/layout/activity_web_gui.xml @@ -1,8 +1,10 @@ - - + android:layout_height="match_parent" + android:orientation="vertical"> + + @@ -30,4 +31,4 @@ android:layout_height="wrap_content" android:text="@string/web_gui_loading" /> - + diff --git a/src/main/res/layout/widget_toolbar.xml b/src/main/res/layout/widget_toolbar.xml new file mode 100644 index 00000000..a9399127 --- /dev/null +++ b/src/main/res/layout/widget_toolbar.xml @@ -0,0 +1,10 @@ + + \ No newline at end of file diff --git a/src/main/res/values/dimens.xml b/src/main/res/values/dimens.xml index dc49535f..9a86ef66 100644 --- a/src/main/res/values/dimens.xml +++ b/src/main/res/values/dimens.xml @@ -1,4 +1,5 @@ 72dp + 4dp diff --git a/src/main/res/values/styles.xml b/src/main/res/values/styles.xml index 305fec20..f6024320 100644 --- a/src/main/res/values/styles.xml +++ b/src/main/res/values/styles.xml @@ -50,6 +50,11 @@ 48dp + + diff --git a/src/main/res/values/themes.xml b/src/main/res/values/themes.xml index d4f33fc6..242ae1d9 100644 --- a/src/main/res/values/themes.xml +++ b/src/main/res/values/themes.xml @@ -4,11 +4,10 @@ - - + -