Search in sources :

Example 1 with Settings

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

the class FirstrunPagerAdapter method initForTurboModePage.

private void initForTurboModePage(@NonNull Context context, @NonNull final View group) {
    final Switch widget = (Switch) group.findViewById(R.id.switch_widget);
    final Settings settings = Settings.getInstance(context);
    widget.setVisibility(View.VISIBLE);
    widget.setText(R.string.label_menu_turbo_mode);
    widget.setChecked(settings.shouldUseTurboMode());
    widget.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            settings.setTurboMode(isChecked);
            TelemetryWrapper.toggleFirstRunPageEvent(isChecked);
        }
    });
}
Also used : Switch(android.widget.Switch) Settings(org.mozilla.focus.utils.Settings) CompoundButton(android.widget.CompoundButton)

Example 2 with Settings

use of org.mozilla.focus.utils.Settings 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 3 with Settings

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

the class MainActivity method driveDefaultBrowser.

private void driveDefaultBrowser() {
    final Settings settings = Settings.getInstance(this);
    if (settings.isDefaultBrowserSettingDidShow()) {
        // We don't need to accumulate the count after we've displayed the default browser promotion
        return;
    } else {
        settings.addMenuPreferenceClickCount();
    }
    if (settings.getMenuPreferenceClickCount() == AppConfigWrapper.getDriveDefaultBrowserFromMenuSettingThreshold()) {
        DialogUtils.showDefaultSettingNotification(this);
        TelemetryWrapper.showDefaultSettingNotification();
    }
}
Also used : Settings(org.mozilla.focus.utils.Settings)

Example 4 with Settings

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

the class RelocateService method handleActionMove.

/**
 * Handle action Move in the provided background thread with the provided
 * parameters.
 */
private void handleActionMove(final long rowId, final long downloadId, @NonNull final File srcFile, @Nullable final String mediaType) {
    final Settings settings = Settings.getInstance(getApplicationContext());
    // Do nothing, if user turned off the option
    if (!settings.shouldSaveToRemovableStorage()) {
        broadcastRelocateFinished(rowId);
        return;
    }
    // if the download id is not in our database, ignore this operation
    final DownloadInfoManager mgr = DownloadInfoManager.getInstance();
    if (!mgr.recordExists(downloadId)) {
        return;
    }
    // To move file if we have correct permission
    if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        moveFile(rowId, downloadId, srcFile, mediaType);
    } else {
        // if no permission, send broadcast to UI.
        // If get permission from UI, it should startService again
        broadcastNoPermission(downloadId, srcFile, mediaType);
    }
}
Also used : Settings(org.mozilla.focus.utils.Settings) DownloadInfoManager(org.mozilla.focus.download.DownloadInfoManager)

Aggregations

Settings (org.mozilla.focus.utils.Settings)4 DownloadInfoManager (org.mozilla.focus.download.DownloadInfoManager)2 CompoundButton (android.widget.CompoundButton)1 Switch (android.widget.Switch)1 File (java.io.File)1 NoRemovableStorageException (org.mozilla.focus.utils.NoRemovableStorageException)1