Example usage for android.content ContentResolver addStatusChangeListener

List of usage examples for android.content ContentResolver addStatusChangeListener

Introduction

In this page you can find the example usage for android.content ContentResolver addStatusChangeListener.

Prototype

public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) 

Source Link

Document

Request notifications when the different aspects of the SyncManager change.

Usage

From source file:de.sindzinski.wetter.ForecastHourlyFragment.java

@Override
public void onResume() {
    super.onResume();
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    sp.registerOnSharedPreferenceChangeListener(this);

    syncObserverHandle = ContentResolver.addStatusChangeListener(
            ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE,
            new ForecastHourlyFragment.MySyncStatusObserver());

    //only display furure hourly items
    String locationSetting = Utility.getPreferredLocation(getActivity());
    WetterSyncAdapter.deleteOldWeatherData(getContext(), Utility.getLocationId(getContext(), locationSetting),
            TYPE_HOURLY, 0);//from   ww w . j  a  va2s . c  o m
    WetterSyncAdapter.deleteOldWeatherData(getContext(), Utility.getLocationId(getContext(), locationSetting),
            TYPE_CURRENT, -1);

}

From source file:com.android.contacts.model.AccountTypeManager.java

/**
 * Internal constructor that only performs initial parsing.
 */// w w  w.  j a v  a2 s .  c o  m
public AccountTypeManagerImpl(Context context) {
    mContext = context;
    mLocalAccountLocator = DeviceLocalAccountLocator.create(context);
    mTypeProvider = new AccountTypeProvider(context);
    mFallbackAccountType = new FallbackAccountType(context);

    mAccountManager = AccountManager.get(mContext);

    mExecutor = ContactsExecutors.getDefaultThreadPoolExecutor();
    mMainThreadExecutor = ContactsExecutors.newHandlerExecutor(mMainThreadHandler);

    // Request updates when packages or accounts change
    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    mContext.registerReceiver(mBroadcastReceiver, filter);
    IntentFilter sdFilter = new IntentFilter();
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
    mContext.registerReceiver(mBroadcastReceiver, sdFilter);

    // Request updates when locale is changed so that the order of each field will
    // be able to be changed on the locale change.
    filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
    mContext.registerReceiver(mBroadcastReceiver, filter);

    mAccountManager.addOnAccountsUpdatedListener(this, mMainThreadHandler, false);

    ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, this);

    if (Flags.getInstance().getBoolean(Experiments.CP2_DEVICE_ACCOUNT_DETECTION_ENABLED)) {
        // Observe changes to RAW_CONTACTS so that we will update the list of "Device" accounts
        // if a new device contact is added.
        mContext.getContentResolver().registerContentObserver(ContactsContract.RawContacts.CONTENT_URI,
                /* notifyDescendents */ true, new ContentObserver(mMainThreadHandler) {
                    @Override
                    public boolean deliverSelfNotifications() {
                        return true;
                    }

                    @Override
                    public void onChange(boolean selfChange) {
                        reloadLocalAccounts();
                    }

                    @Override
                    public void onChange(boolean selfChange, Uri uri) {
                        reloadLocalAccounts();
                    }
                });
    }
    loadAccountTypes();
}

From source file:com.nextgis.ngm_clink_monitoring.activities.MainActivity.java

@Override
protected void onResume() {
    super.onResume();

    if (getIntent().getBooleanExtra(FoclConstants.NO_SDCARD, false)) {
        return;//from  w ww  .  j a  va2s.  co m
    }

    if (!FoclLocationUtil.isOnlyGpsLocationModeEnabled(this)) {
        FoclLocationUtil.showSettingsLocationAlert(this);
    }

    GISApplication app = (GISApplication) getApplication();

    app.setOnAccountAddedListener(this);
    app.setOnAccountDeletedListener(this);
    app.setOnReloadMapListener(this);

    // Refresh synchronization status
    mSyncStatusObserver.onStatusChanged(0);

    // Watch for synchronization status changes
    final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
    mSyncHandle = ContentResolver.addStatusChangeListener(mask, mSyncStatusObserver);

    if (app.isAccountAdded() || app.isAccountDeleted()) {
        refreshActivityView();
    }

    if (app.isMapReloaded()) {
        if (VIEW_STATE_1ST_SYNC == mViewState) {
            mViewState = VIEW_STATE_OBJECTS;
            setActivityView();
        } else {
            refreshFragmentView();
        }
    }
}

