1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 22:31:16 +00:00

Check root availability in background.

This commit is contained in:
Felix Ableitner 2015-05-18 23:15:31 +02:00
parent 98b9029c62
commit 924d033474
2 changed files with 25 additions and 8 deletions

View file

@ -2,6 +2,7 @@ package com.nutomic.syncthingandroid.fragments;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference; import android.preference.EditTextPreference;
@ -51,6 +52,8 @@ public class SettingsFragment extends PreferenceFragment
private CheckBoxPreference mSyncOnlyWifi; private CheckBoxPreference mSyncOnlyWifi;
private Preference mUseRoot;
private PreferenceScreen mOptionsScreen; private PreferenceScreen mOptionsScreen;
private PreferenceScreen mGuiScreen; private PreferenceScreen mGuiScreen;
@ -117,17 +120,17 @@ public class SettingsFragment extends PreferenceFragment
((SyncthingActivity) getActivity()).registerOnServiceConnectedListener(this); ((SyncthingActivity) getActivity()).registerOnServiceConnectedListener(this);
addPreferencesFromResource(R.xml.app_settings); addPreferencesFromResource(R.xml.app_settings);
PreferenceScreen screen = getPreferenceScreen(); final PreferenceScreen screen = getPreferenceScreen();
mAlwaysRunInBackground = (CheckBoxPreference) mAlwaysRunInBackground = (CheckBoxPreference)
findPreference(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND); findPreference(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND);
mSyncOnlyCharging = (CheckBoxPreference) mSyncOnlyCharging = (CheckBoxPreference)
findPreference(SyncthingService.PREF_SYNC_ONLY_CHARGING); findPreference(SyncthingService.PREF_SYNC_ONLY_CHARGING);
mSyncOnlyWifi = (CheckBoxPreference) findPreference(SyncthingService.PREF_SYNC_ONLY_WIFI); mSyncOnlyWifi = (CheckBoxPreference) findPreference(SyncthingService.PREF_SYNC_ONLY_WIFI);
Preference useRoot = findPreference(SyncthingService.PREF_USE_ROOT); mUseRoot = findPreference(SyncthingService.PREF_USE_ROOT);
Preference appVersion = screen.findPreference(APP_VERSION_KEY); Preference appVersion = screen.findPreference(APP_VERSION_KEY);
mOptionsScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_OPTIONS_KEY); mOptionsScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_OPTIONS_KEY);
mGuiScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_GUI_KEY); mGuiScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_GUI_KEY);
Preference user = screen.findPreference(GUI_USER); final Preference user = screen.findPreference(GUI_USER);
Preference password = screen.findPreference(GUI_PASSWORD); Preference password = screen.findPreference(GUI_PASSWORD);
Preference sttrace = findPreference(STTRACE); Preference sttrace = findPreference(STTRACE);
@ -141,10 +144,8 @@ public class SettingsFragment extends PreferenceFragment
mAlwaysRunInBackground.setOnPreferenceChangeListener(this); mAlwaysRunInBackground.setOnPreferenceChangeListener(this);
mSyncOnlyCharging.setOnPreferenceChangeListener(this); mSyncOnlyCharging.setOnPreferenceChangeListener(this);
mSyncOnlyWifi.setOnPreferenceChangeListener(this); mSyncOnlyWifi.setOnPreferenceChangeListener(this);
if (!Shell.SU.available()) { new TestRootTask().execute();
screen.removePreference(useRoot); mUseRoot.setOnPreferenceChangeListener(this);
}
useRoot.setOnPreferenceChangeListener(this);
screen.findPreference(EXPORT_CONFIG).setOnPreferenceClickListener(this); screen.findPreference(EXPORT_CONFIG).setOnPreferenceClickListener(this);
screen.findPreference(IMPORT_CONFIG).setOnPreferenceClickListener(this); screen.findPreference(IMPORT_CONFIG).setOnPreferenceClickListener(this);
screen.findPreference(SYNCTHING_RESET).setOnPreferenceClickListener(this); screen.findPreference(SYNCTHING_RESET).setOnPreferenceClickListener(this);
@ -159,6 +160,21 @@ public class SettingsFragment extends PreferenceFragment
sttrace.setSummary(sp.getString("sttrace", "")); sttrace.setSummary(sp.getString("sttrace", ""));
} }
/**
* Enables or disables {@link #mUseRoot} preference depending whether root is available.
*/
private class TestRootTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
return Shell.SU.available();
}
@Override
protected void onPostExecute(Boolean result) {
mUseRoot.setEnabled(result);
}
}
@Override @Override
public void onServiceConnected() { public void onServiceConnected() {
mSyncthingService = ((SyncthingActivity) getActivity()).getService(); mSyncthingService = ((SyncthingActivity) getActivity()).getService();

View file

@ -29,7 +29,8 @@
android:key="use_root" android:key="use_root"
android:title="Sync as root" android:title="Sync as root"
android:summary="Run syncthing as superuser" android:summary="Run syncthing as superuser"
android:defaultValue="false" /> android:defaultValue="false"
android:enabled="false" />
</PreferenceCategory> </PreferenceCategory>