| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 package org.adblockplus.android; | |
| 2 | |
| 3 import java.util.ArrayList; | |
| 4 import java.util.List; | |
| 5 | |
| 6 import org.adblockplus.android.updater.AlarmReceiver; | |
| 7 | |
| 8 import android.app.AlertDialog; | |
| 9 import android.app.Dialog; | |
| 10 import android.content.ComponentName; | |
| 11 import android.content.DialogInterface; | |
| 12 import android.content.Intent; | |
| 13 import android.content.ServiceConnection; | |
| 14 import android.content.SharedPreferences; | |
| 15 import android.content.pm.PackageInfo; | |
| 16 import android.content.pm.PackageManager.NameNotFoundException; | |
| 17 import android.os.Build; | |
| 18 import android.os.Bundle; | |
| 19 import android.os.IBinder; | |
| 20 import android.preference.Preference; | |
| 21 import android.preference.PreferenceManager; | |
| 22 import android.preference.PreferenceScreen; | |
| 23 import android.preference.Preference.OnPreferenceClickListener; | |
| 24 import android.text.ClipboardManager; | |
| 25 import android.text.TextUtils; | |
| 26 import android.util.Log; | |
| 27 import android.view.View; | |
| 28 import android.widget.TextView; | |
| 29 import android.widget.Toast; | |
| 30 | |
| 31 /** | |
| 32 * Advanced settings UI. | |
| 33 */ | |
| 34 public class AdvancedPreferences extends SummarizedPreferences | |
| 35 { | |
| 36 private final static String TAG = "AdvancedPreferences"; | |
|
Felix Dahlke
2012/10/09 14:27:29
How about "static final" instead of "final static"
Andrey Novikov
2012/10/12 13:19:14
Done.
| |
| 37 | |
| 38 private static final int CONFIGURATION_DIALOG = 1; | |
| 39 | |
| 40 private static ProxyService proxyService = null; | |
| 41 | |
| 42 @Override | |
| 43 public void onCreate(Bundle savedInstanceState) | |
| 44 { | |
| 45 super.onCreate(savedInstanceState); | |
| 46 | |
| 47 addPreferencesFromResource(R.xml.preferences_advanced); | |
| 48 | |
| 49 PreferenceScreen screen = getPreferenceScreen(); | |
| 50 if (Build.VERSION.SDK_INT >= 12) // Honeycomb 3.1 | |
| 51 { | |
| 52 screen.removePreference(findPreference(getString(R.string.pref_proxy))); | |
| 53 } | |
| 54 if (getResources().getBoolean(R.bool.def_release)) | |
| 55 { | |
| 56 screen.removePreference(findPreference(getString(R.string.pref_support))); | |
| 57 } | |
| 58 else | |
| 59 { | |
| 60 Preference prefUpdate = findPreference(getString(R.string.pref_checkupdate )); | |
| 61 prefUpdate.setOnPreferenceClickListener(new OnPreferenceClickListener() { | |
|
Felix Dahlke
2012/10/09 14:27:29
How about putting this opening brace on its own li
Andrey Novikov
2012/10/12 13:19:14
Done.
| |
| 62 public boolean onPreferenceClick(Preference preference) | |
| 63 { | |
| 64 Intent updater = new Intent(getApplicationContext(), AlarmReceiver.cla ss).putExtra("notifynoupdate", true); | |
| 65 sendBroadcast(updater); | |
| 66 return true; | |
| 67 } | |
| 68 }); | |
| 69 | |
| 70 Preference prefConfiguration = findPreference(getString(R.string.pref_conf iguration)); | |
| 71 prefConfiguration.setOnPreferenceClickListener(new OnPreferenceClickListen er() { | |
|
Felix Dahlke
2012/10/09 14:27:29
Same as above, brace should be on its own line.
Andrey Novikov
2012/10/12 13:19:14
Done.
| |
| 72 public boolean onPreferenceClick(Preference preference) | |
| 73 { | |
| 74 showDialog(CONFIGURATION_DIALOG); | |
| 75 return true; | |
| 76 } | |
| 77 }); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 @Override | |
| 82 public void onResume() | |
| 83 { | |
| 84 super.onResume(); | |
| 85 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this ); | |
| 86 int refresh = Integer.valueOf(prefs.getString(getString(R.string.pref_refres h), "0")); | |
| 87 findPreference(getString(R.string.pref_wifirefresh)).setEnabled(refresh > 0) ; | |
| 88 connect(); | |
| 89 } | |
| 90 | |
| 91 @Override | |
| 92 public void onPause() | |
| 93 { | |
| 94 super.onPause(); | |
| 95 disconnect(); | |
| 96 } | |
| 97 | |
| 98 @Override | |
| 99 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str ing key) | |
| 100 { | |
| 101 if (getString(R.string.pref_refresh).equals(key)) | |
| 102 { | |
| 103 int refresh = Integer.valueOf(sharedPreferences.getString(getString(R.stri ng.pref_refresh), "0")); | |
| 104 findPreference(getString(R.string.pref_wifirefresh)).setEnabled(refresh > 0); | |
| 105 } | |
| 106 if (getString(R.string.pref_crashreport).equals(key)) | |
| 107 { | |
| 108 AdblockPlus application = AdblockPlus.getApplication(); | |
| 109 application.updateCrashReportStatus(); | |
| 110 } | |
| 111 super.onSharedPreferenceChanged(sharedPreferences, key); | |
| 112 } | |
| 113 | |
| 114 @Override | |
| 115 protected Dialog onCreateDialog(int id) | |
| 116 { | |
|
Felix Dahlke
2012/10/09 14:27:29
You know I don't like long methods, this is one :)
Andrey Novikov
2012/10/12 13:19:14
We worked out a compromise in adding more comments
| |
| 117 Dialog dialog = null; | |
| 118 switch (id) | |
|
Felix Dahlke
2012/10/09 14:27:29
Why use a switch case if there's only one case han
Andrey Novikov
2012/10/12 13:19:14
This is a well known Android code pattern, there c
| |
| 119 { | |
| 120 case CONFIGURATION_DIALOG: | |
| 121 List<String> items = new ArrayList<String>(); | |
| 122 int versionCode = -1; | |
| 123 try | |
|
Felix Dahlke
2012/10/09 14:27:29
The same code for getting the package info is in A
Andrey Novikov
2012/10/12 13:19:14
NameNotFoundException is thrown when a given packa
Felix Dahlke
2012/10/12 13:29:47
That's why I suggested to return a PackageInfo obj
| |
| 124 { | |
| 125 PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0); | |
| 126 versionCode = pi.versionCode; | |
| 127 } | |
| 128 catch (NameNotFoundException e) | |
| 129 { | |
| 130 // ignore - this shouldn't happen | |
| 131 } | |
| 132 items.add(String.format("API: %d Build: %d", Build.VERSION.SDK_INT, vers ionCode)); | |
| 133 if (proxyService != null) | |
| 134 { | |
| 135 items.add(String.format("Local port: %d", proxyService.port)); | |
| 136 if (proxyService.isTransparent()) | |
| 137 { | |
| 138 items.add("Running in root mode"); | |
| 139 } | |
| 140 if (proxyService.isNativeProxy()) | |
| 141 { | |
| 142 items.add("Uses native proxy"); | |
| 143 } | |
| 144 if (Build.VERSION.SDK_INT >= 12) // Honeycomb 3.1 | |
|
Felix Dahlke
2012/10/09 14:27:29
I think this should be a constant, since it's also
Andrey Novikov
2012/10/12 13:19:14
Done.
| |
| 145 { | |
| 146 String[] px = proxyService.getUserProxy(); | |
| 147 if (px != null) | |
| 148 { | |
| 149 items.add("System settings:"); | |
| 150 items.add(String.format("Host: [%s] Port: [%s] Excl: [%s]", px[0], px[1], px[2])); | |
| 151 } | |
| 152 } | |
| 153 items.add("Proxy settings:"); | |
| 154 items.add(String.format("Host: [%s] Port: [%s] Excl: [%s]", proxyServi ce.proxy.props.getProperty("adblock.proxyHost"), proxyService.proxy.props.getPro perty("adblock.proxyPort"), | |
| 155 proxyService.proxy.props.getProperty("adblock.proxyExcl"))); | |
| 156 if (proxyService.proxy.props.getProperty("adblock.auth") != null) | |
| 157 items.add("Auth: yes"); | |
| 158 } | |
| 159 else | |
| 160 { | |
| 161 items.add("Service not running"); | |
| 162 } | |
| 163 | |
| 164 TextView messageText = new TextView(this); | |
| 165 messageText.setPadding(12, 6, 12, 6); | |
| 166 messageText.setText(TextUtils.join("\n", items)); | |
| 167 messageText.setOnClickListener(new View.OnClickListener() { | |
|
Felix Dahlke
2012/10/09 14:27:29
Brace on a new line please.
Andrey Novikov
2012/10/12 13:19:14
Done.
| |
| 168 | |
| 169 @Override | |
| 170 public void onClick(View v) | |
| 171 { | |
| 172 ClipboardManager manager = (ClipboardManager) getSystemService(CLIPB OARD_SERVICE); | |
| 173 TextView showTextParam = (TextView) v; | |
| 174 manager.setText(showTextParam.getText()); | |
| 175 Toast.makeText(v.getContext(), R.string.msg_clipboard, Toast.LENGTH_ SHORT).show(); | |
| 176 } | |
| 177 }); | |
| 178 | |
| 179 AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
| 180 builder.setView(messageText).setTitle(R.string.configuration_name).setIc on(android.R.drawable.ic_dialog_info).setCancelable(false) | |
| 181 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener( ) { | |
| 182 public void onClick(DialogInterface dialog, int id) | |
| 183 { | |
| 184 dialog.cancel(); | |
| 185 } | |
| 186 }); | |
| 187 dialog = builder.create(); | |
| 188 break; | |
| 189 } | |
| 190 return dialog; | |
| 191 } | |
| 192 | |
| 193 private void connect() | |
| 194 { | |
| 195 bindService(new Intent(this, ProxyService.class), proxyServiceConnection, 0) ; | |
| 196 } | |
| 197 | |
| 198 private void disconnect() | |
| 199 { | |
| 200 unbindService(proxyServiceConnection); | |
| 201 proxyService = null; | |
| 202 } | |
| 203 | |
| 204 private ServiceConnection proxyServiceConnection = new ServiceConnection() { | |
| 205 public void onServiceConnected(ComponentName className, IBinder service) | |
| 206 { | |
| 207 proxyService = ((ProxyService.LocalBinder) service).getService(); | |
| 208 Log.d(TAG, "Proxy service connected"); | |
| 209 } | |
| 210 | |
| 211 public void onServiceDisconnected(ComponentName className) | |
| 212 { | |
| 213 proxyService = null; | |
| 214 Log.d(TAG, "Proxy service disconnected"); | |
| 215 } | |
| 216 }; | |
| 217 } | |
| OLD | NEW |