Example usage for android.net ConnectivityManager getBackgroundDataSetting

List of usage examples for android.net ConnectivityManager getBackgroundDataSetting

Introduction

In this page you can find the example usage for android.net ConnectivityManager getBackgroundDataSetting.

Prototype

@Deprecated
public boolean getBackgroundDataSetting() 

Source Link

Document

Returns the value of the setting for background data usage.

Usage

From source file:com.mwebster.exchange.SyncManager.java

public void run() {
    mStop = false;// w  ww  . jav  a 2s .  c o m

    // If we're really debugging, turn on all logging
    if (Eas.DEBUG) {
        Eas.USER_LOG = true;
        Eas.PARSER_LOG = true;
        Eas.FILE_LOG = true;
    }

    // If we need to wait for the debugger, do so
    if (Eas.WAIT_DEBUG) {
        Debug.waitForDebugger();
    }

    // Set up our observers; we need them to know when to start/stop various syncs based
    // on the insert/delete/update of mailboxes and accounts
    // We also observe synced messages to trigger upsyncs at the appropriate time
    mResolver.registerContentObserver(Mailbox.CONTENT_URI, false, mMailboxObserver);
    mResolver.registerContentObserver(Message.SYNCED_CONTENT_URI, true, mSyncedMessageObserver);
    mResolver.registerContentObserver(Message.CONTENT_URI, true, mMessageObserver);
    ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, mSyncStatusObserver);
    mAccountsUpdatedListener = new EasAccountsUpdatedListener();
    // TODO Find and fix root cause of duplication
    try {
        AccountManager.get(getApplication()).addOnAccountsUpdatedListener(mAccountsUpdatedListener, mHandler,
                true);
    } catch (IllegalStateException e1) {
        // This exception is more of a warning; we shouldn't be in the state in which we
        // already have a listener.
    }

    // Set up receivers for ConnectivityManager
    mConnectivityReceiver = new ConnectivityReceiver();
    registerReceiver(mConnectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    mBackgroundDataSettingReceiver = new ConnectivityReceiver();
    registerReceiver(mBackgroundDataSettingReceiver,
            new IntentFilter(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED));
    // Save away background data setting; we'll keep track of it with the receiver
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    mBackgroundData = cm.getBackgroundDataSetting();

    // See if any settings have changed while we weren't running...
    checkPIMSyncSettings();

    try {
        while (!mStop) {
            runAwake(SYNC_MANAGER_ID);
            waitForConnectivity();
            mNextWaitReason = "Heartbeat";
            long nextWait = checkMailboxes();
            try {
                synchronized (this) {
                    if (!mKicked) {
                        if (nextWait < 0) {
                            log("Negative wait? Setting to 1s");
                            nextWait = 1 * SECONDS;
                        }
                        if (nextWait > 10 * SECONDS) {
                            log("Next awake in " + nextWait / 1000 + "s: " + mNextWaitReason);
                            runAsleep(SYNC_MANAGER_ID, nextWait + (3 * SECONDS));
                        }
                        wait(nextWait);
                    }
                }
            } catch (InterruptedException e) {
                // Needs to be caught, but causes no problem
            } finally {
                synchronized (this) {
                    if (mKicked) {
                        //log("Wait deferred due to kick");
                        mKicked = false;
                    }
                }
            }
        }
        log("Shutdown requested");
    } catch (RuntimeException e) {
        Log.e(TAG, "RuntimeException in SyncManager", e);
        throw e;
    } finally {
        log("Finishing SyncManager");
        // Lots of cleanup here
        // Stop our running syncs
        stopServiceThreads();

        // Stop receivers and content observers
        if (mConnectivityReceiver != null) {
            unregisterReceiver(mConnectivityReceiver);
        }
        if (mBackgroundDataSettingReceiver != null) {
            unregisterReceiver(mBackgroundDataSettingReceiver);
        }

        if (INSTANCE != null) {
            ContentResolver resolver = getContentResolver();
            resolver.unregisterContentObserver(mAccountObserver);
            resolver.unregisterContentObserver(mMailboxObserver);
            resolver.unregisterContentObserver(mSyncedMessageObserver);
            resolver.unregisterContentObserver(mMessageObserver);
            unregisterCalendarObservers();
        }
        // Don't leak the Intent associated with this listener
        if (mAccountsUpdatedListener != null) {
            AccountManager.get(this).removeOnAccountsUpdatedListener(mAccountsUpdatedListener);
            mAccountsUpdatedListener = null;
        }

        // Clear pending alarms and associated Intents
        clearAlarms();

        // Release our wake lock, if we have one
        synchronized (mWakeLocks) {
            if (mWakeLock != null) {
                mWakeLock.release();
                mWakeLock = null;
            }
        }

        log("Goodbye");
    }

    if (!mStop) {
        // If this wasn't intentional, try to restart the service
        throw new RuntimeException("EAS SyncManager crash; please restart me...");
    }
}

