1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 22:31:16 +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";
static final String EXTRA_FIRST_START = "com.nutomic.syncthing-android.MainActivity.FIRST_START";
/**
* 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 AlertDialog mLoadingDialog;
private AlertDialog mDisabledDialog;
private ViewPager mViewPager;
@ -86,16 +83,11 @@ public class MainActivity extends SyncthingActivity
@Override
public void onApiChange(SyncthingService.State currentState) {
switch (currentState) {
case INIT:
showLoadingDialog();
break;
case STARTING:
showLoadingDialog();
dismissDisabledDialog();
break;
case ACTIVE:
dismissDisabledDialog();
dismissLoadingDialog();
showBatteryOptimizationDialogIfNecessary();
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerFragment.requestGuiUpdate();
@ -108,7 +100,6 @@ public class MainActivity extends SyncthingActivity
finish();
break;
case DISABLED:
dismissLoadingDialog();
if (!isFinishing()) {
showDisabledDialog();
}
@ -162,34 +153,6 @@ public class MainActivity extends SyncthingActivity
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 =
new FragmentPagerAdapter(getSupportFragmentManager()) {
@ -269,7 +232,6 @@ public class MainActivity extends SyncthingActivity
public void onDestroy() {
super.onDestroy();
dismissDisabledDialog();
dismissLoadingDialog();
if (getService() != null) {
getService().unregisterOnApiChangeListener(this);
getService().unregisterOnApiChangeListener(mFolderListFragment);

View file

@ -1,12 +1,18 @@
package com.nutomic.syncthingandroid.activities;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
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.SyncthingService;
import com.nutomic.syncthingandroid.service.SyncthingServiceBinder;
@ -18,7 +24,10 @@ import java.util.LinkedList;
*/
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 AlertDialog mLoadingDialog;
private final LinkedList<OnServiceConnectedListener> mServiceConnectedListeners = new LinkedList<>();
@ -38,21 +47,29 @@ public abstract class SyncthingActivity extends ToolbarBindingActivity implement
@Override
protected void onPause() {
unbindService(this);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
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
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
SyncthingServiceBinder binder = (SyncthingServiceBinder) iBinder;
mSyncthingService = binder.getService();
mSyncthingService.registerOnApiChangeListener(this::onApiChange);
for (OnServiceConnectedListener listener : mServiceConnectedListeners) {
listener.onServiceConnected();
}
@ -64,6 +81,19 @@ public abstract class SyncthingActivity extends ToolbarBindingActivity implement
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.
*/
@ -91,4 +121,32 @@ public abstract class SyncthingActivity extends ToolbarBindingActivity implement
: 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;
}
}
}