Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/MainPreferences.java

Issue 29603697: Issue 5931 - Create tests for util package (Closed)
Patch Set: Created Nov. 10, 2017, 2:23 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.adblockplus.sbrowser.contentblocker;
19
20 import org.adblockplus.sbrowser.contentblocker.engine.Engine;
21 import org.adblockplus.sbrowser.contentblocker.engine.EngineService;
22 import org.adblockplus.adblockplussbrowser.R;
23 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils;
24
25 import android.app.AlertDialog;
26 import android.app.ProgressDialog;
27 import android.content.DialogInterface;
28 import android.content.DialogInterface.OnClickListener;
29 import android.content.Intent;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.preference.PreferenceActivity;
33 import android.preference.PreferenceManager;
34 import android.text.Html;
35 import android.util.Log;
36 import android.view.Gravity;
37 import android.widget.Button;
38 import android.widget.LinearLayout;
39
40 public class MainPreferences extends PreferenceActivity implements
41 EngineService.OnEngineCreatedCallback, SharedPrefsUtils.OnSharedPreferenceCh angeListener,
42 Engine.SubscriptionUpdateCallback
43 {
44 private static final String TAG = MainPreferences.class.getSimpleName();
45 private Engine engine = null;
46 private AlertDialog dialog;
47 private int dialogTitleResId;
48
49 @Override
50 public void onCreate(Bundle savedInstanceState)
51 {
52 super.onCreate(savedInstanceState);
53 PreferenceManager.setDefaultValues(this, R.xml.preferences_main, false);
54
55 this.getFragmentManager()
56 .beginTransaction()
57 .replace(android.R.id.content, new Preferences())
58 .commit();
59 }
60
61 @Override
62 protected void onStart()
63 {
64 this.dialogTitleResId = R.string.initialization_title;
65 this.dialog = ProgressDialog.show(this,
66 this.getString(this.dialogTitleResId),
67 this.getString(R.string.initialization_message));
68 super.onStart();
69 SharedPrefsUtils.registerOnSharedPreferenceChangeListener(this, this);
70 EngineService.startService(this.getApplicationContext(), this);
71 }
72
73 @Override
74 protected void onStop()
75 {
76 super.onStop();
77 SharedPrefsUtils.unregisterOnSharedPreferenceChangeListener(this, this);
78 this.dismissDialog();
79 }
80
81 private void dismissDialog()
82 {
83 if (this.dialog != null)
84 {
85 this.dialogTitleResId = 0;
86 this.dialog.dismiss();
87 this.dialog = null;
88 }
89 }
90
91 private void checkForCompatibleSBrowserAndProceed()
92 {
93 if (!Engine.hasCompatibleSBrowserInstalled(this.getApplicationContext()))
94 {
95 this.dialogTitleResId = R.string.sbrowser_dialog_title;
96 this.dialog = new AlertDialog.Builder(this)
97 .setCancelable(false)
98 .setTitle(this.dialogTitleResId)
99 .setMessage(Html.fromHtml(getString(R.string.sbrowser_dialog_message)) )
100 .setNeutralButton(R.string.sbrowser_dialog_button, new OnClickListener ()
101 {
102 @Override
103 public void onClick(DialogInterface dialog, int which)
104 {
105 try
106 {
107 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market:/ /details?id="
108 + Engine.SBROWSER_APP_ID)));
109 }
110 catch (final Throwable t)
111 {
112 startActivity(new Intent(Intent.ACTION_VIEW, Uri
113 .parse("https://play.google.com/store/apps/details?id=" + En gine.SBROWSER_APP_ID)));
114 }
115 }
116 }).create();
117 this.dialog.show();
118 }
119 else
120 {
121 this.checkAAStatusAndProceed();
122 }
123 }
124
125 private void checkAAStatusAndProceed()
126 {
127 final boolean aaInfoShown = SharedPrefsUtils.getBoolean(this, R.string.key_a a_info_shown, false);
128 if (!aaInfoShown)
129 {
130 this.dialogTitleResId = R.string.aa_dialog_title;
131 this.dialog = new AlertDialog.Builder(this)
132 .setCancelable(false)
133 .setTitle(this.dialogTitleResId)
134 .setMessage(Html.fromHtml(getString(R.string.aa_dialog_message)))
135 .setNeutralButton(R.string.aa_dialog_button, new OnClickListener()
136 {
137 @Override
138 public void onClick(DialogInterface dialog, int which)
139 {
140 SharedPrefsUtils.putBoolean(MainPreferences.this, R.string.key_aa_ info_shown, true);
141 MainPreferences.this.checkSetupStatus();
142 }
143 }).create();
144 this.dialog.show();
145 }
146 else
147 {
148 this.checkSetupStatus();
149 }
150 }
151
152 private void checkSetupStatus()
153 {
154 final boolean applicationActivated = SharedPrefsUtils.getBoolean(
155 this, R.string.key_application_activated, false);
156
157 if (!applicationActivated)
158 {
159 Log.d(TAG, "Showing setup dialog");
160 this.dialogTitleResId = R.string.setup_dialog_title;
161 this.dialog = new AlertDialog.Builder(this)
162 .setCancelable(false)
163 .setTitle(this.dialogTitleResId)
164 .setMessage(Html.fromHtml(getString(Engine.hasSamsungInternetVersion5O rNewer(MainPreferences.this) ?
165 R.string.setup_dialog_message_sbrowser_5 : R.string.setup_dial og_message_sbrowser_4)))
166 .setNeutralButton(R.string.setup_dialog_button, new OnClickListener()
167 {
168 @Override
169 public void onClick(DialogInterface dialog, int which)
170 {
171 Engine.openSBrowserSettings(MainPreferences.this);
172 }
173 })
174 .create();
175 this.dialog.show();
176
177 final Button btNeutral = this.dialog.getButton(AlertDialog.BUTTON_NEUTRAL) ;
178 final LinearLayout.LayoutParams btNeutralLayoutParams = (LinearLayout.Layo utParams) btNeutral.getLayoutParams();
179 btNeutralLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
180 btNeutralLayoutParams.gravity = Gravity.CENTER;
181 btNeutral.setLayoutParams(btNeutralLayoutParams);
182 }
183 }
184
185 @Override
186 public void onEngineCreated(Engine engine, boolean success)
187 {
188 Log.d(TAG, "onEngineCreated: " + success);
189 this.engine = success ? engine : null;
190
191 if (engine != null)
192 {
193 this.engine.setSubscriptionUpdateCallback(this);
194 }
195
196 if (this.dialogTitleResId == R.string.initialization_title)
197 {
198 this.dismissDialog();
199
200 this.checkForCompatibleSBrowserAndProceed();
201 }
202 }
203
204 @Override
205 public void onSharedPreferenceChanged(String key)
206 {
207 if (this.getString(R.string.key_automatic_updates).equals(key) && this.engin e != null)
208 {
209 this.engine.connectivityChanged();
210 }
211 else if (this.getString(R.string.key_acceptable_ads).equals(key))
212 {
213 final boolean enabled = SharedPrefsUtils.getBoolean(this, R.string.key_acc eptable_ads, true);
214 final String id = "url:" + this.engine.getPrefsDefault(Engine.SUBSCRIPTION S_EXCEPTIONSURL);
215 Log.d(TAG, "Acceptable ads " + (enabled ? "enabled" : "disabled"));
216 this.engine.changeSubscriptionState(id, enabled);
217 }
218 else if (this.getString(R.string.key_application_activated).equals(key))
219 {
220 if (this.dialogTitleResId == R.string.setup_dialog_title)
221 {
222 this.dismissDialog();
223 }
224 }
225 }
226
227 @Override
228 public void subscriptionUpdateRequested(final boolean enabled)
229 {
230 this.dialog = ProgressDialog.show(this, null, enabled
231 ? getString(R.string.add_subscription_dialog_message)
232 : getString(R.string.remove_subscription_dialog_message));
233 }
234
235 @Override
236 public void subscriptionUpdatedApplied()
237 {
238 this.dismissDialog();
239 }
240 }
OLDNEW

Powered by Google App Engine
This is Rietveld