Search in sources :

Example 1 with DownloadLabel

use of com.hippo.ehviewer.dao.DownloadLabel 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();
    }
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) GalleryInfo(com.hippo.ehviewer.client.data.GalleryInfo) ArrayList(java.util.ArrayList) Intent(android.content.Intent) LongList(com.hippo.yorozuya.collect.LongList) DownloadManager(com.hippo.ehviewer.download.DownloadManager) DownloadService(com.hippo.ehviewer.download.DownloadService) ListCheckBoxDialogBuilder(com.hippo.app.ListCheckBoxDialogBuilder) DownloadLabel(com.hippo.ehviewer.dao.DownloadLabel)

Example 2 with DownloadLabel

use of com.hippo.ehviewer.dao.DownloadLabel in project EhViewer by seven332.

the class DownloadManager method renameLabel.

public void renameLabel(@NonNull String from, @NonNull String to) {
    // Find in label list
    boolean found = false;
    for (DownloadLabel raw : mLabelList) {
        if (from.equals(raw.getLabel())) {
            found = true;
            raw.setLabel(to);
            // Update in DB
            EhDB.updateDownloadLabel(raw);
            break;
        }
    }
    if (!found) {
        return;
    }
    LinkedList<DownloadInfo> list = mMap.remove(from);
    if (list == null) {
        return;
    }
    // Update info label
    for (DownloadInfo info : list) {
        info.label = to;
        // Update in DB
        EhDB.putDownloadInfo(info);
    }
    // Put list back with new label
    mMap.put(to, list);
    // Notify listener
    for (DownloadInfoListener l : mDownloadInfoListeners) {
        l.onRenameLabel(from, to);
    }
}
Also used : DownloadInfo(com.hippo.ehviewer.dao.DownloadInfo) DownloadLabel(com.hippo.ehviewer.dao.DownloadLabel)

Example 3 with DownloadLabel

use of com.hippo.ehviewer.dao.DownloadLabel in project EhViewer by seven332.

the class DownloadManager method moveLabel.

public void moveLabel(int fromPosition, int toPosition) {
    final DownloadLabel item = mLabelList.remove(fromPosition);
    mLabelList.add(toPosition, item);
    EhDB.moveDownloadLabel(fromPosition, toPosition);
    for (DownloadInfoListener l : mDownloadInfoListeners) {
        l.onUpdateLabels();
    }
}
Also used : DownloadLabel(com.hippo.ehviewer.dao.DownloadLabel)

Example 4 with DownloadLabel

use of com.hippo.ehviewer.dao.DownloadLabel 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);
    }
}
Also used : LocalFavoriteInfo(com.hippo.ehviewer.dao.LocalFavoriteInfo) DownloadDirname(com.hippo.ehviewer.dao.DownloadDirname) DownloadManager(com.hippo.ehviewer.download.DownloadManager) DaoMaster(com.hippo.ehviewer.dao.DaoMaster) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Filter(com.hippo.ehviewer.dao.Filter) DownloadInfo(com.hippo.ehviewer.dao.DownloadInfo) QuickSearch(com.hippo.ehviewer.dao.QuickSearch) DownloadLabel(com.hippo.ehviewer.dao.DownloadLabel) HistoryInfo(com.hippo.ehviewer.dao.HistoryInfo) DaoSession(com.hippo.ehviewer.dao.DaoSession)

Example 5 with DownloadLabel

use of com.hippo.ehviewer.dao.DownloadLabel in project EhViewer by seven332.

the class EhDB method addDownloadLabel.

public static synchronized DownloadLabel addDownloadLabel(String label) {
    DownloadLabelDao dao = sDaoSession.getDownloadLabelDao();
    DownloadLabel raw = new DownloadLabel();
    raw.setLabel(label);
    raw.setTime(System.currentTimeMillis());
    raw.setId(dao.insert(raw));
    return raw;
}
Also used : DownloadLabelDao(com.hippo.ehviewer.dao.DownloadLabelDao) DownloadLabel(com.hippo.ehviewer.dao.DownloadLabel)

Aggregations

DownloadLabel (com.hippo.ehviewer.dao.DownloadLabel)10 DownloadInfo (com.hippo.ehviewer.dao.DownloadInfo)4 DownloadManager (com.hippo.ehviewer.download.DownloadManager)4 Intent (android.content.Intent)2 ListCheckBoxDialogBuilder (com.hippo.app.ListCheckBoxDialogBuilder)2 DownloadLabelDao (com.hippo.ehviewer.dao.DownloadLabelDao)2 DownloadService (com.hippo.ehviewer.download.DownloadService)2 LongList (com.hippo.yorozuya.collect.LongList)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 Point (android.graphics.Point)1 AlertDialog (android.support.v7.app.AlertDialog)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1