Example usage for android.os StrictMode allowThreadDiskWrites

List of usage examples for android.os StrictMode allowThreadDiskWrites

Introduction

In this page you can find the example usage for android.os StrictMode allowThreadDiskWrites.

Prototype

public static ThreadPolicy allowThreadDiskWrites() 

Source Link

Document

A convenience wrapper that takes the current ThreadPolicy from #getThreadPolicy , modifies it to permit both disk reads & writes, and sets the new policy with #setThreadPolicy , returning the old policy so you can restore it at the end of a block.

Usage

From source file:org.mozilla.gecko.LocaleAware.java

public static void initializeLocale(Context context) {
    final LocaleManager localeManager = BrowserLocaleManager.getInstance();
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
    StrictMode.allowThreadDiskWrites();
    try {/*from w  w  w.ja v  a2s.co m*/
        localeManager.getAndApplyPersistedLocale(context);
    } finally {
        StrictMode.setThreadPolicy(savedPolicy);
    }
}

From source file:org.chromium.chrome.browser.tabmodel.TabPersistentStore.java

public void saveState() {
    // Temporarily allowing disk access. TODO: Fix. See http://b/5518024
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
    try {//from  www .j av  a2 s.c o  m
        long saveStateStartTime = SystemClock.uptimeMillis();
        // The list of tabs should be saved first in case our activity is terminated early.
        // Explicitly toss out any existing SaveListTask because they only save the TabModel as
        // it looked when the SaveListTask was first created.
        if (mSaveListTask != null)
            mSaveListTask.cancel(true);
        try {
            saveListToFile(serializeTabMetadata());
        } catch (IOException e) {
            Log.w(TAG, "Error while saving tabs state; will attempt to continue...", e);
        }
        logExecutionTime("SaveListTime", saveStateStartTime);

        // Add current tabs to save because they did not get a save signal yet.
        Tab currentStandardTab = TabModelUtils.getCurrentTab(mTabModelSelector.getModel(false));
        if (currentStandardTab != null && !mTabsToSave.contains(currentStandardTab)
                && currentStandardTab.isTabStateDirty()
                // For content URI, the read permission granted to an activity is not
                // persistent.
                && !isTabUrlContentScheme(currentStandardTab)) {
            mTabsToSave.addLast(currentStandardTab);
        }
        Tab currentIncognitoTab = TabModelUtils.getCurrentTab(mTabModelSelector.getModel(true));
        if (currentIncognitoTab != null && !mTabsToSave.contains(currentIncognitoTab)
                && currentIncognitoTab.isTabStateDirty() && !isTabUrlContentScheme(currentIncognitoTab)) {
            mTabsToSave.addLast(currentIncognitoTab);
        }
        // Wait for the current tab to save.
        if (mSaveTabTask != null) {
            // Cancel calls get() to wait for this to finish internally if it has to.
            // The issue is it may assume it cancelled the task, but the task still actually
            // wrote the state to disk.  That's why we have to check mStateSaved here.
            if (mSaveTabTask.cancel(false) && !mSaveTabTask.mStateSaved) {
                // The task was successfully cancelled.  We should try to save this state again.
                Tab cancelledTab = mSaveTabTask.mTab;
                if (!mTabsToSave.contains(cancelledTab) && cancelledTab.isTabStateDirty()
                        && !isTabUrlContentScheme(cancelledTab)) {
                    mTabsToSave.addLast(cancelledTab);
                }
            }

            mSaveTabTask = null;
        }

        long saveTabsStartTime = SystemClock.uptimeMillis();
        // Synchronously save any remaining unsaved tabs (hopefully very few).
        for (Tab tab : mTabsToSave) {
            int id = tab.getId();
            boolean incognito = tab.isIncognito();
            try {
                TabState state = tab.getState();
                if (state != null) {
                    TabState.saveState(getTabStateFile(id, incognito), state, incognito);
                }
            } catch (OutOfMemoryError e) {
                Log.w(TAG, "Out of memory error while attempting to save tab state.  Erasing.");
                deleteTabState(id, incognito);
            }
        }
        mTabsToSave.clear();
        logExecutionTime("SaveTabsTime", saveTabsStartTime);
        logExecutionTime("SaveStateTime", saveStateStartTime);
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }
}

From source file:org.chromium.chrome.browser.customtabs.CustomTabActivity.java

/**
 * Opens the URL currently being displayed in the Custom Tab in the regular browser.
 * @param forceReparenting Whether tab reparenting should be forced for testing.
 *
 * @return Whether or not the tab was sent over successfully.
 *//*  w w  w.j a  v  a  2s . c  o  m*/
boolean openCurrentUrlInBrowser(boolean forceReparenting) {
    Tab tab = getActivityTab();
    if (tab == null)
        return false;

    String url = tab.getUrl();
    if (DomDistillerUrlUtils.isDistilledPage(url)) {
        url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url);
    }
    if (TextUtils.isEmpty(url))
        url = getUrlToLoad();
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(ChromeLauncherActivity.EXTRA_IS_ALLOWED_TO_RETURN_TO_PARENT, false);

    boolean willChromeHandleIntent = getIntentDataProvider().isOpenedByChrome();
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
    StrictMode.allowThreadDiskWrites();
    try {
        willChromeHandleIntent |= ExternalNavigationDelegateImpl.willChromeHandleIntent(this, intent, true);
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }

    Bundle startActivityOptions = ActivityOptionsCompat
            .makeCustomAnimation(this, R.anim.abc_fade_in, R.anim.abc_fade_out).toBundle();
    if (willChromeHandleIntent || forceReparenting) {
        Runnable finalizeCallback = new Runnable() {
            @Override
            public void run() {
                finishAndClose();
            }
        };

        mMainTab = null;
        tab.detachAndStartReparenting(intent, startActivityOptions, finalizeCallback);
    } else {
        // Temporarily allowing disk access while fixing. TODO: http://crbug.com/581860
        StrictMode.allowThreadDiskReads();
        StrictMode.allowThreadDiskWrites();
        try {
            startActivity(intent, startActivityOptions);
        } finally {
            StrictMode.setThreadPolicy(oldPolicy);
        }
    }
    return true;
}