use of com.hippo.ehviewer.download.DownloadManager in project EhViewer by seven332.
the class CommonOperations method startDownload.
// TODO Add context if activity and context are different style
public static void startDownload(final MainActivity activity, final List<GalleryInfo> galleryInfos, boolean forceDefault) {
final DownloadManager dm = EhApplication.getDownloadManager(activity);
LongList toStart = new LongList();
List<GalleryInfo> toAdd = new ArrayList<>();
for (GalleryInfo gi : galleryInfos) {
if (dm.containDownloadInfo(gi.gid)) {
toStart.add(gi.gid);
} else {
toAdd.add(gi);
}
}
if (!toStart.isEmpty()) {
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START_RANGE);
intent.putExtra(DownloadService.KEY_GID_LIST, toStart);
activity.startService(intent);
}
if (toAdd.isEmpty()) {
activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT);
return;
}
boolean justStart = forceDefault;
String label = null;
// Get default download label
if (!justStart && Settings.getHasDefaultDownloadLabel()) {
label = Settings.getDefaultDownloadLabel();
justStart = label == null || dm.containLabel(label);
}
// If there is no other label, just use null label
if (!justStart && 0 == dm.getLabelList().size()) {
justStart = true;
label = null;
}
if (justStart) {
// Got default label
for (GalleryInfo gi : toAdd) {
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_LABEL, label);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi);
activity.startService(intent);
}
// Notify
activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT);
} else {
// Let use chose label
List<DownloadLabel> list = dm.getLabelList();
final String[] items = new String[list.size() + 1];
items[0] = activity.getString(R.string.default_download_label_name);
for (int i = 0, n = list.size(); i < n; i++) {
items[i + 1] = list.get(i).getLabel();
}
new ListCheckBoxDialogBuilder(activity, items, new ListCheckBoxDialogBuilder.OnItemClickListener() {
@Override
public void onItemClick(ListCheckBoxDialogBuilder builder, AlertDialog dialog, int position) {
String label;
if (position == 0) {
label = null;
} else {
label = items[position];
if (!dm.containLabel(label)) {
label = null;
}
}
// Start download
for (GalleryInfo gi : toAdd) {
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_LABEL, label);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi);
activity.startService(intent);
}
// Save settings
if (builder.isChecked()) {
Settings.putHasDefaultDownloadLabel(true);
Settings.putDefaultDownloadLabel(label);
} else {
Settings.putHasDefaultDownloadLabel(false);
}
// Notify
activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT);
}
}, activity.getString(R.string.remember_download_label), false).setTitle(R.string.download).show();
}
}
use of com.hippo.ehviewer.download.DownloadManager in project EhViewer by seven332.
the class EhDB method importDB.
/**
* @param file The db file
* @return error string, null for no error
*/
public static synchronized String importDB(Context context, File file) {
try {
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
int newVersion = DaoMaster.SCHEMA_VERSION;
int oldVersion = db.getVersion();
if (oldVersion < newVersion) {
upgradeDB(db, oldVersion);
db.setVersion(newVersion);
} else if (oldVersion > newVersion) {
return context.getString(R.string.cant_read_the_file);
}
DaoMaster daoMaster = new DaoMaster(db);
DaoSession session = daoMaster.newSession();
// Downloads
DownloadManager manager = EhApplication.getDownloadManager(context);
List<DownloadInfo> downloadInfoList = session.getDownloadsDao().queryBuilder().list();
manager.addDownload(downloadInfoList);
// Download label
List<DownloadLabel> downloadLabelList = session.getDownloadLabelDao().queryBuilder().list();
manager.addDownloadLabel(downloadLabelList);
// Download dirname
List<DownloadDirname> downloadDirnameList = session.getDownloadDirnameDao().queryBuilder().list();
for (DownloadDirname dirname : downloadDirnameList) {
putDownloadDirname(dirname.getGid(), dirname.getDirname());
}
// History
List<HistoryInfo> historyInfoList = session.getHistoryDao().queryBuilder().list();
putHistoryInfo(historyInfoList);
// QuickSearch
List<QuickSearch> quickSearchList = session.getQuickSearchDao().queryBuilder().list();
List<QuickSearch> currentQuickSearchList = sDaoSession.getQuickSearchDao().queryBuilder().list();
for (QuickSearch quickSearch : quickSearchList) {
String name = quickSearch.name;
for (QuickSearch q : currentQuickSearchList) {
if (ObjectUtils.equal(q.name, name)) {
// The same name
name = null;
break;
}
}
if (null == name) {
continue;
}
insertQuickSearch(quickSearch);
}
// LocalFavorites
List<LocalFavoriteInfo> localFavoriteInfoList = session.getLocalFavoritesDao().queryBuilder().list();
for (LocalFavoriteInfo info : localFavoriteInfoList) {
putLocalFavorites(info);
}
// Bookmarks
// TODO
// Filter
List<Filter> filterList = session.getFilterDao().queryBuilder().list();
List<Filter> currentFilterList = sDaoSession.getFilterDao().queryBuilder().list();
for (Filter filter : filterList) {
if (!currentFilterList.contains(filter)) {
addFilter(filter);
}
}
return null;
} catch (Throwable e) {
ExceptionUtils.throwIfFatal(e);
// Ignore
return context.getString(R.string.cant_read_the_file);
}
}
use of com.hippo.ehviewer.download.DownloadManager in project EhViewer by seven332.
the class CommonOperations method startDownload.
// TODO Add context if activity and context are different style
public static void startDownload(final MainActivity activity, final GalleryInfo galleryInfo, boolean forceDefault) {
final DownloadManager dm = EhApplication.getDownloadManager(activity);
boolean justStart = forceDefault || dm.containDownloadInfo(galleryInfo.gid);
String label = null;
// Get default download label
if (!justStart && Settings.getHasDefaultDownloadLabel()) {
label = Settings.getDefaultDownloadLabel();
justStart = label == null || dm.containLabel(label);
}
// If there is no other label, just use null label
if (!justStart && 0 == dm.getLabelList().size()) {
justStart = true;
label = null;
}
if (justStart) {
// Already in download list or get default label
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_LABEL, label);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, galleryInfo);
activity.startService(intent);
// Notify
activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT);
} else {
// Let use chose label
List<DownloadLabel> list = dm.getLabelList();
final String[] items = new String[list.size() + 1];
items[0] = activity.getString(R.string.default_download_label_name);
for (int i = 0, n = list.size(); i < n; i++) {
items[i + 1] = list.get(i).getLabel();
}
new ListCheckBoxDialogBuilder(activity, items, new ListCheckBoxDialogBuilder.OnItemClickListener() {
@Override
public void onItemClick(ListCheckBoxDialogBuilder builder, AlertDialog dialog, int position) {
String label;
if (position == 0) {
label = null;
} else {
label = items[position];
if (!dm.containLabel(label)) {
label = null;
}
}
// Start download
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_LABEL, label);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, galleryInfo);
activity.startService(intent);
// Save settings
if (builder.isChecked()) {
Settings.putHasDefaultDownloadLabel(true);
Settings.putDefaultDownloadLabel(label);
} else {
Settings.putHasDefaultDownloadLabel(false);
}
// Notify
activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT);
}
}, activity.getString(R.string.remember_download_label), false).setTitle(R.string.download).show();
}
}
use of com.hippo.ehviewer.download.DownloadManager in project EhViewer by seven332.
the class DownloadsScene method onDestroy.
@Override
public void onDestroy() {
super.onDestroy();
mList = null;
DownloadManager manager = mDownloadManager;
if (null == manager) {
Context context = getContext2();
if (null != context) {
manager = EhApplication.getDownloadManager(context);
}
} else {
mDownloadManager = null;
}
if (null != manager) {
manager.removeDownloadInfoListener(this);
} else {
Log.e(TAG, "Can't removeDownloadInfoListener");
}
}
use of com.hippo.ehviewer.download.DownloadManager in project EhViewer by seven332.
the class DownloadsScene method onCreateDrawerView.
@Override
public View onCreateDrawerView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.drawer_list, container, false);
final Context context = getContext2();
AssertUtils.assertNotNull(context);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
toolbar.setTitle(R.string.download_labels);
toolbar.inflateMenu(R.menu.drawer_download);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch(id) {
case R.id.action_settings:
startScene(new Announcer(DownloadLabelsScene.class));
return true;
case R.id.action_default_download_label:
DownloadManager dm = mDownloadManager;
if (null == dm) {
return true;
}
List<DownloadLabel> list = dm.getLabelList();
final String[] items = new String[list.size() + 2];
items[0] = getString(R.string.let_me_select);
items[1] = getString(R.string.default_download_label_name);
for (int i = 0, n = list.size(); i < n; i++) {
items[i + 2] = list.get(i).getLabel();
}
new AlertDialog.Builder(context).setTitle(R.string.default_download_label).setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
Settings.putHasDefaultDownloadLabel(false);
} else {
Settings.putHasDefaultDownloadLabel(true);
String label;
if (which == 1) {
label = null;
} else {
label = items[which];
}
Settings.putDefaultDownloadLabel(label);
}
}
}).show();
return true;
}
return false;
}
});
List<DownloadLabel> list = EhApplication.getDownloadManager(context).getLabelList();
final List<String> labels = new ArrayList<>(list.size() + 1);
// Add default label name
labels.add(getString(R.string.default_download_label_name));
for (DownloadLabel raw : list) {
labels.add(raw.getLabel());
}
// TODO handle download label items update
ListView listView = (ListView) view.findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<>(context, R.layout.item_simple_list, labels));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String label;
if (position == 0) {
label = null;
} else {
label = labels.get(position);
}
if (!ObjectUtils.equal(label, mLabel)) {
mLabel = label;
updateForLabel();
updateView();
closeDrawer(Gravity.RIGHT);
}
}
});
return view;
}
Aggregations