Merge "Code cleanup." into jb-ub-mail-ur9

This commit is contained in:
Yu Ping Hu 2013-04-30 21:29:09 +00:00 committed by Android (Google) Code Review
commit 99e66178b9
2 changed files with 40 additions and 99 deletions

View File

@ -226,8 +226,6 @@ public abstract class SyncManager extends Service implements Runnable {
// Receiver of connectivity broadcasts // Receiver of connectivity broadcasts
private ConnectivityReceiver mConnectivityReceiver = null; private ConnectivityReceiver mConnectivityReceiver = null;
private ConnectivityReceiver mBackgroundDataSettingReceiver = null;
private volatile boolean mBackgroundData = true;
// The most current NetworkInfo (from ConnectivityManager) // The most current NetworkInfo (from ConnectivityManager)
private NetworkInfo mNetworkInfo; private NetworkInfo mNetworkInfo;
@ -874,7 +872,7 @@ public abstract class SyncManager extends Service implements Runnable {
* This would work on a real device as well, but it would be better to use the "real" id if * This would work on a real device as well, but it would be better to use the "real" id if
* it's available * it's available
*/ */
static public String getDeviceId(Context context) throws IOException { static public String getDeviceId(Context context) {
if (sDeviceId == null) { if (sDeviceId == null) {
sDeviceId = new AccountServiceProxy(context).getDeviceId(); sDeviceId = new AccountServiceProxy(context).getDeviceId();
alwaysLog("Received deviceId from Email app: " + sDeviceId); alwaysLog("Received deviceId from Email app: " + sDeviceId);
@ -1180,45 +1178,22 @@ public abstract class SyncManager extends Service implements Runnable {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { Bundle b = intent.getExtras();
Bundle b = intent.getExtras(); if (b != null) {
if (b != null) { NetworkInfo a = (NetworkInfo)b.get(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo a = (NetworkInfo)b.get(ConnectivityManager.EXTRA_NETWORK_INFO); String info = "Connectivity alert for " + a.getTypeName();
String info = "Connectivity alert for " + a.getTypeName(); State state = a.getState();
State state = a.getState(); if (state == State.CONNECTED) {
if (state == State.CONNECTED) { info += " CONNECTED";
info += " CONNECTED"; log(info);
log(info); synchronized (sConnectivityLock) {
synchronized (sConnectivityLock) { sConnectivityLock.notifyAll();
sConnectivityLock.notifyAll();
}
kick("connected");
} else if (state == State.DISCONNECTED) {
info += " DISCONNECTED";
log(info);
kick("disconnected");
} }
} kick("connected");
} else if (intent.getAction().equals( } else if (state == State.DISCONNECTED) {
ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED)) { info += " DISCONNECTED";
ConnectivityManager cm = log(info);
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); kick("disconnected");
mBackgroundData = cm.getBackgroundDataSetting();
// If background data is now on, we want to kick SyncServiceManager
if (mBackgroundData) {
kick("background data on");
log("Background data on; restart syncs");
// Otherwise, stop all syncs
} else {
log("Background data off: stop all syncs");
EmailAsyncTask.runAsyncParallel(new Runnable() {
@Override
public void run() {
synchronized (mAccountList) {
for (Account account : mAccountList)
SyncManager.stopAccountSyncs(account.mId);
}
}});
} }
} }
} }
@ -1421,29 +1396,21 @@ public abstract class SyncManager extends Service implements Runnable {
alwaysLog("!!! Email application not found; stopping self"); alwaysLog("!!! Email application not found; stopping self");
stopSelf(); stopSelf();
} }
if (sDeviceId == null) { String deviceId = getDeviceId(SyncManager.this);
try { if (deviceId == null) {
String deviceId = getDeviceId(SyncManager.this); alwaysLog("!!! deviceId unknown; stopping self and retrying");
if (deviceId != null) { stopSelf();
sDeviceId = deviceId; // Try to restart ourselves in a few seconds
} Utility.runAsync(new Runnable() {
} catch (IOException e) { @Override
} public void run() {
if (sDeviceId == null) { try {
alwaysLog("!!! deviceId unknown; stopping self and retrying"); Thread.sleep(5000);
stopSelf(); } catch (InterruptedException e) {
// Try to restart ourselves in a few seconds }
Utility.runAsync(new Runnable() { startService(getServiceIntent());
@Override }});
public void run() { return;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
startService(getServiceIntent());
}});
return;
}
} }
// Run the reconciler and clean up mismatched accounts - if we weren't // Run the reconciler and clean up mismatched accounts - if we weren't
// running when accounts were deleted, it won't have been called. // running when accounts were deleted, it won't have been called.
@ -1556,20 +1523,10 @@ public abstract class SyncManager extends Service implements Runnable {
mResolver.registerContentObserver(Message.SYNCED_CONTENT_URI, true, mResolver.registerContentObserver(Message.SYNCED_CONTENT_URI, true,
mSyncedMessageObserver); mSyncedMessageObserver);
// Set up receivers for connectivity and background data setting
mConnectivityReceiver = new ConnectivityReceiver(); mConnectivityReceiver = new ConnectivityReceiver();
registerReceiver(mConnectivityReceiver, new IntentFilter( registerReceiver(mConnectivityReceiver, new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION)); 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();
onStartup(); onStartup();
} }
} }
@ -1640,9 +1597,6 @@ public abstract class SyncManager extends Service implements Runnable {
if (mConnectivityReceiver != null) { if (mConnectivityReceiver != null) {
unregisterReceiver(mConnectivityReceiver); unregisterReceiver(mConnectivityReceiver);
} }
if (mBackgroundDataSettingReceiver != null) {
unregisterReceiver(mBackgroundDataSettingReceiver);
}
// Unregister observers // Unregister observers
ContentResolver resolver = getContentResolver(); ContentResolver resolver = getContentResolver();
@ -1833,11 +1787,10 @@ public abstract class SyncManager extends Service implements Runnable {
// Never automatically sync trash // Never automatically sync trash
} else if (type == Mailbox.TYPE_TRASH) { } else if (type == Mailbox.TYPE_TRASH) {
return false; return false;
// For non-outbox, non-account mail, we do three checks: // For non-outbox, non-account mail, we do two checks:
// 1) are we restricted by policy (i.e. manual sync only), // 1) are we restricted by policy (i.e. manual sync only),
// 2) has the user checked the "Sync Email" box in Account Settings, and // 2) has the user checked the "Sync Email" box in Account Settings, and
// 3) does the user have the master "background data" box checked in Settings } else if (!canAutoSync(account) || !canSyncEmail(account.mAmAccount)) {
} else if (!canAutoSync(account) || !canSyncEmail(account.mAmAccount) || !mBackgroundData) {
return false; return false;
} }
return true; return true;

View File

@ -16,7 +16,6 @@
package com.android.email.service; package com.android.email.service;
import android.accounts.OperationCanceledException;
import android.app.Service; import android.app.Service;
import android.content.AbstractThreadedSyncAdapter; import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient; import android.content.ContentProviderClient;
@ -46,45 +45,34 @@ import java.util.ArrayList;
public class PopImapSyncAdapterService extends Service { public class PopImapSyncAdapterService extends Service {
private static final String TAG = "PopImapSyncService"; private static final String TAG = "PopImapSyncService";
private static SyncAdapterImpl sSyncAdapter = null; private SyncAdapterImpl mSyncAdapter = null;
private static final Object sSyncAdapterLock = new Object();
public PopImapSyncAdapterService() { public PopImapSyncAdapterService() {
super(); super();
} }
private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter { private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter {
private Context mContext;
public SyncAdapterImpl(Context context) { public SyncAdapterImpl(Context context) {
super(context, true /* autoInitialize */); super(context, true /* autoInitialize */);
mContext = context;
} }
@Override @Override
public void onPerformSync(android.accounts.Account account, Bundle extras, public void onPerformSync(android.accounts.Account account, Bundle extras,
String authority, ContentProviderClient provider, SyncResult syncResult) { String authority, ContentProviderClient provider, SyncResult syncResult) {
try { PopImapSyncAdapterService.performSync(getContext(), account, extras, authority,
PopImapSyncAdapterService.performSync(mContext, account, extras, provider, syncResult);
authority, provider, syncResult);
} catch (OperationCanceledException e) {
}
} }
} }
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
synchronized (sSyncAdapterLock) { mSyncAdapter = new SyncAdapterImpl(getApplicationContext());
if (sSyncAdapter == null) {
sSyncAdapter = new SyncAdapterImpl(getApplicationContext());
}
}
} }
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
return sSyncAdapter.getSyncAdapterBinder(); return mSyncAdapter.getSyncAdapterBinder();
} }
/** /**
@ -168,8 +156,8 @@ public class PopImapSyncAdapterService extends Service {
* Partial integration with system SyncManager; we initiate manual syncs upon request * Partial integration with system SyncManager; we initiate manual syncs upon request
*/ */
private static void performSync(Context context, android.accounts.Account account, private static void performSync(Context context, android.accounts.Account account,
Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) Bundle extras, String authority, ContentProviderClient provider,
throws OperationCanceledException { SyncResult syncResult) {
// Find an EmailProvider account with the Account's email address // Find an EmailProvider account with the Account's email address
Cursor c = null; Cursor c = null;
try { try {