Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit 6dfce5f

Browse files
committed
Removing static app component state in favor of Injector
1 parent e9efb9e commit 6dfce5f

File tree

7 files changed

+66
-62
lines changed

7 files changed

+66
-62
lines changed

app/src/main/java/acr/browser/lightning/BrowserApp.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class BrowserApp : Application() {
3535
@Inject internal lateinit var logger: Logger
3636
@Inject internal lateinit var buildInfo: BuildInfo
3737

38-
val applicationComponent: AppComponent by lazy { appComponent }
38+
lateinit var applicationComponent: AppComponent
3939

4040
override fun attachBaseContext(base: Context) {
4141
super.attachBaseContext(base)
@@ -84,7 +84,7 @@ class BrowserApp : Application() {
8484
}
8585
}
8686

87-
appComponent = DaggerAppComponent.builder()
87+
applicationComponent = DaggerAppComponent.builder()
8888
.application(this)
8989
.buildInfo(createBuildInfo())
9090
.build()
@@ -123,16 +123,11 @@ class BrowserApp : Application() {
123123
})
124124

125125
companion object {
126-
127126
private const val TAG = "BrowserApp"
128127

129128
init {
130129
AppCompatDelegate.setCompatVectorFromResourcesEnabled(Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT)
131130
}
132-
133-
@JvmStatic
134-
lateinit var appComponent: AppComponent
135-
136131
}
137132

138133
}

