Add local logs to TelephonyBackupAgent.
These will give some insight into what happened on the last
restore attempt which should be helpful in case of SMS restore
related issues. This info will be available even after device
is rebooted.
Test: manual
Bug: 164211717
Change-Id: Ia37f3020953dc3bca6d59deac52069a14cdd78e5
diff --git a/src/com/android/providers/telephony/TelephonyBackupAgent.java b/src/com/android/providers/telephony/TelephonyBackupAgent.java
index 6032f5c..34fed99 100644
--- a/src/com/android/providers/telephony/TelephonyBackupAgent.java
+++ b/src/com/android/providers/telephony/TelephonyBackupAgent.java
@@ -36,6 +36,7 @@
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.os.PowerManager;
+import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.provider.Telephony;
import android.telephony.PhoneNumberUtils;
@@ -50,6 +51,7 @@
import android.util.SparseArray;
import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.PhoneFactory;
import com.google.android.mms.ContentType;
import com.google.android.mms.pdu.CharacterSets;
@@ -114,6 +116,15 @@
private static final boolean DEBUG = false;
private static volatile boolean sIsRestoring;
+ // SharedPreferences keys
+ private static final String NUM_SMS_RESTORED = "num_sms_restored";
+ private static final String NUM_SMS_EXCEPTIONS = "num_sms_exceptions";
+ private static final String NUM_SMS_FILES_STORED = "num_sms_files_restored";
+ private static final String NUM_SMS_FILES_WITH_EXCEPTIONS = "num_sms_files_with_exceptions";
+ private static final String NUM_MMS_RESTORED = "num_mms_restored";
+ private static final String NUM_MMS_EXCEPTIONS = "num_mms_exceptions";
+ private static final String NUM_MMS_FILES_STORED = "num_mms_files_restored";
+ private static final String NUM_MMS_FILES_WITH_EXCEPTIONS = "num_mms_files_with_exceptions";
// Copied from packages/apps/Messaging/src/com/android/messaging/sms/MmsUtils.java.
private static final int DEFAULT_DURATION = 5000; //ms
@@ -316,7 +327,7 @@
@Override
public void onCreate() {
super.onCreate();
-
+ Log.d(TAG, "onCreate");
final SubscriptionManager subscriptionManager = SubscriptionManager.from(this);
if (subscriptionManager != null) {
final List<SubscriptionInfo> subInfo =
@@ -505,6 +516,28 @@
public static class DeferredSmsMmsRestoreService extends IntentService {
private static final String TAG = "DeferredSmsMmsRestoreService";
+ private static boolean sSharedPrefsAddedToLocalLogs = false;
+
+ public static void addAllSharedPrefToLocalLog(Context context) {
+ if (sSharedPrefsAddedToLocalLogs) return;
+ localLog("addAllSharedPrefToLocalLog");
+ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
+ Map<String, ?> allPref = sp.getAll();
+ if (allPref.keySet() == null || allPref.keySet().size() == 0) return;
+ for (String key : allPref.keySet()) {
+ try {
+ localLog(key + ":" + allPref.get(key).toString());
+ } catch (Exception e) {
+ localLog("Skipping over key " + key + " due to exception " + e);
+ }
+ }
+ sSharedPrefsAddedToLocalLogs = true;
+ }
+
+ public static void localLog(String logMsg) {
+ Log.d(TAG, logMsg);
+ PhoneFactory.localLog(TAG, logMsg);
+ }
private final Comparator<File> mFileComparator = new Comparator<File>() {
@Override
@@ -515,6 +548,7 @@
public DeferredSmsMmsRestoreService() {
super(TAG);
+ Log.d(TAG, "DeferredSmsMmsRestoreService");
setIntentRedelivery(true);
}
@@ -523,6 +557,7 @@
@Override
protected void onHandleIntent(Intent intent) {
+ Log.d(TAG, "onHandleIntent");
try {
mWakeLock.acquire();
sIsRestoring = true;
@@ -545,6 +580,7 @@
} catch (Exception e) {
// Either IOException or RuntimeException.
Log.e(TAG, "onHandleIntent", e);
+ localLog("onHandleIntent: Exception " + e);
} finally {
file.delete();
}
@@ -552,11 +588,12 @@
if (didRestore) {
// Tell the default sms app to do a full sync now that the messages have been
// restored.
- Log.d(TAG, "onHandleIntent done - notifying default sms app");
+ localLog("onHandleIntent: done - notifying default sms app");
ProviderUtil.notifyIfNotDefaultSmsApp(null /*uri*/, null /*calling package*/,
this);
}
} finally {
+ addAllSharedPrefToLocalLog(this);
sIsRestoring = false;
mWakeLock.release();
}
@@ -565,6 +602,12 @@
@Override
public void onCreate() {
super.onCreate();
+ Log.d(TAG, "onCreate");
+ try {
+ PhoneFactory.addLocalLog(TAG, 32);
+ } catch (IllegalArgumentException e) {
+ // ignore
+ }
mTelephonyBackupAgent = new TelephonyBackupAgent();
mTelephonyBackupAgent.attach(this);
mTelephonyBackupAgent.onCreate();
@@ -583,8 +626,15 @@
}
static void startIfFilesExist(Context context) {
+ try {
+ PhoneFactory.addLocalLog(TAG, 32);
+ } catch (IllegalArgumentException e) {
+ // ignore
+ }
File[] files = getFilesToRestore(context);
if (files == null || files.length == 0) {
+ Log.d(TAG, "startIfFilesExist: no files to restore");
+ addAllSharedPrefToLocalLog(context);
return;
}
context.startService(new Intent(context, DeferredSmsMmsRestoreService.class));
@@ -618,7 +668,7 @@
Log.d(TAG, "Restoring text MMS");
putMmsMessagesToProvider(jsonReader);
} else {
- Log.e(TAG, "Unknown file to restore:" + fileName);
+ DeferredSmsMmsRestoreService.localLog("Unknown file to restore:" + fileName);
}
}
}
@@ -627,6 +677,7 @@
void putSmsMessagesToProvider(JsonReader jsonReader) throws IOException {
jsonReader.beginArray();
int msgCount = 0;
+ int numExceptions = 0;
final int bulkInsertSize = mMaxMsgPerFile;
ContentValues[] values = new ContentValues[bulkInsertSize];
while (jsonReader.hasNext()) {
@@ -641,6 +692,8 @@
}
} catch (RuntimeException e) {
Log.e(TAG, "putSmsMessagesToProvider", e);
+ DeferredSmsMmsRestoreService.localLog("putSmsMessagesToProvider: Exception " + e);
+ numExceptions++;
}
}
if (msgCount % bulkInsertSize > 0) {
@@ -648,12 +701,37 @@
Arrays.copyOf(values, msgCount % bulkInsertSize));
}
jsonReader.endArray();
+ incremenentSharedPref(true, msgCount, numExceptions);
+ }
+
+ void incremenentSharedPref(boolean sms, int msgCount, int numExceptions) {
+ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
+ SharedPreferences.Editor editor = sp.edit();
+ if (sms) {
+ editor.putInt(NUM_SMS_RESTORED, sp.getInt(NUM_SMS_RESTORED, 0) + msgCount);
+ editor.putInt(NUM_SMS_EXCEPTIONS, sp.getInt(NUM_SMS_EXCEPTIONS, 0) + numExceptions);
+ editor.putInt(NUM_SMS_FILES_STORED, sp.getInt(NUM_SMS_FILES_STORED, 0) + 1);
+ if (numExceptions > 0) {
+ editor.putInt(NUM_SMS_FILES_WITH_EXCEPTIONS,
+ sp.getInt(NUM_SMS_FILES_WITH_EXCEPTIONS, 0) + 1);
+ }
+ } else {
+ editor.putInt(NUM_MMS_RESTORED, sp.getInt(NUM_MMS_RESTORED, 0) + msgCount);
+ editor.putInt(NUM_MMS_EXCEPTIONS, sp.getInt(NUM_MMS_EXCEPTIONS, 0) + numExceptions);
+ editor.putInt(NUM_MMS_FILES_STORED, sp.getInt(NUM_MMS_FILES_STORED, 0) + 1);
+ if (numExceptions > 0) {
+ editor.putInt(NUM_MMS_FILES_WITH_EXCEPTIONS,
+ sp.getInt(NUM_MMS_FILES_WITH_EXCEPTIONS, 0) + 1);
+ }
+ }
+ editor.commit();
}
@VisibleForTesting
void putMmsMessagesToProvider(JsonReader jsonReader) throws IOException {
jsonReader.beginArray();
int total = 0;
+ int numExceptions = 0;
while (jsonReader.hasNext()) {
final Mms mms = readMmsFromReader(jsonReader);
if (DEBUG) {
@@ -672,9 +750,12 @@
addMmsMessage(mms);
} catch (Exception e) {
Log.e(TAG, "putMmsMessagesToProvider", e);
+ numExceptions++;
+ DeferredSmsMmsRestoreService.localLog("putMmsMessagesToProvider: Exception " + e);
}
}
Log.d(TAG, "putMmsMessagesToProvider handled " + total + " new messages.");
+ incremenentSharedPref(false, total, numExceptions);
}
@VisibleForTesting