Search in sources :

Example 1 with DownloadManager

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();
    }
}
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 DownloadManager

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);
    }
}
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 3 with DownloadManager

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();
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Intent(android.content.Intent) DownloadManager(com.hippo.ehviewer.download.DownloadManager) DownloadService(com.hippo.ehviewer.download.DownloadService) ListCheckBoxDialogBuilder(com.hippo.app.ListCheckBoxDialogBuilder) DownloadLabel(com.hippo.ehviewer.dao.DownloadLabel)

Example 4 with DownloadManager

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");
    }
}
Also used : Context(android.content.Context) DownloadManager(com.hippo.ehviewer.download.DownloadManager)

Example 5 with DownloadManager

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;
}
Also used : Context(android.content.Context) DialogInterface(android.content.DialogInterface) CheckBoxDialogBuilder(com.hippo.app.CheckBoxDialogBuilder) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) EasyRecyclerView(com.hippo.easyrecyclerview.EasyRecyclerView) ShowcaseView(com.github.amlcurran.showcaseview.ShowcaseView) View(android.view.View) AdapterView(android.widget.AdapterView) RecyclerView(androidx.recyclerview.widget.RecyclerView) SimpleRatingView(com.hippo.ehviewer.widget.SimpleRatingView) LoadImageView(com.hippo.widget.LoadImageView) TextView(android.widget.TextView) ListView(android.widget.ListView) DownloadManager(com.hippo.ehviewer.download.DownloadManager) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) ListView(android.widget.ListView) Announcer(com.hippo.scene.Announcer) List(java.util.List) LongList(com.hippo.yorozuya.collect.LongList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) AdapterView(android.widget.AdapterView) DownloadLabel(com.hippo.ehviewer.dao.DownloadLabel) Toolbar(androidx.appcompat.widget.Toolbar)

Aggregations

DownloadManager (com.hippo.ehviewer.download.DownloadManager)5 DownloadLabel (com.hippo.ehviewer.dao.DownloadLabel)4 Context (android.content.Context)2 Intent (android.content.Intent)2 ListCheckBoxDialogBuilder (com.hippo.app.ListCheckBoxDialogBuilder)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 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 AlertDialog (androidx.appcompat.app.AlertDialog)1 Toolbar (androidx.appcompat.widget.Toolbar)1