2010-05-28 01:29:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.service;
|
|
|
|
|
|
|
|
import com.android.email.Email;
|
|
|
|
import com.android.email.ExchangeUtils;
|
|
|
|
import com.android.email.Preferences;
|
2010-12-30 08:16:55 +00:00
|
|
|
import com.android.email.SecurityPolicy;
|
2010-05-28 01:29:35 +00:00
|
|
|
import com.android.email.VendorPolicyLoader;
|
2011-05-13 00:27:56 +00:00
|
|
|
import com.android.email.activity.setup.AccountSettings;
|
2011-02-22 22:17:37 +00:00
|
|
|
import com.android.email.mail.Store;
|
2011-02-08 01:23:05 +00:00
|
|
|
import com.android.email.widget.WidgetManager;
|
2011-02-11 23:05:17 +00:00
|
|
|
import com.android.emailcommon.Logging;
|
2011-06-13 22:32:27 +00:00
|
|
|
import com.android.emailcommon.provider.Account;
|
2011-02-22 22:17:37 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent.AccountColumns;
|
2011-05-19 22:18:12 +00:00
|
|
|
import com.android.emailcommon.provider.HostAuth;
|
2010-05-28 01:29:35 +00:00
|
|
|
|
2011-01-14 20:00:17 +00:00
|
|
|
import android.accounts.AccountManager;
|
2010-05-28 01:29:35 +00:00
|
|
|
import android.app.IntentService;
|
|
|
|
import android.content.ComponentName;
|
2011-02-22 22:17:37 +00:00
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentValues;
|
2010-05-28 01:29:35 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
2011-02-22 22:17:37 +00:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2010-05-28 01:29:35 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The service that really handles broadcast intents on a worker thread.
|
|
|
|
*
|
|
|
|
* We make it a service, because:
|
|
|
|
* <ul>
|
|
|
|
* <li>So that it's less likely for the process to get killed.
|
|
|
|
* <li>Even if it does, the Intent that have started it will be re-delivered by the system,
|
|
|
|
* and we can start the process again. (Using {@link #setIntentRedelivery}).
|
|
|
|
* </ul>
|
2010-12-30 08:16:55 +00:00
|
|
|
*
|
|
|
|
* This also handles the DeviceAdminReceiver in SecurityPolicy, because it is also
|
|
|
|
* a BroadcastReceiver and requires the same processing semantics.
|
2010-05-28 01:29:35 +00:00
|
|
|
*/
|
|
|
|
public class EmailBroadcastProcessorService extends IntentService {
|
2010-12-30 08:16:55 +00:00
|
|
|
// Action used for BroadcastReceiver entry point
|
|
|
|
private static final String ACTION_BROADCAST = "broadcast_receiver";
|
|
|
|
|
2010-06-30 22:43:12 +00:00
|
|
|
// Dialing "*#*#36245#*#*" to open the debug screen. "36245" = "email"
|
2010-12-30 08:16:55 +00:00
|
|
|
private static final String ACTION_SECRET_CODE = "android.provider.Telephony.SECRET_CODE";
|
2010-06-30 22:43:12 +00:00
|
|
|
private static final String SECRET_CODE_HOST_DEBUG_SCREEN = "36245";
|
|
|
|
|
2010-12-30 08:16:55 +00:00
|
|
|
// This is a helper used to process DeviceAdminReceiver messages
|
|
|
|
private static final String ACTION_DEVICE_POLICY_ADMIN = "com.android.email.devicepolicy";
|
|
|
|
private static final String EXTRA_DEVICE_POLICY_ADMIN = "message_code";
|
|
|
|
|
2010-05-28 01:29:35 +00:00
|
|
|
public EmailBroadcastProcessorService() {
|
|
|
|
// Class name will be the thread name.
|
|
|
|
super(EmailBroadcastProcessorService.class.getName());
|
|
|
|
|
|
|
|
// Intent should be redelivered if the process gets killed before completing the job.
|
|
|
|
setIntentRedelivery(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point for {@link EmailBroadcastReceiver}.
|
|
|
|
*/
|
|
|
|
public static void processBroadcastIntent(Context context, Intent broadcastIntent) {
|
|
|
|
Intent i = new Intent(context, EmailBroadcastProcessorService.class);
|
2010-12-30 08:16:55 +00:00
|
|
|
i.setAction(ACTION_BROADCAST);
|
2010-05-28 01:29:35 +00:00
|
|
|
i.putExtra(Intent.EXTRA_INTENT, broadcastIntent);
|
|
|
|
context.startService(i);
|
|
|
|
}
|
|
|
|
|
2010-12-30 08:16:55 +00:00
|
|
|
/**
|
|
|
|
* Entry point for {@link com.android.email.SecurityPolicy.PolicyAdmin}. These will
|
|
|
|
* simply callback to {@link
|
|
|
|
* com.android.email.SecurityPolicy#onDeviceAdminReceiverMessage(Context, int)}.
|
|
|
|
*/
|
|
|
|
public static void processDevicePolicyMessage(Context context, int message) {
|
|
|
|
Intent i = new Intent(context, EmailBroadcastProcessorService.class);
|
|
|
|
i.setAction(ACTION_DEVICE_POLICY_ADMIN);
|
|
|
|
i.putExtra(EXTRA_DEVICE_POLICY_ADMIN, message);
|
|
|
|
context.startService(i);
|
|
|
|
}
|
|
|
|
|
2010-05-28 01:29:35 +00:00
|
|
|
@Override
|
|
|
|
protected void onHandleIntent(Intent intent) {
|
|
|
|
// This method is called on a worker thread.
|
|
|
|
|
2010-12-30 08:16:55 +00:00
|
|
|
// Dispatch from entry point
|
|
|
|
final String action = intent.getAction();
|
|
|
|
if (ACTION_BROADCAST.equals(action)) {
|
|
|
|
final Intent broadcastIntent = intent.getParcelableExtra(Intent.EXTRA_INTENT);
|
|
|
|
final String broadcastAction = broadcastIntent.getAction();
|
2010-05-28 01:29:35 +00:00
|
|
|
|
2010-12-30 08:16:55 +00:00
|
|
|
if (Intent.ACTION_BOOT_COMPLETED.equals(broadcastAction)) {
|
|
|
|
onBootCompleted();
|
2010-05-28 01:29:35 +00:00
|
|
|
|
2010-12-30 08:16:55 +00:00
|
|
|
// TODO: Do a better job when we get ACTION_DEVICE_STORAGE_LOW.
|
|
|
|
// The code below came from very old code....
|
|
|
|
} else if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(broadcastAction)) {
|
|
|
|
// Stop IMAP/POP3 poll.
|
|
|
|
MailService.actionCancel(this);
|
|
|
|
} else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(broadcastAction)) {
|
|
|
|
enableComponentsIfNecessary();
|
|
|
|
} else if (ACTION_SECRET_CODE.equals(broadcastAction)
|
|
|
|
&& SECRET_CODE_HOST_DEBUG_SCREEN.equals(broadcastIntent.getData().getHost())) {
|
2011-05-13 00:27:56 +00:00
|
|
|
AccountSettings.actionSettingsWithDebug(this);
|
2011-01-14 20:00:17 +00:00
|
|
|
} else if (AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION.equals(broadcastAction)) {
|
|
|
|
onSystemAccountChanged();
|
2010-12-30 08:16:55 +00:00
|
|
|
}
|
|
|
|
} else if (ACTION_DEVICE_POLICY_ADMIN.equals(action)) {
|
|
|
|
int message = intent.getIntExtra(EXTRA_DEVICE_POLICY_ADMIN, -1);
|
|
|
|
SecurityPolicy.onDeviceAdminReceiverMessage(this, message);
|
2010-05-28 01:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enableComponentsIfNecessary() {
|
2010-12-23 21:19:55 +00:00
|
|
|
if (Email.setServicesEnabledSync(this)) {
|
2010-05-28 01:29:35 +00:00
|
|
|
// At least one account exists.
|
|
|
|
// TODO probably we should check if it's a POP/IMAP account.
|
|
|
|
MailService.actionReschedule(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles {@link Intent#ACTION_BOOT_COMPLETED}. Called on a worker thread.
|
|
|
|
*/
|
|
|
|
private void onBootCompleted() {
|
|
|
|
performOneTimeInitialization();
|
|
|
|
|
|
|
|
enableComponentsIfNecessary();
|
|
|
|
|
|
|
|
// Starts the service for Exchange, if supported.
|
|
|
|
ExchangeUtils.startExchangeService(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void performOneTimeInitialization() {
|
|
|
|
final Preferences pref = Preferences.getPreferences(this);
|
|
|
|
int progress = pref.getOneTimeInitializationProgress();
|
|
|
|
final int initialProgress = progress;
|
|
|
|
|
|
|
|
if (progress < 1) {
|
2011-02-11 23:05:17 +00:00
|
|
|
Log.i(Logging.LOG_TAG, "Onetime initialization: 1");
|
2010-05-28 01:29:35 +00:00
|
|
|
progress = 1;
|
|
|
|
if (VendorPolicyLoader.getInstance(this).useAlternateExchangeStrings()) {
|
|
|
|
setComponentEnabled(EasAuthenticatorServiceAlternate.class, true);
|
|
|
|
setComponentEnabled(EasAuthenticatorService.class, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExchangeUtils.enableEasCalendarSync(this);
|
|
|
|
}
|
|
|
|
|
2011-02-22 22:17:37 +00:00
|
|
|
if (progress < 2) {
|
|
|
|
Log.i(Logging.LOG_TAG, "Onetime initialization: 2");
|
|
|
|
progress = 2;
|
|
|
|
setImapDeletePolicy(this);
|
|
|
|
}
|
|
|
|
|
2010-05-28 01:29:35 +00:00
|
|
|
// Add your initialization steps here.
|
|
|
|
// Use "progress" to skip the initializations that's already done before.
|
|
|
|
// Using this preference also makes it safe when a user skips an upgrade. (i.e. upgrading
|
|
|
|
// version N to version N+2)
|
|
|
|
|
|
|
|
if (progress != initialProgress) {
|
|
|
|
pref.setOneTimeInitializationProgress(progress);
|
2011-02-11 23:05:17 +00:00
|
|
|
Log.i(Logging.LOG_TAG, "Onetime initialization: completed.");
|
2010-05-28 01:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-22 22:17:37 +00:00
|
|
|
/**
|
|
|
|
* Sets the delete policy to the correct value for all IMAP accounts. This will have no
|
|
|
|
* effect on either EAS or POP3 accounts.
|
|
|
|
*/
|
|
|
|
/*package*/ static void setImapDeletePolicy(Context context) {
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
Cursor c = resolver.query(Account.CONTENT_URI, Account.CONTENT_PROJECTION,
|
|
|
|
null, null, null);
|
|
|
|
try {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
long recvAuthKey = c.getLong(Account.CONTENT_HOST_AUTH_KEY_RECV_COLUMN);
|
|
|
|
HostAuth recvAuth = HostAuth.restoreHostAuthWithId(context, recvAuthKey);
|
|
|
|
if (Store.STORE_SCHEME_IMAP.equals(recvAuth.mProtocol)) {
|
|
|
|
int flags = c.getInt(Account.CONTENT_FLAGS_COLUMN);
|
|
|
|
flags &= ~Account.FLAGS_DELETE_POLICY_MASK;
|
|
|
|
flags |= Account.DELETE_POLICY_ON_DELETE << Account.FLAGS_DELETE_POLICY_SHIFT;
|
|
|
|
ContentValues cv = new ContentValues();
|
|
|
|
cv.put(AccountColumns.FLAGS, flags);
|
|
|
|
long accountId = c.getLong(Account.CONTENT_ID_COLUMN);
|
|
|
|
Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId);
|
|
|
|
resolver.update(uri, cv, null, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-28 01:29:35 +00:00
|
|
|
private void setComponentEnabled(Class<?> clazz, boolean enabled) {
|
|
|
|
final ComponentName c = new ComponentName(this, clazz.getName());
|
|
|
|
getPackageManager().setComponentEnabledSetting(c,
|
|
|
|
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
|
|
|
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
|
|
|
PackageManager.DONT_KILL_APP);
|
|
|
|
}
|
2011-01-14 20:00:17 +00:00
|
|
|
|
|
|
|
private void onSystemAccountChanged() {
|
2011-03-02 02:49:45 +00:00
|
|
|
Log.i(Logging.LOG_TAG, "System accounts updated.");
|
2011-01-14 20:00:17 +00:00
|
|
|
MailService.reconcilePopImapAccountsSync(this);
|
|
|
|
|
2011-03-02 02:49:45 +00:00
|
|
|
// If the exchange service wasn't already running, starting it will cause exchange account
|
|
|
|
// reconciliation to be performed. The service stops itself it there are no EAS accounts.
|
2011-01-14 20:00:17 +00:00
|
|
|
ExchangeUtils.startExchangeService(this);
|
|
|
|
}
|
2010-05-28 01:29:35 +00:00
|
|
|
}
|