Search in sources :

Example 1 with NoRemovableStorageException

use of org.mozilla.focus.utils.NoRemovableStorageException in project Rocket by mozilla-tw.

the class RelocateService method moveFile.

/**
 * If removable storage exists, to move a file to it. Once moving completed, also update
 * database for latest file path.
 *
 * @param rowId      id of downloaded file in our database
 * @param downloadId downloadId of downloaded file, need this to update system database
 * @param srcFile    file to be moved
 * @param type       MIME type of the file, to decide sub directory
 */
private void moveFile(final long rowId, final long downloadId, final File srcFile, final String type) {
    final Settings settings = Settings.getInstance(getApplicationContext());
    File destFile = null;
    try {
        final File outputDir = StorageUtils.getTargetDirOnRemovableStorageForDownloads(this, type);
        if (outputDir != null) {
            FileUtils.ensureDir(outputDir);
            destFile = FileUtils.getFileSlot(outputDir, srcFile.getName());
            if (outputDir.getUsableSpace() < srcFile.length()) {
                final CharSequence msg = getString(R.string.message_removable_storage_space_not_enough);
                broadcastUi(msg);
                Log.w(TAG, msg.toString());
                broadcastRelocateFinished(rowId);
                return;
            }
            // instead of rename, to use copy + remove for safety
            boolean copied = FileUtils.copy(srcFile, destFile);
            if (!copied) {
                Log.w(TAG, String.format("cannot copy file from %s to %s", srcFile.getPath(), destFile.getPath()));
                broadcastRelocateFinished(rowId);
                return;
            }
            boolean deleted = srcFile.delete();
            if (!deleted) {
                throw new RuntimeException("Cannot delete original file: " + srcFile.getAbsolutePath());
            }
            // downloaded file is moved, update database to reflect this changing
            final DownloadInfoManager mgr = DownloadInfoManager.getInstance();
            mgr.replacePath(downloadId, destFile.getAbsolutePath(), type);
            // we moved download file to removable-storage, now we should inform user
            if (!settings.getRemovableStorageStateOnCreate()) {
                // avoid sending same message continuously
                if (settings.getShowedStorageMessage() != Settings.STORAGE_MSG_TYPE_REMOVABLE_AVAILABLE) {
                    settings.setShowedStorageMessage(Settings.STORAGE_MSG_TYPE_REMOVABLE_AVAILABLE);
                    final CharSequence msg = getString(R.string.message_start_to_save_to_removable_storage);
                    broadcastUi(msg);
                    Log.w(TAG, msg.toString());
                }
            }
        }
    } catch (NoRemovableStorageException e) {
        // removable-storage existed on app creation, but now it is gone
        // we keep download file in original path, now we should inform user
        broadcastRelocateFinished(rowId);
        if (settings.getRemovableStorageStateOnCreate()) {
            // avoid sending same message continuously
            if (settings.getShowedStorageMessage() != Settings.STORAGE_MSG_TYPE_REMOVABLE_UNAVAILABLE) {
                settings.setShowedStorageMessage(Settings.STORAGE_MSG_TYPE_REMOVABLE_UNAVAILABLE);
                final CharSequence msg = getString(R.string.message_fallback_save_to_primary_external);
                broadcastUi(msg);
                Log.w(TAG, msg.toString());
            }
        }
        e.printStackTrace();
    } catch (Exception e) {
        // if anything wrong, try to keep original file
        broadcastRelocateFinished(rowId);
        try {
            if ((destFile != null) && destFile.exists() && destFile.canWrite() && srcFile.exists()) {
                if (destFile.delete()) {
                    Log.w(TAG, "cannot delete copied file: " + destFile.getAbsolutePath());
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        e.printStackTrace();
    }
}
Also used : NoRemovableStorageException(org.mozilla.focus.utils.NoRemovableStorageException) File(java.io.File) Settings(org.mozilla.focus.utils.Settings) DownloadInfoManager(org.mozilla.focus.download.DownloadInfoManager) NoRemovableStorageException(org.mozilla.focus.utils.NoRemovableStorageException)

Example 2 with NoRemovableStorageException

use of org.mozilla.focus.utils.NoRemovableStorageException in project Rocket by mozilla-tw.

the class MainActivity method asyncCheckStorage.

/**
 * To check existence of removable storage, and write result to preference
 */
private void asyncCheckStorage() {
    boolean exist;
    try {
        final File dir = StorageUtils.getTargetDirOnRemovableStorageForDownloads(this, "*/*");
        exist = (dir != null);
    } catch (NoRemovableStorageException e) {
        exist = false;
    }
    Settings.getInstance(this).setRemovableStorageStateOnCreate(exist);
}
Also used : NoRemovableStorageException(org.mozilla.focus.utils.NoRemovableStorageException) File(java.io.File)

Example 3 with NoRemovableStorageException

use of org.mozilla.focus.utils.NoRemovableStorageException in project Rocket by mozilla-tw.

the class DownloadTest method triggerDownload_showToast.

@Test
public void triggerDownload_showToast() throws InterruptedException, UiObjectNotFoundException, IOException {
    // Start the activity
    activityRule.launchActivity(new Intent());
    final SessionLoadedIdlingResource sessionLoadedIdlingResource = new SessionLoadedIdlingResource(activityRule.getActivity());
    // Click and prepare to enter the URL
    onView(withId(R.id.home_fragment_fake_input)).perform(click());
    // Enter URL and load the page
    onView(withId(R.id.url_edit)).perform(replaceText(webServer.url(TEST_PATH).toString()), pressImeActionButton());
    // Waiting for page loading completes
    IdlingRegistry.getInstance().register(sessionLoadedIdlingResource);
    // Find the element in HTML with id "download" after the page is loaded
    onWebView().withElement(findElement(Locator.ID, HTML_ELEMENT_ID_DOWNLOAD)).perform(webClick());
    // Unregister session loaded idling resource
    IdlingRegistry.getInstance().unregister(sessionLoadedIdlingResource);
    // If there's no removable storage for Downloads, we skip this test
    try {
        final File dir = StorageUtils.getTargetDirOnRemovableStorageForDownloads(activityRule.getActivity(), "*/*");
        // Check if toast is displayed.
        onView(withText(R.string.download_started)).inRoot(withDecorView(not(is(activityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));
    } catch (NoRemovableStorageException e) {
        Log.e(TAG, "there's no removable storage for DownloadTest so we skip this.");
    }
// TODO: 1. Long Click and check context menu 2. Check File name after downloads completed.
}
Also used : SessionLoadedIdlingResource(org.mozilla.focus.helper.SessionLoadedIdlingResource) Intent(android.content.Intent) NoRemovableStorageException(org.mozilla.focus.utils.NoRemovableStorageException) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)3 NoRemovableStorageException (org.mozilla.focus.utils.NoRemovableStorageException)3 Intent (android.content.Intent)1 Test (org.junit.Test)1 DownloadInfoManager (org.mozilla.focus.download.DownloadInfoManager)1 SessionLoadedIdlingResource (org.mozilla.focus.helper.SessionLoadedIdlingResource)1 Settings (org.mozilla.focus.utils.Settings)1