app/src/main/java/acr/browser/lightning/di/AppComponent.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import acr.browser.lightning.browser.activity.ThemableBrowserActivity
99
import acr.browser.lightning.browser.bookmarks.BookmarksDrawerView
1010
import acr.browser.lightning.device.BuildInfo
1111
import acr.browser.lightning.dialog.LightningDialogBuilder
12-
import acr.browser.lightning.download.DownloadHandler
1312
import acr.browser.lightning.download.LightningDownloadListener
1413
import acr.browser.lightning.reading.activity.ReadingActivity
1514
import acr.browser.lightning.search.SuggestionsAdapter
1615
import acr.browser.lightning.settings.activity.SettingsActivity
1716
import acr.browser.lightning.settings.activity.ThemableSettingsActivity
1817
import acr.browser.lightning.settings.fragment.*
19-
import acr.browser.lightning.utils.ProxyUtils
2018
import acr.browser.lightning.view.LightningChromeClient
2119
import acr.browser.lightning.view.LightningView
2220
import acr.browser.lightning.view.LightningWebClient
@@ -55,8 +53,6 @@ interface AppComponent {
5553

5654
fun inject(app: BrowserApp)
5755

58-
fun inject(proxyUtils: ProxyUtils)
59-
6056
fun inject(activity: ReadingActivity)
6157

6258
fun inject(webClient: LightningWebClient)
@@ -75,8 +71,6 @@ interface AppComponent {
7571

7672
fun inject(chromeClient: LightningChromeClient)
7773

78-
fun inject(downloadHandler: DownloadHandler)
79-
8074
fun inject(searchBoxModel: SearchBoxModel)
8175

8276
fun inject(generalSettingsFragment: GeneralSettingsFragment)

app/src/main/java/acr/browser/lightning/di/DiExtensions.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:JvmName("Injector")
2+
13
package acr.browser.lightning.di
24

35
import acr.browser.lightning.BrowserApp

app/src/main/java/acr/browser/lightning/download/DownloadHandler.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import javax.inject.Inject;
2424
import javax.inject.Singleton;
2525

26-
import acr.browser.lightning.BrowserApp;
2726
import acr.browser.lightning.BuildConfig;
2827
import acr.browser.lightning.MainActivity;
2928
import acr.browser.lightning.R;
@@ -57,16 +56,26 @@ public class DownloadHandler {
5756

5857
private static final String COOKIE_REQUEST_HEADER = "Cookie";
5958

60-
@Inject DownloadsRepository downloadsRepository;
61-
@Inject DownloadManager downloadManager;
62-
@Inject @DatabaseScheduler Scheduler databaseScheduler;
63-
@Inject @NetworkScheduler Scheduler networkScheduler;
64-
@Inject @MainScheduler Scheduler mainScheduler;
65-
@Inject Logger logger;
59+
private final DownloadsRepository downloadsRepository;
60+
private final DownloadManager downloadManager;
61+
private final Scheduler databaseScheduler;
62+
private final Scheduler networkScheduler;
63+
private final Scheduler mainScheduler;
64+
private final Logger logger;
6665

6766
@Inject
68-
public DownloadHandler() {
69-
BrowserApp.getAppComponent().inject(this);
67+
public DownloadHandler(DownloadsRepository downloadsRepository,
68+
DownloadManager downloadManager,
69+
@DatabaseScheduler Scheduler databaseScheduler,
70+
@NetworkScheduler Scheduler networkScheduler,
71+
@MainScheduler Scheduler mainScheduler,
72+
Logger logger) {
73+
this.downloadsRepository = downloadsRepository;
74+
this.downloadManager = downloadManager;
75+
this.databaseScheduler = databaseScheduler;
76+
this.networkScheduler = networkScheduler;
77+
this.mainScheduler = mainScheduler;
78+
this.logger = logger;
7079
}
7180

7281
/**
@@ -77,15 +86,15 @@ public DownloadHandler() {
7786
* @param url The full url to the content that should be downloaded
7887
* @param userAgent User agent of the downloading application.
7988
* @param contentDisposition Content-disposition http header, if present.
80-
* @param mimetype The mimetype of the content reported by the server
89+
* @param mimeType The mimeType of the content reported by the server
8190
* @param contentSize The size of the content
8291
*/
8392
public void onDownloadStart(@NonNull Activity context, @NonNull UserPreferences manager, @NonNull String url, String userAgent,
84-
@Nullable String contentDisposition, String mimetype, @NonNull String contentSize) {
93+
@Nullable String contentDisposition, String mimeType, @NonNull String contentSize) {
8594

8695
logger.log(TAG, "DOWNLOAD: Trying to download from URL: " + url);
8796
logger.log(TAG, "DOWNLOAD: Content disposition: " + contentDisposition);
88-
logger.log(TAG, "DOWNLOAD: Mimetype: " + mimetype);
97+
logger.log(TAG, "DOWNLOAD: MimeType: " + mimeType);
8998
logger.log(TAG, "DOWNLOAD: User agent: " + userAgent);
9099

91100
// if we're dealing wih A/V content that's not explicitly marked
@@ -95,7 +104,7 @@ public void onDownloadStart(@NonNull Activity context, @NonNull UserPreferences
95104
// query the package manager to see if there's a registered handler
96105
// that matches.
97106
Intent intent = new Intent(Intent.ACTION_VIEW);
98-
intent.setDataAndType(Uri.parse(url), mimetype);
107+
intent.setDataAndType(Uri.parse(url), mimeType);
99108
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
100109
intent.addCategory(Intent.CATEGORY_BROWSABLE);
101110
intent.setComponent(null);
@@ -119,7 +128,7 @@ public void onDownloadStart(@NonNull Activity context, @NonNull UserPreferences
119128
}
120129
}
121130
}
122-
onDownloadStartNoStream(context, manager, url, userAgent, contentDisposition, mimetype, contentSize);
131+
onDownloadStartNoStream(context, manager, url, userAgent, contentDisposition, mimeType, contentSize);
123132
}
124133

125134
// This is to work around the fact that java.net.URI throws Exceptions
@@ -299,7 +308,7 @@ private void onDownloadStartNoStream(@NonNull final Activity context, @NonNull U
299308
}
300309

301310
private static boolean isWriteAccessAvailable(@NonNull Uri fileUri) {
302-
if (fileUri.getPath() == null){
311+
if (fileUri.getPath() == null) {
303312
return false;
304313
}
305314
File file = new File(fileUri.getPath());

app/src/main/java/acr/browser/lightning/download/LightningDownloadListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
import javax.inject.Inject;
1818

19-
import acr.browser.lightning.BrowserApp;
2019
import acr.browser.lightning.R;
2120
import acr.browser.lightning.database.downloads.DownloadsRepository;
21+
import acr.browser.lightning.di.Injector;
2222
import acr.browser.lightning.dialog.BrowserDialog;
2323
import acr.browser.lightning.log.Logger;
2424
import acr.browser.lightning.preference.UserPreferences;
@@ -37,7 +37,7 @@ public class LightningDownloadListener implements DownloadListener {
3737
@Inject Logger logger;
3838

3939
public LightningDownloadListener(Activity context) {
40-
BrowserApp.getAppComponent().inject(this);
40+
Injector.getInjector(context).inject(this);
4141
mActivity = context;
4242
}
4343

app/src/main/java/acr/browser/lightning/reading/activity/ReadingActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import javax.inject.Inject;
2020

21-
import acr.browser.lightning.BrowserApp;
2221
import acr.browser.lightning.R;
22+
import acr.browser.lightning.di.Injector;
2323
import acr.browser.lightning.di.MainScheduler;
2424
import acr.browser.lightning.di.NetworkScheduler;
2525
import acr.browser.lightning.dialog.BrowserDialog;
@@ -79,7 +79,7 @@ public static void launch(@NonNull Context context, @NonNull String url) {
7979

8080
@Override
8181
protected void onCreate(Bundle savedInstanceState) {
82-
BrowserApp.getAppComponent().inject(this);
82+
Injector.getInjector(this).inject(this);
8383

8484
overridePendingTransition(R.anim.slide_in_from_right, R.anim.fade_out_scale);
8585
mInvert = mUserPreferences.getInvertColors();

app/src/main/java/acr/browser/lightning/utils/ProxyUtils.java

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,41 @@ public final class ProxyUtils {
3838
private static boolean sI2PHelperBound;
3939
private static boolean sI2PProxyInitialized;
4040

41-
@Inject UserPreferences mUserPreferences;
42-
@Inject DeveloperPreferences mDeveloperPreferences;
43-
@Inject I2PAndroidHelper mI2PHelper;
41+
private final UserPreferences userPreferences;
42+
private final DeveloperPreferences developerPreferences;
43+
private final I2PAndroidHelper i2PAndroidHelper;
4444

4545
@Inject
46-
public ProxyUtils() {
47-
BrowserApp.getAppComponent().inject(this);
46+
public ProxyUtils(UserPreferences userPreferences,
47+
DeveloperPreferences developerPreferences,
48+
I2PAndroidHelper i2PAndroidHelper) {
49+
this.userPreferences = userPreferences;
50+
this.developerPreferences = developerPreferences;
51+
this.i2PAndroidHelper = i2PAndroidHelper;
4852
}
4953

5054
/*
5155
* If Orbot/Tor or I2P is installed, prompt the user if they want to enable
5256
* proxying for this session
5357
*/
5458
public void checkForProxy(@NonNull final Activity activity) {
55-
final ProxyChoice currentProxyChoice = mUserPreferences.getProxyChoice();
59+
final ProxyChoice currentProxyChoice = userPreferences.getProxyChoice();
5660

5761
final boolean orbotInstalled = OrbotHelper.isOrbotInstalled(activity);
58-
boolean orbotChecked = mDeveloperPreferences.getCheckedForTor();
62+
boolean orbotChecked = developerPreferences.getCheckedForTor();
5963
boolean orbot = orbotInstalled && !orbotChecked;
6064

61-
boolean i2pInstalled = mI2PHelper.isI2PAndroidInstalled();
62-
boolean i2pChecked = mDeveloperPreferences.getCheckedForI2P();
65+
boolean i2pInstalled = i2PAndroidHelper.isI2PAndroidInstalled();
66+
boolean i2pChecked = developerPreferences.getCheckedForI2P();
6367
boolean i2p = i2pInstalled && !i2pChecked;
6468

6569
// Do only once per install
6670
if (currentProxyChoice != ProxyChoice.NONE && (orbot || i2p)) {
6771
if (orbot) {
68-
mDeveloperPreferences.setCheckedForTor(true);
72+
developerPreferences.setCheckedForTor(true);
6973
}
7074
if (i2p) {
71-
mDeveloperPreferences.setCheckedForI2P(true);
75+
developerPreferences.setCheckedForI2P(true);
7276
}
7377
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
7478

@@ -80,27 +84,27 @@ public void checkForProxy(@NonNull final Activity activity) {
8084
list.add(new Pair<>(proxyChoice, proxyChoices[proxyChoice.getValue()]));
8185
}
8286
builder.setTitle(activity.getResources().getString(R.string.http_proxy));
83-
AlertDialogExtensionsKt.withSingleChoiceItems(builder, list, mUserPreferences.getProxyChoice(), newProxyChoice -> {
84-
mUserPreferences.setProxyChoice(newProxyChoice);
87+
AlertDialogExtensionsKt.withSingleChoiceItems(builder, list, userPreferences.getProxyChoice(), newProxyChoice -> {
88+
userPreferences.setProxyChoice(newProxyChoice);
8589
return Unit.INSTANCE;
8690
});
8791
builder.setPositiveButton(activity.getResources().getString(R.string.action_ok),
8892
(dialog, which) -> {
89-
if (mUserPreferences.getProxyChoice() != ProxyChoice.NONE) {
93+
if (userPreferences.getProxyChoice() != ProxyChoice.NONE) {
9094
initializeProxy(activity);
9195
}
9296
});
9397
} else {
9498
DialogInterface.OnClickListener dialogClickListener = (dialog, which) -> {
9599
switch (which) {
96100
case DialogInterface.BUTTON_POSITIVE:
97-
mUserPreferences.setProxyChoice(orbotInstalled
101+
userPreferences.setProxyChoice(orbotInstalled
98102
? ProxyChoice.ORBOT
99103
: ProxyChoice.I2P);
100104
initializeProxy(activity);
101105
break;
102106
case DialogInterface.BUTTON_NEGATIVE:
103-
mUserPreferences.setProxyChoice(ProxyChoice.NONE);
107+
userPreferences.setProxyChoice(ProxyChoice.NONE);
104108
break;
105109
}
106110
};
@@ -121,7 +125,7 @@ private void initializeProxy(@NonNull Activity activity) {
121125
String host;
122126
int port;
123127

124-
switch (mUserPreferences.getProxyChoice()) {
128+
switch (userPreferences.getProxyChoice()) {
125129
case NONE:
126130
// We shouldn't be here
127131
return;
@@ -134,16 +138,16 @@ private void initializeProxy(@NonNull Activity activity) {
134138
break;
135139
case I2P:
136140
sI2PProxyInitialized = true;
137-
if (sI2PHelperBound && !mI2PHelper.isI2PAndroidRunning()) {
138-
mI2PHelper.requestI2PAndroidStart(activity);
141+
if (sI2PHelperBound && !i2PAndroidHelper.isI2PAndroidRunning()) {
142+
i2PAndroidHelper.requestI2PAndroidStart(activity);
139143
}
140144
host = "localhost";
141145
port = 4444;
142146
break;
143147
default:
144148
case MANUAL:
145-
host = mUserPreferences.getProxyHost();
146-
port = mUserPreferences.getProxyPort();
149+
host = userPreferences.getProxyHost();
150+
port = userPreferences.getProxyPort();
147151
break;
148152
}
149153

@@ -156,11 +160,11 @@ private void initializeProxy(@NonNull Activity activity) {
156160
}
157161

158162
public boolean isProxyReady(@NonNull Activity activity) {
159-
if (mUserPreferences.getProxyChoice() == ProxyChoice.I2P) {
160-
if (!mI2PHelper.isI2PAndroidRunning()) {
163+
if (userPreferences.getProxyChoice() == ProxyChoice.I2P) {
164+
if (!i2PAndroidHelper.isI2PAndroidRunning()) {
161165
ActivityExtensions.snackbar(activity, R.string.i2p_not_running);
162166
return false;
163-
} else if (!mI2PHelper.areTunnelsActive()) {
167+
} else if (!i2PAndroidHelper.areTunnelsActive()) {
164168
ActivityExtensions.snackbar(activity, R.string.i2p_tunnels_not_ready);
165169
return false;
166170
}
@@ -170,7 +174,7 @@ public boolean isProxyReady(@NonNull Activity activity) {
170174
}
171175

172176
public void updateProxySettings(@NonNull Activity activity) {
173-
if (mUserPreferences.getProxyChoice() != ProxyChoice.NONE) {
177+
if (userPreferences.getProxyChoice() != ProxyChoice.NONE) {
174178
initializeProxy(activity);
175179
} else {
176180
try {
@@ -184,17 +188,17 @@ public void updateProxySettings(@NonNull Activity activity) {
184188
}
185189

186190
public void onStop() {
187-
mI2PHelper.unbind();
191+
i2PAndroidHelper.unbind();
188192
sI2PHelperBound = false;
189193
}
190194

191195
public void onStart(final Activity activity) {
192-
if (mUserPreferences.getProxyChoice() == ProxyChoice.I2P) {
196+
if (userPreferences.getProxyChoice() == ProxyChoice.I2P) {
193197
// Try to bind to I2P Android
194-
mI2PHelper.bind(() -> {
198+
i2PAndroidHelper.bind(() -> {
195199
sI2PHelperBound = true;
196-
if (sI2PProxyInitialized && !mI2PHelper.isI2PAndroidRunning())
197-
mI2PHelper.requestI2PAndroidStart(activity);
200+
if (sI2PProxyInitialized && !i2PAndroidHelper.isI2PAndroidRunning())
201+
i2PAndroidHelper.requestI2PAndroidStart(activity);
198202
});
199203
}
200204
}

0 commit comments

Comments
 (0)