1313import android .os .Bundle ;
1414import android .os .Handler ;
1515import android .os .IBinder ;
16- import android .util . DisplayMetrics ;
16+ import android .text . TextUtils ;
1717import android .util .Log ;
18- import android .util .TypedValue ;
1918import android .view .KeyEvent ;
2019import android .view .LayoutInflater ;
2120import android .view .MenuItem ;
2221import android .view .View ;
23- import android .view .ViewGroup ;
2422import android .widget .Button ;
2523import android .widget .ProgressBar ;
2624import android .widget .TextView ;
25+ import android .widget .Toast ;
2726
2827import androidx .activity .OnBackPressedCallback ;
2928import androidx .appcompat .app .ActionBar ;
6665
6766import javax .inject .Inject ;
6867
69- import static java .lang .Math .min ;
70-
7168/**
7269 * Shows {@link FolderListFragment} and
7370 * {@link DeviceListFragment} in different tabs, and
@@ -80,8 +77,6 @@ public class MainActivity extends SyncthingActivity
8077
8178 private Boolean ENABLE_VERBOSE_LOG = false ;
8279
83- private static final String IS_SHOWING_RESTART_DIALOG = "RESTART_DIALOG_STATE" ;
84-
8580 private static final int FOLDER_FRAGMENT_ID = 0 ;
8681 private static final int DEVICE_FRAGMENT_ID = 1 ;
8782 private static final int STATUS_FRAGMENT_ID = 2 ;
@@ -121,7 +116,7 @@ public class MainActivity extends SyncthingActivity
121116 // Session flag to track if user chose "remind later" for important news
122117 private boolean mImportantNewsRemindLaterThisSession = false ;
123118
124- @ Inject SharedPreferences mPreferences ;
119+ @ Inject public SharedPreferences mPreferences ;
125120 private ConfigRouter mConfig ;
126121 private final Handler mUIRefreshHandler = new Handler ();
127122
@@ -258,17 +253,10 @@ public void onCreate(Bundle savedInstanceState) {
258253 mDrawerFragment = new DrawerFragment ();
259254 }
260255
261- if (savedInstanceState != null ) {
262- if (savedInstanceState .getBoolean (IS_SHOWING_RESTART_DIALOG )){
263- showRestartDialog ();
264- }
265- }
266-
267256 fm .beginTransaction ().replace (R .id .drawer , mDrawerFragment ).commitAllowingStateLoss ();
268- mDrawerToggle = new Toggle (this , mDrawerLayout );
257+ mDrawerToggle = new Toggle (this , mDrawerLayout , mDrawerFragment );
269258 mDrawerLayout .setDrawerLockMode (DrawerLayout .LOCK_MODE_UNLOCKED );
270259 mDrawerLayout .addDrawerListener (mDrawerToggle );
271- setOptimalDrawerWidth (findViewById (R .id .drawer ));
272260
273261 /**
274262 * SyncthingService needs to be started from this activity as the user
@@ -442,7 +430,6 @@ protected void onSaveInstanceState(Bundle outState) {
442430 putFragment .accept (mDeviceListFragment );
443431 putFragment .accept (mStatusFragment );
444432
445- outState .putBoolean (IS_SHOWING_RESTART_DIALOG , mRestartDialog != null && mRestartDialog .isShowing ());
446433 Util .dismissDialogSafe (mRestartDialog , this );
447434 Util .dismissDialogSafe (mUsageReportingDialog , this );
448435 }
@@ -497,7 +484,12 @@ public void showRestartDialog(){
497484 mRestartDialog .show ();
498485 }
499486
500- public void showQrCodeDialog (String deviceId ) {
487+ public void showQrCodeDialog () {
488+ String deviceId = mPreferences .getString (Constants .PREF_LOCAL_DEVICE_ID , "" );
489+ if (TextUtils .isEmpty (deviceId )) {
490+ Toast .makeText (this , R .string .could_not_access_deviceid , Toast .LENGTH_SHORT ).show ();
491+ return ;
492+ }
501493 RestApi restApi = getApi ();
502494 List <Device > devices = mConfig .getDevices (restApi , true );
503495 String deviceName = "" ;
@@ -525,10 +517,20 @@ public boolean onOptionsItemSelected(MenuItem item) {
525517 /**
526518 * Handles drawer opened and closed events, toggling option menu state.
527519 */
528- private class Toggle extends ActionBarDrawerToggle {
529- public Toggle (AppCompatActivity activity , DrawerLayout drawerLayout ) {
520+ private static class Toggle extends ActionBarDrawerToggle {
521+
522+ private final DrawerFragment drawerFragment ;
523+
524+ public Toggle (AppCompatActivity activity , DrawerLayout drawerLayout , DrawerFragment drawerFragment ) {
530525 super (activity , drawerLayout , R .string .open_main_menu , R .string .close_main_menu );
531526 setDrawerIndicatorEnabled (false );
527+ this .drawerFragment = drawerFragment ;
528+ }
529+
530+ @ Override
531+ public void onDrawerOpened (View drawerView ) {
532+ super .onDrawerOpened (drawerView );
533+ drawerFragment .drawerOpened ();
532534 }
533535
534536 @ Override
@@ -563,25 +565,6 @@ public boolean onKeyDown(int keyCode, KeyEvent e) {
563565 return super .onKeyDown (keyCode , e );
564566 }
565567
566- /**
567- * Calculating width based on
568- * http://www.google.com/design/spec/patterns/navigation-drawer.html#navigation-drawer-specs.
569- */
570- private void setOptimalDrawerWidth (View drawerContainer ) {
571- int actionBarSize = 0 ;
572- TypedValue tv = new TypedValue ();
573- if (getTheme ().resolveAttribute (android .R .attr .actionBarSize , tv , true )) {
574- actionBarSize = TypedValue .complexToDimensionPixelSize (tv .data ,getResources ().getDisplayMetrics ());
575- }
576-
577- ViewGroup .LayoutParams params = drawerContainer .getLayoutParams ();
578- DisplayMetrics displayMetrics = getResources ().getDisplayMetrics ();
579- int minScreenWidth = min (displayMetrics .widthPixels , displayMetrics .heightPixels );
580-
581- params .width = min (minScreenWidth - actionBarSize , 5 * actionBarSize );
582- drawerContainer .requestLayout ();
583- }
584-
585568 /**
586569 * Displays dialog asking user to accept/deny usage reporting.
587570 */
@@ -795,12 +778,13 @@ private void updateExitFabVisibility() {
795778 /**
796779 * Exits the application by stopping the service and finishing the activity.
797780 * This method is called directly from the FAB since it's only visible when safe to exit.
781+ * Also called from drawer's exit button
798782 */
799- private void doExit () {
783+ public void doExit () {
800784 if (isFinishing ()) {
801785 return ;
802786 }
803- Log .i (TAG , "Exiting app on user request via FAB " );
787+ Log .i (TAG , "Exiting app on user request" );
804788 stopService (new Intent (this , SyncthingService .class ));
805789 finishAndRemoveTask ();
806790 }
0 commit comments