github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/clients/android/src/org/camlistore/SettingsActivity.java (about) 1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package org.camlistore; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.ServiceConnection; 23 import android.content.SharedPreferences; 24 import android.net.wifi.WifiInfo; 25 import android.net.wifi.WifiManager; 26 import android.os.Bundle; 27 import android.os.IBinder; 28 import android.os.RemoteException; 29 import android.preference.CheckBoxPreference; 30 import android.preference.EditTextPreference; 31 import android.preference.Preference; 32 import android.preference.Preference.OnPreferenceChangeListener; 33 import android.preference.PreferenceActivity; 34 import android.preference.PreferenceScreen; 35 import android.text.TextUtils; 36 import android.util.Log; 37 38 public class SettingsActivity extends PreferenceActivity { 39 private static final String TAG = "SettingsActivity"; 40 41 private IUploadService mServiceStub = null; 42 43 private EditTextPreference hostPref; 44 private EditTextPreference trustedCertPref; 45 private EditTextPreference usernamePref; 46 private EditTextPreference passwordPref; 47 private EditTextPreference devIPPref; 48 private CheckBoxPreference autoPref; 49 private PreferenceScreen autoOpts; 50 private EditTextPreference maxCacheSizePref; 51 52 private SharedPreferences mSharedPrefs; 53 private Preferences mPrefs; 54 55 private final ServiceConnection mServiceConnection = new ServiceConnection() { 56 @Override 57 public void onServiceConnected(ComponentName name, IBinder service) { 58 mServiceStub = IUploadService.Stub.asInterface(service); 59 } 60 61 @Override 62 public void onServiceDisconnected(ComponentName name) { 63 mServiceStub = null; 64 }; 65 }; 66 67 @Override 68 protected void onCreate(Bundle savedInstanceState) { 69 super.onCreate(savedInstanceState); 70 71 getPreferenceManager().setSharedPreferencesName(Preferences.NAME); 72 addPreferencesFromResource(R.xml.preferences); 73 74 hostPref = (EditTextPreference) findPreference(Preferences.HOST); 75 // TODO(mpl): popup window that proposes to automatically add the cert to 76 // the prefs when we fail to dial an untrusted server (and only in that case). 77 trustedCertPref = (EditTextPreference) findPreference(Preferences.TRUSTED_CERT); 78 usernamePref = (EditTextPreference) findPreference(Preferences.USERNAME); 79 passwordPref = (EditTextPreference) findPreference(Preferences.PASSWORD); 80 autoPref = (CheckBoxPreference) findPreference(Preferences.AUTO); 81 autoOpts = (PreferenceScreen) findPreference(Preferences.AUTO_OPTS); 82 maxCacheSizePref = (EditTextPreference) findPreference(Preferences.MAX_CACHE_MB); 83 devIPPref = (EditTextPreference) findPreference(Preferences.DEV_IP); 84 85 mSharedPrefs = getSharedPreferences(Preferences.NAME, 0); 86 mPrefs = new Preferences(mSharedPrefs); 87 88 // Display defaults. 89 maxCacheSizePref.setSummary(getString( 90 R.string.settings_max_cache_size_summary, mPrefs.maxCacheMb())); 91 92 OnPreferenceChangeListener onChange = new OnPreferenceChangeListener() { 93 @Override 94 public boolean onPreferenceChange(Preference pref, Object newValue) { 95 final String key = pref.getKey(); 96 Log.v(TAG, "preference change for: " + key); 97 98 // Note: newValue isn't yet persisted, but easiest to update the 99 // UI here. 100 String newStr = (newValue instanceof String) ? (String) newValue 101 : null; 102 if (pref == hostPref) { 103 updateHostSummary(newStr); 104 } else if (pref == trustedCertPref) { 105 updateTrustedCertSummary(newStr); 106 } else if (pref == passwordPref) { 107 updatePasswordSummary(newStr); 108 } else if (pref == usernamePref) { 109 updateUsernameSummary(newStr); 110 } else if (pref == maxCacheSizePref) { 111 if (!updateMaxCacheSizeSummary(newStr)) 112 return false; 113 } else if (pref == devIPPref) { 114 updateDevIP(newStr); 115 } 116 return true; // yes, persist it 117 } 118 }; 119 hostPref.setOnPreferenceChangeListener(onChange); 120 trustedCertPref.setOnPreferenceChangeListener(onChange); 121 passwordPref.setOnPreferenceChangeListener(onChange); 122 usernamePref.setOnPreferenceChangeListener(onChange); 123 maxCacheSizePref.setOnPreferenceChangeListener(onChange); 124 devIPPref.setOnPreferenceChangeListener(onChange); 125 } 126 127 private final SharedPreferences.OnSharedPreferenceChangeListener prefChangedHandler = new SharedPreferences.OnSharedPreferenceChangeListener() { 128 @Override 129 public void onSharedPreferenceChanged(SharedPreferences sp, String key) { 130 if (Preferences.AUTO.equals(key)) { 131 boolean val = mPrefs.autoUpload(); 132 updateAutoOpts(val); 133 Log.d(TAG, "AUTO changed to " + val); 134 if (mServiceStub != null) { 135 try { 136 mServiceStub.setBackgroundWatchersEnabled(val); 137 } catch (RemoteException e) { 138 // Ignore. 139 } 140 } 141 } 142 143 } 144 }; 145 146 @Override 147 protected void onPause() { 148 super.onPause(); 149 mSharedPrefs 150 .unregisterOnSharedPreferenceChangeListener(prefChangedHandler); 151 if (mServiceConnection != null) { 152 unbindService(mServiceConnection); 153 } 154 } 155 156 @Override 157 protected void onResume() { 158 super.onResume(); 159 updatePreferenceSummaries(); 160 mSharedPrefs 161 .registerOnSharedPreferenceChangeListener(prefChangedHandler); 162 bindService(new Intent(this, UploadService.class), mServiceConnection, 163 Context.BIND_AUTO_CREATE); 164 } 165 166 private void updatePreferenceSummaries() { 167 updateHostSummary(hostPref.getText()); 168 updateTrustedCertSummary(trustedCertPref.getText()); 169 updatePasswordSummary(passwordPref.getText()); 170 updateAutoOpts(autoPref.isChecked()); 171 updateMaxCacheSizeSummary(maxCacheSizePref.getText()); 172 updateUsernameSummary(usernamePref.getText()); 173 updateDevIP(devIPPref.getText()); 174 } 175 176 private void updateDevIP(String value) { 177 // The Brad-is-lazy shortcut: if the user enters "12", assumes 178 // "10.0.0.12", or whatever 179 // the current wifi connections's /24 is. 180 if (!TextUtils.isEmpty(value) && !value.contains(".")) { 181 WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); 182 WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 183 if (wifiInfo != null) { 184 int ip = wifiInfo.getIpAddress(); 185 value = String.format("%d.%d.%d.", ip & 0xff, (ip >> 8) & 0xff, 186 (ip >> 16) & 0xff) + value; 187 devIPPref.setText(value); 188 mPrefs.setDevIP(value); 189 } 190 191 } 192 boolean enabled = TextUtils.isEmpty(value); 193 hostPref.setEnabled(enabled); 194 trustedCertPref.setEnabled(enabled); 195 usernamePref.setEnabled(enabled); 196 passwordPref.setEnabled(enabled); 197 if (!enabled) { 198 devIPPref.setSummary("Using http://" + value 199 + ":3179 user/pass \"camlistore\", \"pass3179\""); 200 } else { 201 devIPPref.setSummary("(Dev-server IP to override settings above)"); 202 } 203 } 204 205 private void updatePasswordSummary(String value) { 206 if (value != null && value.length() > 0) { 207 passwordPref.setSummary("*********"); 208 } else { 209 passwordPref.setSummary("<unset>"); 210 } 211 } 212 213 private void updateUsernameSummary(String value) { 214 if (value != null && value.length() > 0) { 215 usernamePref.setSummary(value); 216 } else { 217 usernamePref.setSummary("<unset>"); 218 } 219 } 220 221 private void updateHostSummary(String value) { 222 if (value != null && value.length() > 0) { 223 hostPref.setSummary(value); 224 } else { 225 hostPref.setSummary(getString(R.string.settings_host_summary)); 226 } 227 } 228 229 private void updateTrustedCertSummary(String value) { 230 if (value != null && value.length() > 0) { 231 trustedCertPref.setSummary(value); 232 } else { 233 trustedCertPref.setSummary("<unset; optional 20 hex SHA-256 prefix>"); 234 } 235 } 236 237 private void updateAutoOpts(boolean checked) { 238 autoOpts.setEnabled(checked); 239 } 240 241 // Update the summary for the max cache size setting. 242 // Returns true if the value is valid and should be persisted and false 243 // otherwise. 244 private boolean updateMaxCacheSizeSummary(String value) { 245 try { 246 int mb = Integer.parseInt(value); 247 if (mb <= 0) 248 return false; 249 maxCacheSizePref.setSummary(getString( 250 R.string.settings_max_cache_size_summary, mb)); 251 return true; 252 } catch (NumberFormatException e) { 253 return false; 254 } 255 } 256 257 // Convenience method. 258 static void show(Context context) { 259 final Intent intent = new Intent(context, SettingsActivity.class); 260 context.startActivity(intent); 261 } 262 }