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.
166 lines
4.3 KiB
166 lines
4.3 KiB
10 years ago
|
package acr.browser.lightning;
|
||
|
|
||
|
import java.io.File;
|
||
|
import java.util.Arrays;
|
||
|
import java.util.Comparator;
|
||
|
|
||
|
import android.app.AlertDialog;
|
||
|
import android.app.AlertDialog.Builder;
|
||
|
import android.app.Dialog;
|
||
|
import android.content.DialogInterface;
|
||
|
import android.os.Bundle;
|
||
|
import android.os.Environment;
|
||
9 years ago
|
import android.support.v7.widget.Toolbar;
|
||
10 years ago
|
import android.view.MenuItem;
|
||
|
import android.view.View;
|
||
|
import android.view.View.OnClickListener;
|
||
|
import android.widget.LinearLayout;
|
||
|
import android.widget.TextView;
|
||
|
|
||
9 years ago
|
public class BookmarkActivity extends ThemableSettingsActivity implements OnClickListener {
|
||
10 years ago
|
|
||
|
private BookmarkManager mBookmarkManager;
|
||
9 years ago
|
private PreferenceManager mPreferences;
|
||
10 years ago
|
private boolean mSystemBrowser;
|
||
|
private File[] mFileList;
|
||
|
private String[] mFileNameList;
|
||
9 years ago
|
private static final File mPath = new File(Environment.getExternalStorageDirectory().toString());
|
||
10 years ago
|
private static final int DIALOG_LOAD_FILE = 1000;
|
||
|
|
||
|
@Override
|
||
|
public void onCreate(Bundle savedInstanceState) {
|
||
|
super.onCreate(savedInstanceState);
|
||
9 years ago
|
setContentView(R.layout.bookmark_settings);
|
||
10 years ago
|
|
||
9 years ago
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||
|
setSupportActionBar(toolbar);
|
||
|
|
||
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||
10 years ago
|
|
||
|
LinearLayout exportBackup = (LinearLayout) findViewById(R.id.exportBackup);
|
||
|
LinearLayout importBackup = (LinearLayout) findViewById(R.id.importBackup);
|
||
|
LinearLayout importFromBrowser = (LinearLayout) findViewById(R.id.importFromBrowser);
|
||
|
|
||
|
TextView importBookmarks = (TextView) findViewById(R.id.isImportBrowserAvailable);
|
||
|
|
||
9 years ago
|
mBookmarkManager = BookmarkManager.getInstance(getApplicationContext());
|
||
9 years ago
|
mPreferences = PreferenceManager.getInstance();
|
||
|
|
||
9 years ago
|
mSystemBrowser = mPreferences.getSystemBrowserPresent();
|
||
10 years ago
|
|
||
|
exportBackup.setOnClickListener(this);
|
||
|
importBackup.setOnClickListener(this);
|
||
|
importFromBrowser.setOnClickListener(this);
|
||
|
|
||
|
if (mSystemBrowser) {
|
||
|
importBookmarks.setText(getResources().getString(R.string.stock_browser_available));
|
||
|
} else {
|
||
|
importBookmarks.setText(getResources().getString(R.string.stock_browser_unavailable));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onClick(View v) {
|
||
|
switch (v.getId()) {
|
||
|
case R.id.importBackup:
|
||
|
loadFileList(null);
|
||
|
onCreateDialog(DIALOG_LOAD_FILE);
|
||
|
break;
|
||
|
case R.id.importFromBrowser:
|
||
9 years ago
|
mBookmarkManager.importBookmarksFromBrowser(BookmarkActivity.this);
|
||
10 years ago
|
break;
|
||
|
case R.id.exportBackup:
|
||
|
mBookmarkManager.exportBookmarks();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||
|
finish();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
private void loadFileList(File path) {
|
||
|
File file;
|
||
|
if (path != null) {
|
||
|
file = path;
|
||
|
} else {
|
||
|
file = mPath;
|
||
|
}
|
||
|
try {
|
||
|
file.mkdirs();
|
||
|
} catch (SecurityException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
if (file.exists()) {
|
||
|
mFileList = file.listFiles();
|
||
|
} else {
|
||
|
mFileList = new File[0];
|
||
|
}
|
||
|
|
||
10 years ago
|
Arrays.sort(mFileList, new SortName());
|
||
10 years ago
|
|
||
|
if (mFileList == null) {
|
||
|
mFileNameList = new String[0];
|
||
|
mFileList = new File[0];
|
||
|
} else {
|
||
|
mFileNameList = new String[mFileList.length];
|
||
|
}
|
||
|
for (int n = 0; n < mFileList.length; n++) {
|
||
|
mFileNameList[n] = mFileList[n].getName();
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
public class SortName implements Comparator<File> {
|
||
|
|
||
|
@Override
|
||
|
public int compare(File a, File b) {
|
||
|
if (a.isDirectory() && b.isDirectory())
|
||
|
return a.getName().compareTo(b.getName());
|
||
|
|
||
|
if (a.isDirectory())
|
||
|
return -1;
|
||
|
|
||
|
if (b.isDirectory())
|
||
|
return 1;
|
||
|
|
||
|
if (a.isFile() && b.isFile())
|
||
|
return a.getName().compareTo(b.getName());
|
||
|
else
|
||
|
return 1;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
protected Dialog onCreateDialog(int id) {
|
||
|
Dialog dialog = null;
|
||
|
final AlertDialog.Builder builder = new Builder(this);
|
||
|
|
||
|
switch (id) {
|
||
|
case DIALOG_LOAD_FILE:
|
||
|
builder.setTitle(R.string.title_chooser);
|
||
|
if (mFileList == null) {
|
||
|
dialog = builder.create();
|
||
|
return dialog;
|
||
|
}
|
||
|
builder.setItems(mFileNameList, new DialogInterface.OnClickListener() {
|
||
|
|
||
|
@Override
|
||
|
public void onClick(DialogInterface dialog, int which) {
|
||
|
if (mFileList[which].isDirectory()) {
|
||
|
loadFileList(mFileList[which]);
|
||
|
builder.setItems(mFileNameList, this);
|
||
|
builder.show();
|
||
|
} else {
|
||
9 years ago
|
mBookmarkManager.importBookmarksFromFile(mFileList[which], BookmarkActivity.this);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
});
|
||
|
break;
|
||
|
}
|
||
|
dialog = builder.show();
|
||
|
return dialog;
|
||
|
}
|
||
|
}
|