You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
5.0 KiB
142 lines
5.0 KiB
7 years ago
|
package acr.browser.lightning;
|
||
9 years ago
|
|
||
9 years ago
|
import android.app.Activity;
|
||
9 years ago
|
import android.app.Application;
|
||
8 years ago
|
import android.content.ClipData;
|
||
|
import android.content.ClipboardManager;
|
||
9 years ago
|
import android.content.Context;
|
||
9 years ago
|
import android.os.Build;
|
||
8 years ago
|
import android.os.StrictMode;
|
||
9 years ago
|
import android.support.annotation.NonNull;
|
||
8 years ago
|
import android.support.annotation.Nullable;
|
||
8 years ago
|
import android.support.v7.app.AppCompatDelegate;
|
||
9 years ago
|
import android.util.Log;
|
||
9 years ago
|
import android.webkit.WebView;
|
||
9 years ago
|
|
||
8 years ago
|
import com.anthonycr.bonsai.Schedulers;
|
||
9 years ago
|
import com.squareup.leakcanary.LeakCanary;
|
||
9 years ago
|
|
||
8 years ago
|
import java.util.List;
|
||
9 years ago
|
|
||
9 years ago
|
import javax.inject.Inject;
|
||
|
|
||
8 years ago
|
import acr.browser.lightning.database.HistoryItem;
|
||
8 years ago
|
import acr.browser.lightning.database.bookmark.BookmarkExporter;
|
||
8 years ago
|
import acr.browser.lightning.database.bookmark.BookmarkModel;
|
||
7 years ago
|
import acr.browser.lightning.database.bookmark.legacy.LegacyBookmarkManager;
|
||
|
import acr.browser.lightning.di.AppComponent;
|
||
|
import acr.browser.lightning.di.AppModule;
|
||
|
import acr.browser.lightning.di.DaggerAppComponent;
|
||
9 years ago
|
import acr.browser.lightning.preference.PreferenceManager;
|
||
8 years ago
|
import acr.browser.lightning.utils.FileUtils;
|
||
9 years ago
|
import acr.browser.lightning.utils.MemoryLeakUtils;
|
||
8 years ago
|
import acr.browser.lightning.utils.Preconditions;
|
||
9 years ago
|
|
||
9 years ago
|
public class BrowserApp extends Application {
|
||
|
|
||
8 years ago
|
private static final String TAG = "BrowserApp";
|
||
9 years ago
|
|
||
8 years ago
|
static {
|
||
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT);
|
||
|
}
|
||
|
|
||
8 years ago
|
@Nullable private static AppComponent sAppComponent;
|
||
9 years ago
|
|
||
9 years ago
|
@Inject PreferenceManager mPreferenceManager;
|
||
8 years ago
|
@Inject BookmarkModel mBookmarkModel;
|
||
9 years ago
|
|
||
8 years ago
|
@Override
|
||
|
protected void attachBaseContext(Context base) {
|
||
|
super.attachBaseContext(base);
|
||
|
}
|
||
|
|
||
9 years ago
|
@Override
|
||
|
public void onCreate() {
|
||
|
super.onCreate();
|
||
8 years ago
|
if (BuildConfig.DEBUG) {
|
||
|
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
|
||
8 years ago
|
.detectAll()
|
||
|
.penaltyLog()
|
||
|
.build());
|
||
8 years ago
|
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
|
||
8 years ago
|
.detectAll()
|
||
|
.penaltyLog()
|
||
|
.build());
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
|
||
|
|
||
|
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||
|
@Override
|
||
8 years ago
|
public void uncaughtException(Thread thread, @NonNull Throwable ex) {
|
||
8 years ago
|
|
||
|
if (BuildConfig.DEBUG) {
|
||
|
FileUtils.writeCrashToStorage(ex);
|
||
|
}
|
||
|
|
||
|
if (defaultHandler != null) {
|
||
|
defaultHandler.uncaughtException(thread, ex);
|
||
|
} else {
|
||
|
System.exit(2);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
8 years ago
|
sAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
|
||
|
sAppComponent.inject(this);
|
||
9 years ago
|
|
||
8 years ago
|
Schedulers.worker().execute(new Runnable() {
|
||
|
@Override
|
||
|
public void run() {
|
||
8 years ago
|
List<HistoryItem> oldBookmarks = LegacyBookmarkManager.destructiveGetBookmarks(BrowserApp.this);
|
||
8 years ago
|
|
||
|
if (!oldBookmarks.isEmpty()) {
|
||
8 years ago
|
// If there are old bookmarks, import them
|
||
8 years ago
|
mBookmarkModel.addBookmarkList(oldBookmarks).subscribeOn(Schedulers.io()).subscribe();
|
||
8 years ago
|
} else if (mBookmarkModel.count() == 0) {
|
||
|
// If the database is empty, fill it from the assets list
|
||
|
List<HistoryItem> assetsBookmarks = BookmarkExporter.importBookmarksFromAssets(BrowserApp.this);
|
||
|
mBookmarkModel.addBookmarkList(assetsBookmarks).subscribeOn(Schedulers.io()).subscribe();
|
||
8 years ago
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
9 years ago
|
if (mPreferenceManager.getUseLeakCanary() && !isRelease()) {
|
||
|
LeakCanary.install(this);
|
||
|
}
|
||
|
if (!isRelease() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||
9 years ago
|
WebView.setWebContentsDebuggingEnabled(true);
|
||
|
}
|
||
9 years ago
|
|
||
|
registerActivityLifecycleCallbacks(new MemoryLeakUtils.LifecycleAdapter() {
|
||
|
@Override
|
||
|
public void onActivityDestroyed(Activity activity) {
|
||
|
Log.d(TAG, "Cleaning up after the Android framework");
|
||
8 years ago
|
MemoryLeakUtils.clearNextServedView(activity, BrowserApp.this);
|
||
9 years ago
|
}
|
||
|
});
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
@NonNull
|
||
9 years ago
|
public static AppComponent getAppComponent() {
|
||
8 years ago
|
Preconditions.checkNonNull(sAppComponent);
|
||
|
return sAppComponent;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
/**
|
||
|
* Determines whether this is a release build.
|
||
|
*
|
||
|
* @return true if this is a release build, false otherwise.
|
||
|
*/
|
||
|
public static boolean isRelease() {
|
||
|
return !BuildConfig.DEBUG || BuildConfig.BUILD_TYPE.toLowerCase().equals("release");
|
||
|
}
|
||
|
|
||
8 years ago
|
public static void copyToClipboard(@NonNull Context context, @NonNull String string) {
|
||
|
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||
|
ClipData clip = ClipData.newPlainText("URL", string);
|
||
|
clipboard.setPrimaryClip(clip);
|
||
|
}
|
||
|
|
||
9 years ago
|
}
|