From source file:com.google.samples.apps.sergio.ui.BaseActivity.java

@Override
protected void onResume() {
    super.onResume();

    // Verifies the proper version of Google Play Services exists on the device.
    PlayServicesUtils.checkGooglePlaySevices(this);

    // Watch for sync state changes
    mSyncStatusObserver.onStatusChanged(0);
    final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
    mSyncObserverHandle = ContentResolver.addStatusChangeListener(mask, mSyncStatusObserver);
}

From source file:xyz.template.material.menu.ui.BaseActivity.java

@Override
protected void onResume() {
    super.onResume();

    // Verifies the proper version of Google Play Services exists on the device.
    //        PlayServicesUtils.checkGooglePlaySevices(this);

    // Watch for sync state changes
    mSyncStatusObserver.onStatusChanged(0);
    final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
    mSyncObserverHandle = ContentResolver.addStatusChangeListener(mask, mSyncStatusObserver);
}

From source file:com.razza.apps.iosched.ui.BaseActivity.java

@Override
protected void onResume() {
    super.onResume();

    // Perform one-time bootstrap setup, if needed
    DataBootstrapService.startDataBootstrapIfNecessary(this);

    // Check to ensure a Google Account is active for the app. Placing the check here ensures
    // it is run again in the case where a Google Account wasn't present on the device and a
    // picker had to be started.
    if (!AccountUtils.enforceActiveGoogleAccount(this, SELECT_GOOGLE_ACCOUNT_RESULT)) {
        LogUtils.LOGD(TAG, "EnforceActiveGoogleAccount returned false");
        return;//w  w w  . j a  v  a  2  s  .  c o m
    }

    // Watch for sync state changes
    mSyncStatusObserver.onStatusChanged(0);
    final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
    mSyncObserverHandle = ContentResolver.addStatusChangeListener(mask, mSyncStatusObserver);

    startLoginProcess();
}

From source file:com.google.samples.apps.iosched.ui.BaseActivity.java

@Override
protected void onResume() {
    super.onResume();

    // Perform one-time bootstrap setup, if needed
    DataBootstrapService.startDataBootstrapIfNecessary(this);

    // Check to ensure a Google Account is active for the app. Placing the check here ensures
    // it is run again in the case where a Google Account wasn't present on the device and a
    // picker had to be started.
    if (!AccountUtils.enforceActiveGoogleAccount(this, SELECT_GOOGLE_ACCOUNT_RESULT)) {
        LOGD(TAG, "EnforceActiveGoogleAccount returned false");
        return;/*  w w  w  . ja  v  a  2 s .co  m*/
    }

    // Watch for sync state changes
    mSyncStatusObserver.onStatusChanged(0);
    final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
    mSyncObserverHandle = ContentResolver.addStatusChangeListener(mask, mSyncStatusObserver);

    startLoginProcess();
}

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

public void run() {
    sStop = false;/*from ww  w  .ja v  a 2 s.  co m*/
    alwaysLog("!!! SyncManager 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;
    }

    // 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.CONTENT_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);
            mMessageObserver = new MessageObserver(mHandler);
            mResolver.registerContentObserver(Message.CONTENT_URI, true, mMessageObserver);
            mSyncStatusObserver = new EasSyncStatusObserver();
            mStatusChangeListener = ContentResolver
                    .addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, mSyncStatusObserver);

            // Set up our observer for AccountManager
            mAccountsUpdatedListener = new EasAccountsUpdatedListener();
            AccountManager.get(getApplication()).addOnAccountsUpdatedListener(mAccountsUpdatedListener,
                    mHandler, true);

            // 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();

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

    try {
        // Loop indefinitely until we're shut down
        while (!sStop) {
            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
                log("SyncManager interrupted");
            } 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 {
        shutdown();
    }
}

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

public void run() {
    mStop = false;/* ww w.  j ava2 s .  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...");
    }
}