1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-02-09 18:44:43 +00:00

Also show disabled dialog in WebGuiActivity and ShareActivity (fixes #955)

This commit is contained in:
Felix Ableitner 2017-09-26 11:45:15 +09:00
parent 267c2598f8
commit 5aed81a8f3
5 changed files with 124 additions and 102 deletions

View file

@ -60,7 +60,7 @@ import static java.lang.Math.min;
* {@link DeviceListFragment} in different tabs, and
* {@link DrawerFragment} in the navigation drawer.
*/
public class MainActivity extends SyncthingActivity
public class MainActivity extends StateDialogActivity
implements SyncthingService.OnApiChangeListener {
private static final String TAG = "MainActivity";
@ -77,7 +77,6 @@ public class MainActivity extends SyncthingActivity
*/
private static final long USAGE_REPORTING_DIALOG_DELAY = TimeUnit.DAYS.toMillis(3);
private AlertDialog mDisabledDialog;
private AlertDialog mBatteryOptimizationsDialog;
private AlertDialog mQrCodeDialog;
private Dialog mRestartDialog;
@ -100,10 +99,8 @@ public class MainActivity extends SyncthingActivity
public void onApiChange(SyncthingService.State currentState) {
switch (currentState) {
case STARTING:
dismissDisabledDialog();
break;
case ACTIVE:
dismissDisabledDialog();
showBatteryOptimizationDialogIfNecessary();
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerFragment.requestGuiUpdate();
@ -116,9 +113,6 @@ public class MainActivity extends SyncthingActivity
finish();
break;
case DISABLED:
if (!isFinishing()) {
showDisabledDialog();
}
break;
}
}
@ -158,13 +152,6 @@ public class MainActivity extends SyncthingActivity
.show();
}
private void dismissDisabledDialog() {
if (mDisabledDialog != null && mDisabledDialog.isShowing()) {
mDisabledDialog.dismiss();
mDisabledDialog = null;
}
}
/**
* Returns the unix timestamp at which the app was first installed.
*/
@ -264,7 +251,6 @@ public class MainActivity extends SyncthingActivity
@Override
public void onDestroy() {
super.onDestroy();
dismissDisabledDialog();
if (getService() != null) {
getService().unregisterOnApiChangeListener(this);
getService().unregisterOnApiChangeListener(mFolderListFragment);
@ -326,24 +312,6 @@ public class MainActivity extends SyncthingActivity
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void showDisabledDialog() {
mDisabledDialog = new AlertDialog.Builder(this)
.setTitle(R.string.syncthing_disabled_title)
.setMessage(R.string.syncthing_disabled_message)
.setPositiveButton(R.string.syncthing_disabled_change_settings,
(dialogInterface, i) -> {
finish();
startActivity(new Intent(this, SettingsActivity.class));
}
)
.setNegativeButton(R.string.exit,
(dialogInterface, i) -> finish()
)
.setOnCancelListener(dialogInterface -> finish())
.show();
mDisabledDialog.setCanceledOnTouchOutside(false);
}
public void showRestartDialog(){
mRestartDialog = createRestartDialog();
mRestartDialog.show();

View file

@ -40,7 +40,7 @@ import java.util.Map;
* {@link #getDisplayNameForUri} and {@link #getDisplayNameFromContentResolver} are taken from
* ownCloud Android {@see https://github.com/owncloud/android/blob/79664304fdb762b2e04f1ac505f50d0923ddd212/src/com/owncloud/android/utils/UriUtils.java#L193}
*/
public class ShareActivity extends SyncthingActivity
public class ShareActivity extends StateDialogActivity
implements SyncthingActivity.OnServiceConnectedListener, SyncthingService.OnApiChangeListener {
private static final String TAG = "ShareActivity";

View file

@ -0,0 +1,121 @@
package com.nutomic.syncthingandroid.activities;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.service.SyncthingService;
/**
* Handles loading/disabled dialogs.
*/
public abstract class StateDialogActivity extends SyncthingActivity {
private static final String TAG = "StateDialogActivity";
private AlertDialog mLoadingDialog;
private AlertDialog mDisabledDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerOnServiceConnectedListener(() -> {
getService().registerOnApiChangeListener(this::onApiChange);
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (getService() != null) {
getService().unregisterOnApiChangeListener(this::onApiChange);
}
dismissDisabledDialog();
}
private void onApiChange(SyncthingService.State currentState) {
switch (currentState) {
case INIT: // fallthrough
case STARTING:
dismissDisabledDialog();
showLoadingDialog();
break;
case ACTIVE:
dismissDisabledDialog();
dismissLoadingDialog();
break;
case DISABLED:
dismissLoadingDialog();
if (!isFinishing()) {
showDisabledDialog();
}
break;
}
}
private void showDisabledDialog() {
mDisabledDialog = new AlertDialog.Builder(this)
.setTitle(R.string.syncthing_disabled_title)
.setMessage(R.string.syncthing_disabled_message)
.setPositiveButton(R.string.syncthing_disabled_change_settings,
(dialogInterface, i) -> {
finish();
startActivity(new Intent(this, SettingsActivity.class));
}
)
.setNegativeButton(R.string.exit,
(dialogInterface, i) -> ActivityCompat.finishAffinity(this)
)
.setOnCancelListener(dialogInterface -> ActivityCompat.finishAffinity(this))
.show();
mDisabledDialog.setCanceledOnTouchOutside(false);
}
private void dismissDisabledDialog() {
if (mDisabledDialog != null && mDisabledDialog.isShowing()) {
mDisabledDialog.dismiss();
mDisabledDialog = 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);
try {
mLoadingDialog = new AlertDialog.Builder(this)
.setCancelable(false)
.setView(dialogLayout)
.show();
} catch (RuntimeException e) {
// Catch and do nothing, workaround for https://stackoverflow.com/q/46030692/1837158
Log.w(TAG, e);
}
}
private void dismissLoadingDialog() {
if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
mLoadingDialog.dismiss();
mLoadingDialog = null;
}
}
}

View file

@ -1,18 +1,11 @@
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.IBinder;
import android.util.Log;
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;
@ -24,12 +17,9 @@ import java.util.LinkedList;
*/
public abstract class SyncthingActivity extends ToolbarBindingActivity implements ServiceConnection {
private static final String TAG = "SyncthingActivity";
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<>();
@ -52,20 +42,10 @@ public abstract class SyncthingActivity extends ToolbarBindingActivity implement
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();
}
@ -77,19 +57,6 @@ 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.
*/
@ -116,38 +83,4 @@ public abstract class SyncthingActivity extends ToolbarBindingActivity implement
? getService().getApi()
: 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);
try {
mLoadingDialog = new AlertDialog.Builder(this)
.setCancelable(false)
.setView(dialogLayout)
.show();
} catch (RuntimeException e) {
// Catch and do nothing, workaround for https://stackoverflow.com/q/46030692/1837158
Log.w(TAG, e);
}
}
private void dismissLoadingDialog() {
if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
mLoadingDialog.dismiss();
mLoadingDialog = null;
}
}
}

View file

@ -36,7 +36,7 @@ import java.security.cert.X509Certificate;
/**
* Holds a WebView that shows the web ui of the local syncthing instance.
*/
public class WebGuiActivity extends SyncthingActivity
public class WebGuiActivity extends StateDialogActivity
implements SyncthingService.OnWebGuiAvailableListener {
private static final String TAG = "WebGuiActivity";