@ -6,6 +6,8 @@ package acr.browser.lightning.fragment;
@@ -6,6 +6,8 @@ package acr.browser.lightning.fragment;
import android.Manifest ;
import android.app.Activity ;
import android.content.DialogInterface ;
import android.content.pm.ApplicationInfo ;
import android.content.pm.PackageManager ;
import android.os.AsyncTask ;
import android.os.Build ;
import android.os.Bundle ;
@ -15,9 +17,15 @@ import android.preference.PreferenceFragment;
@@ -15,9 +17,15 @@ import android.preference.PreferenceFragment;
import android.support.annotation.NonNull ;
import android.support.annotation.Nullable ;
import android.support.v7.app.AlertDialog ;
import android.util.Log ;
import android.widget.ArrayAdapter ;
import com.anthonycr.grant.PermissionsManager ;
import com.anthonycr.grant.PermissionsResultAction ;
import java.io.File ;
import java.lang.ref.WeakReference ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Comparator ;
import java.util.List ;
@ -26,13 +34,13 @@ import javax.inject.Inject;
@@ -26,13 +34,13 @@ import javax.inject.Inject;
import acr.browser.lightning.R ;
import acr.browser.lightning.app.BrowserApp ;
import acr.browser.lightning.constant.Constants ;
import acr.browser.lightning.database.BookmarkLocalSync ;
import acr.browser.lightning.database.BookmarkLocalSync.Source ;
import acr.browser.lightning.database.BookmarkManager ;
import acr.browser.lightning.database.HistoryItem ;
import com.anthonycr.grant.PermissionsManager ;
import com.anthonycr.grant.PermissionsResultAction ;
import acr.browser.lightning.react.OnSubscribe ;
import acr.browser.lightning.react.Schedulers ;
import acr.browser.lightning.utils.Preconditions ;
import acr.browser.lightning.utils.Utils ;
@ -41,6 +49,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
@@ -41,6 +49,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
private static final String SETTINGS_EXPORT = "export_bookmark" ;
private static final String SETTINGS_IMPORT = "import_bookmark" ;
private static final String SETTINGS_IMPORT_BROWSER = "import_browser" ;
private static final String SETTINGS_DELETE_BOOKMARKS = "delete_bookmarks" ;
@Nullable private Activity mActivity ;
@ -53,27 +62,41 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
@@ -53,27 +62,41 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
Manifest . permission . READ_EXTERNAL_STORAGE ,
Manifest . permission . WRITE_EXTERNAL_STORAGE
} ;
private ImportBookmarksTask mImportTaskReference ;
private static final File mPath = new File ( Environment . getExternalStorageDirectory ( ) . toString ( ) ) ;
private class ImportBookmarksTask extends AsyncTask < Void , Void , Integer > {
@NonNull private final WeakReference < Activity > mActivityReference ;
private final Source mSource ;
public ImportBookmarksTask ( Activity activity ) {
public ImportBookmarksTask ( Activity activity , Source source ) {
mActivityReference = new WeakReference < > ( activity ) ;
mSource = source ;
}
@Override
protected Integer doInBackground ( Void . . . params ) {
List < HistoryItem > list = null ;
if ( getSync ( ) . isStockSupported ( ) ) {
list = getSync ( ) . getBookmarksFromStockBrowser ( ) ;
} else if ( getSync ( ) . isChromeSupported ( ) ) {
list = getSync ( ) . getBookmarksFromChrome ( ) ;
List < HistoryItem > list ;
Log . d ( Constants . TAG , "Loading bookmarks from: " + mSource . name ( ) ) ;
switch ( mSource ) {
case STOCK :
list = getSync ( ) . getBookmarksFromStockBrowser ( ) ;
break ;
case CHROME_STABLE :
list = getSync ( ) . getBookmarksFromChrome ( ) ;
break ;
case CHROME_BETA :
list = getSync ( ) . getBookmarksFromChromeBeta ( ) ;
break ;
case CHROME_DEV :
list = getSync ( ) . getBookmarksFromChromeDev ( ) ;
break ;
default :
list = new ArrayList < > ( 0 ) ;
break ;
}
int count = 0 ;
if ( list ! = null & & ! list . isEmpty ( ) ) {
if ( ! list . isEmpty ( ) ) {
mBookmarkManager . addBookmarkList ( list ) ;
count = list . size ( ) ;
}
@ -124,30 +147,28 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
@@ -124,30 +147,28 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
public void onDestroy ( ) {
super . onDestroy ( ) ;
mActivity = null ;
if ( mImportTaskReference ! = null ) {
mImportTaskReference . cancel ( false ) ;
}
}
private void initPrefs ( ) {
Preference exportpref = findPreference ( SETTINGS_EXPORT ) ;
Preference importpref = findPreference ( SETTINGS_IMPORT ) ;
Preference exportPref = findPreference ( SETTINGS_EXPORT ) ;
Preference importPref = findPreference ( SETTINGS_IMPORT ) ;
Preference deletePref = findPreference ( SETTINGS_DELETE_BOOKMARKS ) ;
exportpref . setOnPreferenceClickListener ( this ) ;
importpref . setOnPreferenceClickListener ( this ) ;
exportPref . setOnPreferenceClickListener ( this ) ;
importPref . setOnPreferenceClickListener ( this ) ;
deletePref . setOnPreferenceClickListener ( this ) ;
BrowserApp . getTaskThread ( ) . execute ( mInitializeImportPreference ) ;
}
BrowserApp . getTaskThread ( ) . execute ( new Runnable ( ) {
@Override
public void run ( ) {
Preference importStock = findPreference ( SETTINGS_IMPORT_BROWSER ) ;
importStock . setEnabled ( getSync ( ) . isBrowserImportSupported ( ) ) ;
importStock . setOnPreferenceClickListener ( BookmarkSettingsFragment . this ) ;
}
} ) ;
private final Runnable mInitializeImportPreference = new Runnable ( ) {
@Override
public void run ( ) {
Preference importStock = findPreference ( SETTINGS_IMPORT_BROWSER ) ;
importStock . setEnabled ( getSync ( ) . isStockSupported ( ) | | getSync ( ) . isChromeSupported ( ) ) ;
importStock . setOnPreferenceClickListener ( BookmarkSettingsFragment . this ) ;
}
} ;
}
@Override
public boolean onPreferenceClick ( @NonNull Preference preference ) {
@ -182,14 +203,125 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
@@ -182,14 +203,125 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
} ) ;
return true ;
case SETTINGS_IMPORT_BROWSER :
mImportTaskReference = new ImportBookmarksTask ( getActivity ( ) ) ;
mImportTaskReference . executeOnExecutor ( AsyncTask . THREAD_POOL_EXECUTOR ) ;
getSync ( ) . getSupportedBrowsers ( ) . subscribeOn ( Schedulers . worker ( ) )
. observeOn ( Schedulers . main ( ) ) . subscribe ( new OnSubscribe < List < Source > > ( ) {
@Override
public void onNext ( @Nullable List < Source > items ) {
Activity activity = getActivity ( ) ;
if ( items = = null | | activity = = null ) {
return ;
}
List < String > titles = buildTitleList ( activity , items ) ;
showChooserDialog ( activity , titles ) ;
}
} ) ;
return true ;
case SETTINGS_DELETE_BOOKMARKS :
showDeleteBookmarksDialog ( ) ;
return true ;
default :
return false ;
}
}
private void showDeleteBookmarksDialog ( ) {
Activity activity = getActivity ( ) ;
if ( activity = = null ) {
return ;
}
AlertDialog . Builder builder = new AlertDialog . Builder ( activity ) ;
builder . setTitle ( R . string . action_delete ) ;
builder . setMessage ( R . string . action_delete_all_bookmarks ) ;
builder . setNegativeButton ( R . string . no , null ) ;
builder . setPositiveButton ( R . string . yes , new DialogInterface . OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog , int which ) {
mBookmarkManager . deleteAllBookmarks ( ) ;
}
} ) ;
builder . show ( ) ;
}
@NonNull
private List < String > buildTitleList ( @NonNull Activity activity , @NonNull List < Source > items ) {
List < String > titles = new ArrayList < > ( ) ;
String title ;
for ( Source source : items ) {
switch ( source ) {
case STOCK :
titles . add ( getString ( R . string . stock_browser ) ) ;
break ;
case CHROME_STABLE :
title = getTitle ( activity , "com.android.chrome" ) ;
if ( title ! = null ) {
titles . add ( title ) ;
}
break ;
case CHROME_BETA :
title = getTitle ( activity , "com.chrome.beta" ) ;
if ( title ! = null ) {
titles . add ( title ) ;
}
break ;
case CHROME_DEV :
title = getTitle ( activity , "com.chrome.beta" ) ;
if ( title ! = null ) {
titles . add ( title ) ;
}
break ;
default :
break ;
}
}
return titles ;
}
private void showChooserDialog ( final Activity activity , List < String > list ) {
AlertDialog . Builder builder = new AlertDialog . Builder ( activity ) ;
final ArrayAdapter < String > adapter = new ArrayAdapter < > ( activity ,
android . R . layout . simple_list_item_1 ) ;
for ( String title : list ) {
adapter . add ( title ) ;
}
builder . setTitle ( R . string . supported_browsers_title ) ;
builder . setAdapter ( adapter , new DialogInterface . OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog , int which ) {
String title = adapter . getItem ( which ) ;
Source source = null ;
if ( title . equals ( getString ( R . string . stock_browser ) ) ) {
source = Source . STOCK ;
} else if ( title . equals ( getTitle ( activity , "com.android.chrome" ) ) ) {
source = Source . CHROME_STABLE ;
} else if ( title . equals ( getTitle ( activity , "com.android.beta" ) ) ) {
source = Source . CHROME_BETA ;
} else if ( title . equals ( getTitle ( activity , "com.android.dev" ) ) ) {
source = Source . CHROME_DEV ;
}
if ( source ! = null ) {
new ImportBookmarksTask ( activity , source ) . executeOnExecutor ( AsyncTask . THREAD_POOL_EXECUTOR ) ;
}
}
} ) ;
builder . show ( ) ;
}
@Nullable
private String getTitle ( @NonNull Activity activity , @NonNull String packageName ) {
PackageManager pm = activity . getPackageManager ( ) ;
try {
ApplicationInfo info = pm . getApplicationInfo ( packageName , PackageManager . GET_META_DATA ) ;
CharSequence title = pm . getApplicationLabel ( info ) ;
if ( title ! = null ) {
return title . toString ( ) ;
}
} catch ( PackageManager . NameNotFoundException e ) {
e . printStackTrace ( ) ;
}
return null ;
}
private void loadFileList ( @Nullable File path ) {
File file ;
if ( path ! = null ) {