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

Show toolbar when calling sub-pref screen (fixes #247) (#251)

* SettingsActivity: Fix var name

* Add verbose logging

* SettingsActivity: Use a handler to call sub pref screen (fixes #247)

* Add verbose log

* Fix toolbar ignoring input after going back from subpref screen

when directly opened in subpref screen before (fixes #247)

* Revert "Add verbose log"

This reverts commit 33207d0721dd38ce5efa04dddb891557ade6f5c3.

* SettingsActivity: Re-register actionbar after a subpref screen dismissal (fixes #247)
This commit is contained in:
Catfriend1 2019-01-20 12:54:55 +00:00 committed by GitHub
parent beee401baa
commit 71c5c494bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,6 +13,7 @@ import android.content.pm.PackageManager;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference; import android.preference.EditTextPreference;
@ -192,6 +193,7 @@ public class SettingsActivity extends SyncthingActivity {
private Options mOptions; private Options mOptions;
private Gui mGui; private Gui mGui;
private Handler mHandler;
private Boolean mPendingConfig = false; private Boolean mPendingConfig = false;
/** /**
@ -367,30 +369,36 @@ public class SettingsActivity extends SyncthingActivity {
} }
screen.findPreference(KEY_SYNCTHING_DATABASE_SIZE).setSummary(getDatabaseSize()); screen.findPreference(KEY_SYNCTHING_DATABASE_SIZE).setSummary(getDatabaseSize());
openSubPrefScreen(screen); // Check if we should directly show a sub preference screen.
Bundle bundle = getArguments();
if (bundle != null) {
// Fix issue #247: "Calling sub pref screen directly won't show toolbar on top"
mHandler = new Handler();
mHandler.post(() -> {
// Open sub preferences screen if EXTRA_OPEN_SUB_PREF_SCREEN was passed in bundle.
openSubPrefScreen(screen, bundle.getString(EXTRA_OPEN_SUB_PREF_SCREEN, ""));
});
}
} }
private void openSubPrefScreen(PreferenceScreen prefScreen) { private void openSubPrefScreen(PreferenceScreen parentPrefScreen, String subPrefScreenId) {
Bundle bundle = getArguments(); if (parentPrefScreen == null ||
if (bundle == null) { subPrefScreenId == null ||
TextUtils.isEmpty(subPrefScreenId)) {
return; return;
} }
String openSubPrefScreen = bundle.getString(EXTRA_OPEN_SUB_PREF_SCREEN, ""); Log.v(TAG, "Transitioning to pref screen " + subPrefScreenId);
// Open sub preferences screen if EXTRA_OPEN_SUB_PREF_SCREEN was passed in bundle. PreferenceScreen desiredSubPrefScreen = (PreferenceScreen) findPreference(subPrefScreenId);
if (openSubPrefScreen != null && !TextUtils.isEmpty(openSubPrefScreen)) { final ListAdapter listAdapter = parentPrefScreen.getRootAdapter();
Log.v(TAG, "Transitioning to pref screen " + openSubPrefScreen);
PreferenceScreen categoryRunConditions = (PreferenceScreen) findPreference(openSubPrefScreen);
final ListAdapter listAdapter = prefScreen.getRootAdapter();
final int itemsCount = listAdapter.getCount(); final int itemsCount = listAdapter.getCount();
for (int itemNumber = 0; itemNumber < itemsCount; ++itemNumber) { for (int itemNumber = 0; itemNumber < itemsCount; ++itemNumber) {
if (listAdapter.getItem(itemNumber).equals(categoryRunConditions)) { if (listAdapter.getItem(itemNumber).equals(desiredSubPrefScreen)) {
// Simulates click on the sub-preference // Simulates click on the sub-preference. This will invoke {@link #onPreferenceTreeClick} subsequently.
prefScreen.onItemClick(null, null, itemNumber, 0); parentPrefScreen.onItemClick(null, null, itemNumber, 0);
break; break;
} }
} }
} }
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
@ -406,11 +414,7 @@ public class SettingsActivity extends SyncthingActivity {
Toolbar toolbar = (Toolbar) layoutInflater.inflate(R.layout.widget_toolbar, root, false); Toolbar toolbar = (Toolbar) layoutInflater.inflate(R.layout.widget_toolbar, root, false);
root.addView(toolbar, 0); root.addView(toolbar, 0);
toolbar.setTitle(((PreferenceScreen) preference).getTitle()); toolbar.setTitle(((PreferenceScreen) preference).getTitle());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { registerActionBar(toolbar);
toolbar.setTouchscreenBlocksFocus(false);
}
syncthingActivity.setSupportActionBar(toolbar);
syncthingActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} catch (Exception e) { } catch (Exception e) {
/** /**
* The above code has been verified working but due to known bugs in the * The above code has been verified working but due to known bugs in the
@ -433,12 +437,31 @@ public class SettingsActivity extends SyncthingActivity {
// User is on a sub-preferences screen. // User is on a sub-preferences screen.
mCurrentPrefScreenDialog.dismiss(); mCurrentPrefScreenDialog.dismiss();
mCurrentPrefScreenDialog = null; mCurrentPrefScreenDialog = null;
// We need to re-register the action bar, see issue #247.
registerActionBar(null);
} }
return true; return true;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
private void registerActionBar(Toolbar toolbar) {
SyncthingActivity syncthingActivity = (SyncthingActivity) getActivity();
if (toolbar == null) {
toolbar = (Toolbar) syncthingActivity.findViewById(R.id.toolbar);
}
if (toolbar == null) {
Log.w(TAG, "registerActionBar: toolbar == null");
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
toolbar.setTouchscreenBlocksFocus(false);
}
syncthingActivity.setSupportActionBar(toolbar);
syncthingActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void setService(SyncthingService syncthingService) { public void setService(SyncthingService syncthingService) {
mSyncthingService = syncthingService; mSyncthingService = syncthingService;
} }