1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-30 08:11:17 +00:00

Moved restart dialog into SyncthingActivity

This commit is contained in:
Felix Ableitner 2016-11-10 23:35:47 +09:00
parent f005fcd1d2
commit 2ecca8f1bd
2 changed files with 60 additions and 40 deletions

View file

@ -59,8 +59,6 @@ public class MainActivity extends SyncthingActivity
private static final String TAG = "MainActivity"; private static final String TAG = "MainActivity";
static final String EXTRA_FIRST_START = "com.nutomic.syncthing-android.MainActivity.FIRST_START";
/** /**
* Time after first start when usage reporting dialog should be shown. * Time after first start when usage reporting dialog should be shown.
* *
@ -68,7 +66,6 @@ public class MainActivity extends SyncthingActivity
*/ */
private static final long USAGE_REPORTING_DIALOG_DELAY = TimeUnit.DAYS.toMillis(3); private static final long USAGE_REPORTING_DIALOG_DELAY = TimeUnit.DAYS.toMillis(3);
private AlertDialog mLoadingDialog;
private AlertDialog mDisabledDialog; private AlertDialog mDisabledDialog;
private ViewPager mViewPager; private ViewPager mViewPager;
@ -86,16 +83,11 @@ public class MainActivity extends SyncthingActivity
@Override @Override
public void onApiChange(SyncthingService.State currentState) { public void onApiChange(SyncthingService.State currentState) {
switch (currentState) { switch (currentState) {
case INIT:
showLoadingDialog();
break;
case STARTING: case STARTING:
showLoadingDialog();
dismissDisabledDialog(); dismissDisabledDialog();
break; break;
case ACTIVE: case ACTIVE:
dismissDisabledDialog(); dismissDisabledDialog();
dismissLoadingDialog();
showBatteryOptimizationDialogIfNecessary(); showBatteryOptimizationDialogIfNecessary();
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerFragment.requestGuiUpdate(); mDrawerFragment.requestGuiUpdate();
@ -108,7 +100,6 @@ public class MainActivity extends SyncthingActivity
finish(); finish();
break; break;
case DISABLED: case DISABLED:
dismissLoadingDialog();
if (!isFinishing()) { if (!isFinishing()) {
showDisabledDialog(); showDisabledDialog();
} }
@ -162,34 +153,6 @@ public class MainActivity extends SyncthingActivity
return firstInstallTime; return firstInstallTime;
} }
/**
* Shows the loading dialog with the correct text ("creating keys" or "loading").
*/
private void showLoadingDialog() {
if (isFinishing() || mLoadingDialog != null)
return;
LayoutInflater inflater = getLayoutInflater();
@SuppressLint("InflateParams")
View dialogLayout = inflater.inflate(R.layout.dialog_loading, null);
TextView loadingText = (TextView) dialogLayout.findViewById(R.id.loading_text);
loadingText.setText((getIntent().getBooleanExtra(EXTRA_FIRST_START, false))
? R.string.web_gui_creating_key
: R.string.api_loading);
mLoadingDialog = new AlertDialog.Builder(MainActivity.this)
.setCancelable(false)
.setView(dialogLayout)
.show();
}
private void dismissLoadingDialog() {
if (mLoadingDialog != null) {
mLoadingDialog.dismiss();
mLoadingDialog = null;
}
}
private final FragmentPagerAdapter mSectionsPagerAdapter = private final FragmentPagerAdapter mSectionsPagerAdapter =
new FragmentPagerAdapter(getSupportFragmentManager()) { new FragmentPagerAdapter(getSupportFragmentManager()) {
@ -269,7 +232,6 @@ public class MainActivity extends SyncthingActivity
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
dismissDisabledDialog(); dismissDisabledDialog();
dismissLoadingDialog();
if (getService() != null) { if (getService() != null) {
getService().unregisterOnApiChangeListener(this); getService().unregisterOnApiChangeListener(this);
getService().unregisterOnApiChangeListener(mFolderListFragment); getService().unregisterOnApiChangeListener(mFolderListFragment);

View file

@ -1,12 +1,18 @@
package com.nutomic.syncthingandroid.activities; package com.nutomic.syncthingandroid.activities;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.service.RestApi; import com.nutomic.syncthingandroid.service.RestApi;
import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.service.SyncthingServiceBinder; import com.nutomic.syncthingandroid.service.SyncthingServiceBinder;
@ -18,7 +24,10 @@ import java.util.LinkedList;
*/ */
public abstract class SyncthingActivity extends ToolbarBindingActivity implements ServiceConnection { public abstract class SyncthingActivity extends ToolbarBindingActivity implements ServiceConnection {
public static final String EXTRA_FIRST_START = "com.nutomic.syncthing-android.SyncthingActivity.FIRST_START";
private SyncthingService mSyncthingService; private SyncthingService mSyncthingService;
private AlertDialog mLoadingDialog;
private final LinkedList<OnServiceConnectedListener> mServiceConnectedListeners = new LinkedList<>(); private final LinkedList<OnServiceConnectedListener> mServiceConnectedListeners = new LinkedList<>();
@ -38,21 +47,29 @@ public abstract class SyncthingActivity extends ToolbarBindingActivity implement
@Override @Override
protected void onPause() { protected void onPause() {
unbindService(this); unbindService(this);
super.onPause(); super.onPause();
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
bindService(new Intent(this, SyncthingService.class), this, Context.BIND_AUTO_CREATE); bindService(new Intent(this, SyncthingService.class), this, Context.BIND_AUTO_CREATE);
} }
@Override
protected void onDestroy() {
super.onDestroy();
dismissLoadingDialog();
if (getService() != null) {
getService().unregisterOnApiChangeListener(this::onApiChange);
}
}
@Override @Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
SyncthingServiceBinder binder = (SyncthingServiceBinder) iBinder; SyncthingServiceBinder binder = (SyncthingServiceBinder) iBinder;
mSyncthingService = binder.getService(); mSyncthingService = binder.getService();
mSyncthingService.registerOnApiChangeListener(this::onApiChange);
for (OnServiceConnectedListener listener : mServiceConnectedListeners) { for (OnServiceConnectedListener listener : mServiceConnectedListeners) {
listener.onServiceConnected(); listener.onServiceConnected();
} }
@ -64,6 +81,19 @@ public abstract class SyncthingActivity extends ToolbarBindingActivity implement
mSyncthingService = null; mSyncthingService = null;
} }
private void onApiChange(SyncthingService.State currentState) {
switch (currentState) {
case INIT: // fallthrough
case STARTING:
showLoadingDialog();
break;
case ACTIVE: // fallthrough
case DISABLED:
dismissLoadingDialog();
break;
}
}
/** /**
* Used for Fragments to use the Activity's service connection. * Used for Fragments to use the Activity's service connection.
*/ */
@ -91,4 +121,32 @@ public abstract class SyncthingActivity extends ToolbarBindingActivity implement
: null; : null;
} }
/**
* Shows the loading dialog with the correct text ("creating keys" or "loading").
*/
private void showLoadingDialog() {
if (isFinishing() || mLoadingDialog != null)
return;
LayoutInflater inflater = getLayoutInflater();
@SuppressLint("InflateParams")
View dialogLayout = inflater.inflate(R.layout.dialog_loading, null);
TextView loadingText = (TextView) dialogLayout.findViewById(R.id.loading_text);
loadingText.setText((getIntent().getBooleanExtra(EXTRA_FIRST_START, false))
? R.string.web_gui_creating_key
: R.string.api_loading);
mLoadingDialog = new AlertDialog.Builder(this)
.setCancelable(false)
.setView(dialogLayout)
.show();
}
private void dismissLoadingDialog() {
if (mLoadingDialog != null) {
mLoadingDialog.dismiss();
mLoadingDialog = null;
}
}
} }