From source file:com.android.exchange.ExchangeService.java

public void run() {
    sStop = false;//  w  w  w . j a  v a2 s. c o  m
    alwaysLog("ExchangeService thread running");
    // If we're really debugging, turn on all logging
    if (Eas.DEBUG) {
        Eas.USER_LOG = true;
        Eas.PARSER_LOG = true;
        Eas.FILE_LOG = true;
    }

    TempDirectory.setTempDirectory(this);

    // If we need to wait for the debugger, do so
    if (Eas.WAIT_DEBUG) {
        Debug.waitForDebugger();
    }

    // Synchronize here to prevent a shutdown from happening while we initialize our observers
    // and receivers
    synchronized (sSyncLock) {
        if (INSTANCE != null) {
            mResolver = getContentResolver();

            // Set up our observers; we need them to know when to start/stop various syncs based
            // on the insert/delete/update of mailboxes and accounts
            // We also observe synced messages to trigger upsyncs at the appropriate time
            mAccountObserver = new AccountObserver(mHandler);
            mResolver.registerContentObserver(Account.NOTIFIER_URI, true, mAccountObserver);
            mMailboxObserver = new MailboxObserver(mHandler);
            mResolver.registerContentObserver(Mailbox.CONTENT_URI, false, mMailboxObserver);
            mSyncedMessageObserver = new SyncedMessageObserver(mHandler);
            mResolver.registerContentObserver(Message.SYNCED_CONTENT_URI, true, mSyncedMessageObserver);

            // Set up receivers for connectivity and background data setting
            mConnectivityReceiver = new ConnectivityReceiver();
            registerReceiver(mConnectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

            mBackgroundDataSettingReceiver = new ConnectivityReceiver();
            registerReceiver(mBackgroundDataSettingReceiver,
                    new IntentFilter(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED));
            // Save away the current background data setting; we'll keep track of it with the
            // receiver we just registered
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            mBackgroundData = cm.getBackgroundDataSetting();

            // Do any required work to clean up our Mailboxes (this serves to upgrade
            // mailboxes that existed prior to EmailProvider database version 17)
            MailboxUtilities.fixupUninitializedParentKeys(this, getEasAccountSelector());
        }
    }

    try {
        // Loop indefinitely until we're shut down
        while (!sStop) {
            runAwake(EXTRA_MAILBOX_ID);
            waitForConnectivity();
            mNextWaitReason = null;
            long nextWait = checkMailboxes();
            try {
                synchronized (this) {
                    if (!mKicked) {
                        if (nextWait < 0) {
                            log("Negative wait? Setting to 1s");
                            nextWait = 1 * SECONDS;
                        }
                        if (nextWait > 10 * SECONDS) {
                            if (mNextWaitReason != null) {
                                log("Next awake " + nextWait / 1000 + "s: " + mNextWaitReason);
                            }
                            runAsleep(EXTRA_MAILBOX_ID, nextWait + (3 * SECONDS));
                        }
                        wait(nextWait);
                    }
                }
            } catch (InterruptedException e) {
                // Needs to be caught, but causes no problem
                log("ExchangeService interrupted");
            } finally {
                synchronized (this) {
                    if (mKicked) {
                        //log("Wait deferred due to kick");
                        mKicked = false;
                    }
                }
            }
        }
        log("Shutdown requested");
    } catch (ProviderUnavailableException pue) {
        // Shutdown cleanly in this case
        // NOTE: Sync adapters will also crash with this error, but that is already handled
        // in the adapters themselves, i.e. they return cleanly via done().  When the Email
        // process starts running again, the Exchange process will be started again in due
        // course, assuming there is at least one existing EAS account.
        Log.e(TAG, "EmailProvider unavailable; shutting down");
        // Ask for our service to be restarted; this should kick-start the Email process as well
        startService(new Intent(this, ExchangeService.class));
    } catch (RuntimeException e) {
        // Crash; this is a completely unexpected runtime error
        Log.e(TAG, "RuntimeException in ExchangeService", e);
        throw e;
    } finally {
        shutdown();
    }
}

From source file:org.brandroid.openmanager.activities.OpenExplorer.java

public boolean isNetworkConnected() {
    ConnectivityManager conman = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (!conman.getBackgroundDataSetting())
        return false;
    NetworkInfo ni = conman.getActiveNetworkInfo();
    if (ni == null)
        return false;
    if (!ni.isAvailable() || !ni.isConnected())
        return false;
    if (ni.getState() == State.CONNECTING)
        return false;
    return true;//from   w  w  w.  j a  va2  s .c  om
}