Merge "Remove the development privelege level from SET_DISPLAY_OFFSET."
diff --git a/Android.mk b/Android.mk
index f28f803..5544ea4 100644
--- a/Android.mk
+++ b/Android.mk
@@ -216,6 +216,8 @@
 	core/java/android/hardware/location/IGeofenceHardwareMonitorCallback.aidl \
 	core/java/android/hardware/location/IContextHubCallback.aidl \
 	core/java/android/hardware/location/IContextHubService.aidl \
+	core/java/android/hardware/radio/IRadioService.aidl \
+	core/java/android/hardware/radio/ITuner.aidl \
 	core/java/android/hardware/soundtrigger/IRecognitionStatusCallback.aidl \
 	core/java/android/hardware/usb/IUsbManager.aidl \
 	core/java/android/net/ICaptivePortal.aidl \
@@ -673,6 +675,7 @@
 	frameworks/base/core/java/android/print/PrinterInfo.aidl \
 	frameworks/base/core/java/android/print/PrintJobId.aidl \
 	frameworks/base/core/java/android/printservice/recommendation/RecommendationInfo.aidl \
+	frameworks/base/core/java/android/hardware/radio/RadioManager.aidl \
 	frameworks/base/core/java/android/hardware/usb/UsbDevice.aidl \
 	frameworks/base/core/java/android/hardware/usb/UsbInterface.aidl \
 	frameworks/base/core/java/android/hardware/usb/UsbEndpoint.aidl \
@@ -844,7 +847,6 @@
 include libcore/Docs.mk
 
 non_base_dirs := \
-	../opt/telephony/src/java/android/provider \
 	../opt/telephony/src/java/android/telephony \
 	../opt/telephony/src/java/android/telephony/gsm \
 	../opt/net/voip/src/java/android/net/rtp \
diff --git a/api/current.txt b/api/current.txt
index 95084fc..2b2b608 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -1952,6 +1952,7 @@
     field public static final int no = 17039369; // 0x1040009
     field public static final int ok = 17039370; // 0x104000a
     field public static final int paste = 17039371; // 0x104000b
+    field public static final int paste_as_plain_text = 17039385; // 0x1040019
     field public static final int search_go = 17039372; // 0x104000c
     field public static final int selectAll = 17039373; // 0x104000d
     field public static final int selectTextMode = 17039382; // 0x1040016
@@ -6829,6 +6830,8 @@
   }
 
   public class JobParameters implements android.os.Parcelable {
+    method public void completeWork(android.app.job.JobWorkItem);
+    method public android.app.job.JobWorkItem dequeueWork();
     method public int describeContents();
     method public android.content.ClipData getClipData();
     method public int getClipGrantFlags();
@@ -6846,6 +6849,7 @@
     ctor public JobScheduler();
     method public abstract void cancel(int);
     method public abstract void cancelAll();
+    method public abstract int enqueue(android.app.job.JobInfo, android.app.job.JobWorkItem);
     method public abstract java.util.List<android.app.job.JobInfo> getAllPendingJobs();
     method public abstract android.app.job.JobInfo getPendingJob(int);
     method public abstract int schedule(android.app.job.JobInfo);
@@ -6862,6 +6866,15 @@
     field public static final java.lang.String PERMISSION_BIND = "android.permission.BIND_JOB_SERVICE";
   }
 
+  public final class JobWorkItem implements android.os.Parcelable {
+    ctor public JobWorkItem(android.content.Intent);
+    ctor public JobWorkItem(android.os.Parcel);
+    method public int describeContents();
+    method public android.content.Intent getIntent();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.job.JobWorkItem> CREATOR;
+  }
+
 }
 
 package android.app.usage {
@@ -14388,15 +14401,12 @@
     field public static final android.os.Parcelable.Creator<android.graphics.fonts.FontRequest> CREATOR;
   }
 
-  public final class FontVariationAxis implements android.os.Parcelable {
+  public final class FontVariationAxis {
     ctor public FontVariationAxis(java.lang.String, float) throws android.graphics.fonts.FontVariationAxis.InvalidFormatException;
-    method public int describeContents();
     method public static android.graphics.fonts.FontVariationAxis[] fromFontVariationSettings(java.lang.String) throws android.graphics.fonts.FontVariationAxis.InvalidFormatException;
     method public float getStyleValue();
     method public java.lang.String getTag();
     method public static java.lang.String toFontVariationSettings(android.graphics.fonts.FontVariationAxis[]);
-    method public void writeToParcel(android.os.Parcel, int);
-    field public static final android.os.Parcelable.Creator<android.graphics.fonts.FontVariationAxis> CREATOR;
   }
 
   public static class FontVariationAxis.InvalidFormatException extends java.lang.Exception {
@@ -23623,7 +23633,7 @@
 
   public static final class VolumeShaper.Configuration implements android.os.Parcelable {
     method public int describeContents();
-    method public double getDurationMs();
+    method public double getDurationMillis();
     method public int getInterpolatorType();
     method public static int getMaximumCurvePoints();
     method public float[] getTimes();
@@ -23649,7 +23659,7 @@
     method public android.media.VolumeShaper.Configuration.Builder scaleToEndVolume(float);
     method public android.media.VolumeShaper.Configuration.Builder scaleToStartVolume(float);
     method public android.media.VolumeShaper.Configuration.Builder setCurve(float[], float[]);
-    method public android.media.VolumeShaper.Configuration.Builder setDurationMs(double);
+    method public android.media.VolumeShaper.Configuration.Builder setDurationMillis(double);
     method public android.media.VolumeShaper.Configuration.Builder setInterpolatorType(int);
   }
 
@@ -24708,6 +24718,8 @@
     field public static final java.lang.String COLUMN_LONG_DESCRIPTION = "long_description";
     field public static final java.lang.String COLUMN_POSTER_ART_URI = "poster_art_uri";
     field public static final java.lang.String COLUMN_RECORDING_PROHIBITED = "recording_prohibited";
+    field public static final java.lang.String COLUMN_REVIEW_RATING = "review_rating";
+    field public static final java.lang.String COLUMN_REVIEW_RATING_STYLE = "review_rating_style";
     field public static final java.lang.String COLUMN_SEARCHABLE = "searchable";
     field public static final java.lang.String COLUMN_SEASON_DISPLAY_NUMBER = "season_display_number";
     field public static final deprecated java.lang.String COLUMN_SEASON_NUMBER = "season_number";
@@ -24722,6 +24734,9 @@
     field public static final java.lang.String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/program";
     field public static final java.lang.String CONTENT_TYPE = "vnd.android.cursor.dir/program";
     field public static final android.net.Uri CONTENT_URI;
+    field public static final int REVIEW_RATING_STYLE_PERCENTAGE = 2; // 0x2
+    field public static final int REVIEW_RATING_STYLE_STARS = 0; // 0x0
+    field public static final int REVIEW_RATING_STYLE_THUMBS_UP_DOWN = 1; // 0x1
   }
 
   public static final class TvContract.Programs.Genres {
@@ -24768,6 +24783,8 @@
     field public static final java.lang.String COLUMN_RECORDING_DATA_URI = "recording_data_uri";
     field public static final java.lang.String COLUMN_RECORDING_DURATION_MILLIS = "recording_duration_millis";
     field public static final java.lang.String COLUMN_RECORDING_EXPIRE_TIME_UTC_MILLIS = "recording_expire_time_utc_millis";
+    field public static final java.lang.String COLUMN_REVIEW_RATING = "review_rating";
+    field public static final java.lang.String COLUMN_REVIEW_RATING_STYLE = "review_rating_style";
     field public static final java.lang.String COLUMN_SEARCHABLE = "searchable";
     field public static final java.lang.String COLUMN_SEASON_DISPLAY_NUMBER = "season_display_number";
     field public static final java.lang.String COLUMN_SEASON_TITLE = "season_title";
@@ -24781,6 +24798,9 @@
     field public static final java.lang.String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/recorded_program";
     field public static final java.lang.String CONTENT_TYPE = "vnd.android.cursor.dir/recorded_program";
     field public static final android.net.Uri CONTENT_URI;
+    field public static final int REVIEW_RATING_STYLE_PERCENTAGE = 2; // 0x2
+    field public static final int REVIEW_RATING_STYLE_STARS = 0; // 0x0
+    field public static final int REVIEW_RATING_STYLE_THUMBS_UP_DOWN = 1; // 0x1
   }
 
   public static final class TvContract.WatchNextPrograms implements android.media.tv.TvContract.BaseTvColumns {
@@ -31753,6 +31773,7 @@
     field public static final java.lang.String DISALLOW_APPS_CONTROL = "no_control_apps";
     field public static final java.lang.String DISALLOW_AUTOFILL = "no_autofill";
     field public static final java.lang.String DISALLOW_BLUETOOTH = "no_bluetooth";
+    field public static final java.lang.String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";
     field public static final java.lang.String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
     field public static final java.lang.String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
     field public static final java.lang.String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
@@ -34486,6 +34507,7 @@
 
   public static final class FontsContract.Columns implements android.provider.BaseColumns {
     ctor public FontsContract.Columns();
+    field public static final java.lang.String FILE_ID = "file_id";
     field public static final java.lang.String ITALIC = "font_italic";
     field public static final java.lang.String RESULT_CODE = "result_code";
     field public static final int RESULT_CODE_FONT_NOT_FOUND = 1; // 0x1
diff --git a/api/system-current.txt b/api/system-current.txt
index 9e8c9b35..a36de39 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -2074,6 +2074,7 @@
     field public static final int no = 17039369; // 0x1040009
     field public static final int ok = 17039370; // 0x104000a
     field public static final int paste = 17039371; // 0x104000b
+    field public static final int paste_as_plain_text = 17039385; // 0x1040019
     field public static final int search_go = 17039372; // 0x104000c
     field public static final int selectAll = 17039373; // 0x104000d
     field public static final int selectTextMode = 17039382; // 0x1040016
@@ -7259,6 +7260,8 @@
   }
 
   public class JobParameters implements android.os.Parcelable {
+    method public void completeWork(android.app.job.JobWorkItem);
+    method public android.app.job.JobWorkItem dequeueWork();
     method public int describeContents();
     method public android.content.ClipData getClipData();
     method public int getClipGrantFlags();
@@ -7276,6 +7279,7 @@
     ctor public JobScheduler();
     method public abstract void cancel(int);
     method public abstract void cancelAll();
+    method public abstract int enqueue(android.app.job.JobInfo, android.app.job.JobWorkItem);
     method public abstract java.util.List<android.app.job.JobInfo> getAllPendingJobs();
     method public abstract android.app.job.JobInfo getPendingJob(int);
     method public abstract int schedule(android.app.job.JobInfo);
@@ -7293,6 +7297,15 @@
     field public static final java.lang.String PERMISSION_BIND = "android.permission.BIND_JOB_SERVICE";
   }
 
+  public final class JobWorkItem implements android.os.Parcelable {
+    ctor public JobWorkItem(android.content.Intent);
+    ctor public JobWorkItem(android.os.Parcel);
+    method public int describeContents();
+    method public android.content.Intent getIntent();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.job.JobWorkItem> CREATOR;
+  }
+
 }
 
 package android.app.usage {
@@ -15154,15 +15167,12 @@
     field public static final android.os.Parcelable.Creator<android.graphics.fonts.FontRequest> CREATOR;
   }
 
-  public final class FontVariationAxis implements android.os.Parcelable {
+  public final class FontVariationAxis {
     ctor public FontVariationAxis(java.lang.String, float) throws android.graphics.fonts.FontVariationAxis.InvalidFormatException;
-    method public int describeContents();
     method public static android.graphics.fonts.FontVariationAxis[] fromFontVariationSettings(java.lang.String) throws android.graphics.fonts.FontVariationAxis.InvalidFormatException;
     method public float getStyleValue();
     method public java.lang.String getTag();
     method public static java.lang.String toFontVariationSettings(android.graphics.fonts.FontVariationAxis[]);
-    method public void writeToParcel(android.os.Parcel, int);
-    field public static final android.os.Parcelable.Creator<android.graphics.fonts.FontVariationAxis> CREATOR;
   }
 
   public static class FontVariationAxis.InvalidFormatException extends java.lang.Exception {
@@ -25462,7 +25472,7 @@
 
   public static final class VolumeShaper.Configuration implements android.os.Parcelable {
     method public int describeContents();
-    method public double getDurationMs();
+    method public double getDurationMillis();
     method public int getInterpolatorType();
     method public static int getMaximumCurvePoints();
     method public float[] getTimes();
@@ -25488,7 +25498,7 @@
     method public android.media.VolumeShaper.Configuration.Builder scaleToEndVolume(float);
     method public android.media.VolumeShaper.Configuration.Builder scaleToStartVolume(float);
     method public android.media.VolumeShaper.Configuration.Builder setCurve(float[], float[]);
-    method public android.media.VolumeShaper.Configuration.Builder setDurationMs(double);
+    method public android.media.VolumeShaper.Configuration.Builder setDurationMillis(double);
     method public android.media.VolumeShaper.Configuration.Builder setInterpolatorType(int);
   }
 
@@ -26688,6 +26698,8 @@
     field public static final java.lang.String COLUMN_LONG_DESCRIPTION = "long_description";
     field public static final java.lang.String COLUMN_POSTER_ART_URI = "poster_art_uri";
     field public static final java.lang.String COLUMN_RECORDING_PROHIBITED = "recording_prohibited";
+    field public static final java.lang.String COLUMN_REVIEW_RATING = "review_rating";
+    field public static final java.lang.String COLUMN_REVIEW_RATING_STYLE = "review_rating_style";
     field public static final java.lang.String COLUMN_SEARCHABLE = "searchable";
     field public static final java.lang.String COLUMN_SEASON_DISPLAY_NUMBER = "season_display_number";
     field public static final deprecated java.lang.String COLUMN_SEASON_NUMBER = "season_number";
@@ -26702,6 +26714,9 @@
     field public static final java.lang.String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/program";
     field public static final java.lang.String CONTENT_TYPE = "vnd.android.cursor.dir/program";
     field public static final android.net.Uri CONTENT_URI;
+    field public static final int REVIEW_RATING_STYLE_PERCENTAGE = 2; // 0x2
+    field public static final int REVIEW_RATING_STYLE_STARS = 0; // 0x0
+    field public static final int REVIEW_RATING_STYLE_THUMBS_UP_DOWN = 1; // 0x1
   }
 
   public static final class TvContract.Programs.Genres {
@@ -26748,6 +26763,8 @@
     field public static final java.lang.String COLUMN_RECORDING_DATA_URI = "recording_data_uri";
     field public static final java.lang.String COLUMN_RECORDING_DURATION_MILLIS = "recording_duration_millis";
     field public static final java.lang.String COLUMN_RECORDING_EXPIRE_TIME_UTC_MILLIS = "recording_expire_time_utc_millis";
+    field public static final java.lang.String COLUMN_REVIEW_RATING = "review_rating";
+    field public static final java.lang.String COLUMN_REVIEW_RATING_STYLE = "review_rating_style";
     field public static final java.lang.String COLUMN_SEARCHABLE = "searchable";
     field public static final java.lang.String COLUMN_SEASON_DISPLAY_NUMBER = "season_display_number";
     field public static final java.lang.String COLUMN_SEASON_TITLE = "season_title";
@@ -26761,6 +26778,9 @@
     field public static final java.lang.String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/recorded_program";
     field public static final java.lang.String CONTENT_TYPE = "vnd.android.cursor.dir/recorded_program";
     field public static final android.net.Uri CONTENT_URI;
+    field public static final int REVIEW_RATING_STYLE_PERCENTAGE = 2; // 0x2
+    field public static final int REVIEW_RATING_STYLE_STARS = 0; // 0x0
+    field public static final int REVIEW_RATING_STYLE_THUMBS_UP_DOWN = 1; // 0x1
   }
 
   public static final class TvContract.WatchNextPrograms implements android.media.tv.TvContract.BaseTvColumns {
@@ -34621,6 +34641,7 @@
     field public static final java.lang.String DISALLOW_APPS_CONTROL = "no_control_apps";
     field public static final java.lang.String DISALLOW_AUTOFILL = "no_autofill";
     field public static final java.lang.String DISALLOW_BLUETOOTH = "no_bluetooth";
+    field public static final java.lang.String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";
     field public static final java.lang.String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
     field public static final java.lang.String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
     field public static final java.lang.String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
@@ -37462,6 +37483,7 @@
 
   public static final class FontsContract.Columns implements android.provider.BaseColumns {
     ctor public FontsContract.Columns();
+    field public static final java.lang.String FILE_ID = "file_id";
     field public static final java.lang.String ITALIC = "font_italic";
     field public static final java.lang.String RESULT_CODE = "result_code";
     field public static final int RESULT_CODE_FONT_NOT_FOUND = 1; // 0x1
diff --git a/api/test-current.txt b/api/test-current.txt
index 28275fa..dbdaa36 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -1952,6 +1952,7 @@
     field public static final int no = 17039369; // 0x1040009
     field public static final int ok = 17039370; // 0x104000a
     field public static final int paste = 17039371; // 0x104000b
+    field public static final int paste_as_plain_text = 17039385; // 0x1040019
     field public static final int search_go = 17039372; // 0x104000c
     field public static final int selectAll = 17039373; // 0x104000d
     field public static final int selectTextMode = 17039382; // 0x1040016
@@ -6858,6 +6859,8 @@
   }
 
   public class JobParameters implements android.os.Parcelable {
+    method public void completeWork(android.app.job.JobWorkItem);
+    method public android.app.job.JobWorkItem dequeueWork();
     method public int describeContents();
     method public android.content.ClipData getClipData();
     method public int getClipGrantFlags();
@@ -6875,6 +6878,7 @@
     ctor public JobScheduler();
     method public abstract void cancel(int);
     method public abstract void cancelAll();
+    method public abstract int enqueue(android.app.job.JobInfo, android.app.job.JobWorkItem);
     method public abstract java.util.List<android.app.job.JobInfo> getAllPendingJobs();
     method public abstract android.app.job.JobInfo getPendingJob(int);
     method public abstract int schedule(android.app.job.JobInfo);
@@ -6891,6 +6895,15 @@
     field public static final java.lang.String PERMISSION_BIND = "android.permission.BIND_JOB_SERVICE";
   }
 
+  public final class JobWorkItem implements android.os.Parcelable {
+    ctor public JobWorkItem(android.content.Intent);
+    ctor public JobWorkItem(android.os.Parcel);
+    method public int describeContents();
+    method public android.content.Intent getIntent();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.job.JobWorkItem> CREATOR;
+  }
+
 }
 
 package android.app.usage {
@@ -14440,15 +14453,12 @@
     field public static final android.os.Parcelable.Creator<android.graphics.fonts.FontRequest> CREATOR;
   }
 
-  public final class FontVariationAxis implements android.os.Parcelable {
+  public final class FontVariationAxis {
     ctor public FontVariationAxis(java.lang.String, float) throws android.graphics.fonts.FontVariationAxis.InvalidFormatException;
-    method public int describeContents();
     method public static android.graphics.fonts.FontVariationAxis[] fromFontVariationSettings(java.lang.String) throws android.graphics.fonts.FontVariationAxis.InvalidFormatException;
     method public float getStyleValue();
     method public java.lang.String getTag();
     method public static java.lang.String toFontVariationSettings(android.graphics.fonts.FontVariationAxis[]);
-    method public void writeToParcel(android.os.Parcel, int);
-    field public static final android.os.Parcelable.Creator<android.graphics.fonts.FontVariationAxis> CREATOR;
   }
 
   public static class FontVariationAxis.InvalidFormatException extends java.lang.Exception {
@@ -23737,7 +23747,7 @@
 
   public static final class VolumeShaper.Configuration implements android.os.Parcelable {
     method public int describeContents();
-    method public double getDurationMs();
+    method public double getDurationMillis();
     method public int getInterpolatorType();
     method public static int getMaximumCurvePoints();
     method public float[] getTimes();
@@ -23763,7 +23773,7 @@
     method public android.media.VolumeShaper.Configuration.Builder scaleToEndVolume(float);
     method public android.media.VolumeShaper.Configuration.Builder scaleToStartVolume(float);
     method public android.media.VolumeShaper.Configuration.Builder setCurve(float[], float[]);
-    method public android.media.VolumeShaper.Configuration.Builder setDurationMs(double);
+    method public android.media.VolumeShaper.Configuration.Builder setDurationMillis(double);
     method public android.media.VolumeShaper.Configuration.Builder setInterpolatorType(int);
   }
 
@@ -24822,6 +24832,8 @@
     field public static final java.lang.String COLUMN_LONG_DESCRIPTION = "long_description";
     field public static final java.lang.String COLUMN_POSTER_ART_URI = "poster_art_uri";
     field public static final java.lang.String COLUMN_RECORDING_PROHIBITED = "recording_prohibited";
+    field public static final java.lang.String COLUMN_REVIEW_RATING = "review_rating";
+    field public static final java.lang.String COLUMN_REVIEW_RATING_STYLE = "review_rating_style";
     field public static final java.lang.String COLUMN_SEARCHABLE = "searchable";
     field public static final java.lang.String COLUMN_SEASON_DISPLAY_NUMBER = "season_display_number";
     field public static final deprecated java.lang.String COLUMN_SEASON_NUMBER = "season_number";
@@ -24836,6 +24848,9 @@
     field public static final java.lang.String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/program";
     field public static final java.lang.String CONTENT_TYPE = "vnd.android.cursor.dir/program";
     field public static final android.net.Uri CONTENT_URI;
+    field public static final int REVIEW_RATING_STYLE_PERCENTAGE = 2; // 0x2
+    field public static final int REVIEW_RATING_STYLE_STARS = 0; // 0x0
+    field public static final int REVIEW_RATING_STYLE_THUMBS_UP_DOWN = 1; // 0x1
   }
 
   public static final class TvContract.Programs.Genres {
@@ -24882,6 +24897,8 @@
     field public static final java.lang.String COLUMN_RECORDING_DATA_URI = "recording_data_uri";
     field public static final java.lang.String COLUMN_RECORDING_DURATION_MILLIS = "recording_duration_millis";
     field public static final java.lang.String COLUMN_RECORDING_EXPIRE_TIME_UTC_MILLIS = "recording_expire_time_utc_millis";
+    field public static final java.lang.String COLUMN_REVIEW_RATING = "review_rating";
+    field public static final java.lang.String COLUMN_REVIEW_RATING_STYLE = "review_rating_style";
     field public static final java.lang.String COLUMN_SEARCHABLE = "searchable";
     field public static final java.lang.String COLUMN_SEASON_DISPLAY_NUMBER = "season_display_number";
     field public static final java.lang.String COLUMN_SEASON_TITLE = "season_title";
@@ -24895,6 +24912,9 @@
     field public static final java.lang.String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/recorded_program";
     field public static final java.lang.String CONTENT_TYPE = "vnd.android.cursor.dir/recorded_program";
     field public static final android.net.Uri CONTENT_URI;
+    field public static final int REVIEW_RATING_STYLE_PERCENTAGE = 2; // 0x2
+    field public static final int REVIEW_RATING_STYLE_STARS = 0; // 0x0
+    field public static final int REVIEW_RATING_STYLE_THUMBS_UP_DOWN = 1; // 0x1
   }
 
   public static final class TvContract.WatchNextPrograms implements android.media.tv.TvContract.BaseTvColumns {
@@ -31892,6 +31912,7 @@
     field public static final java.lang.String DISALLOW_APPS_CONTROL = "no_control_apps";
     field public static final java.lang.String DISALLOW_AUTOFILL = "no_autofill";
     field public static final java.lang.String DISALLOW_BLUETOOTH = "no_bluetooth";
+    field public static final java.lang.String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";
     field public static final java.lang.String DISALLOW_CONFIG_BLUETOOTH = "no_config_bluetooth";
     field public static final java.lang.String DISALLOW_CONFIG_CELL_BROADCASTS = "no_config_cell_broadcasts";
     field public static final java.lang.String DISALLOW_CONFIG_CREDENTIALS = "no_config_credentials";
@@ -34628,6 +34649,7 @@
 
   public static final class FontsContract.Columns implements android.provider.BaseColumns {
     ctor public FontsContract.Columns();
+    field public static final java.lang.String FILE_ID = "file_id";
     field public static final java.lang.String ITALIC = "font_italic";
     field public static final java.lang.String RESULT_CODE = "result_code";
     field public static final int RESULT_CODE_FONT_NOT_FOUND = 1; // 0x1
diff --git a/cmds/bu/src/com/android/commands/bu/Backup.java b/cmds/bu/src/com/android/commands/bu/Backup.java
index ce114fd..345895b 100644
--- a/cmds/bu/src/com/android/commands/bu/Backup.java
+++ b/cmds/bu/src/com/android/commands/bu/Backup.java
@@ -57,7 +57,7 @@
         } else if (arg.equals("restore")) {
             doRestore(OsConstants.STDIN_FILENO);
         } else {
-            Log.e(TAG, "Invalid operation '" + arg + "'");
+            showUsage();
         }
     }
 
@@ -158,6 +158,21 @@
         }
     }
 
+    private static void showUsage() {
+        System.err.println(" backup [-f FILE] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all]");
+        System.err.println("        [-system|-nosystem] [-keyvalue|-nokeyvalue] [PACKAGE...]");
+        System.err.println("     write an archive of the device's data to FILE [default=backup.adb]");
+        System.err.println("     package list optional if -all/-shared are supplied");
+        System.err.println("     -apk/-noapk: do/don't back up .apk files (default -noapk)");
+        System.err.println("     -obb/-noobb: do/don't back up .obb files (default -noobb)");
+        System.err.println("     -shared|-noshared: do/don't back up shared storage (default -noshared)");
+        System.err.println("     -all: back up all installed applications");
+        System.err.println("     -system|-nosystem: include system apps in -all (default -system)");
+        System.err.println("     -keyvalue|-nokeyvalue: include apps that perform key/value backups.");
+        System.err.println("         (default -nokeyvalue)");
+        System.err.println(" restore FILE             restore device contents from FILE");
+    }
+
     private String nextArg() {
         if (mNextArg >= mArgs.length) {
             return null;
diff --git a/core/java/android/app/IActivityManager.aidl b/core/java/android/app/IActivityManager.aidl
index 079bbcd..79c2f1e 100644
--- a/core/java/android/app/IActivityManager.aidl
+++ b/core/java/android/app/IActivityManager.aidl
@@ -576,17 +576,6 @@
      * @param hasTopUi Whether the calling process has "top-level" UI.
      */
     void setHasTopUi(boolean hasTopUi);
-    /**
-     * Returns if the target of the PendingIntent can be fired directly, without triggering
-     * a work profile challenge. This can happen if the PendingIntent is to start direct-boot
-     * aware activities, and the target user is in RUNNING_LOCKED state, i.e. we should allow
-     * direct-boot aware activity to bypass work challenge when the user hasn't unlocked yet.
-     * @param intent the {@link  PendingIntent} to be tested.
-     * @return {@code true} if the intent should not trigger a work challenge, {@code false}
-     *     otherwise.
-     * @throws RemoteException
-     */
-    boolean canBypassWorkChallenge(in PendingIntent intent);
 
     // Start of O transactions
     void requestActivityRelaunch(in IBinder token);
diff --git a/core/java/android/app/JobSchedulerImpl.java b/core/java/android/app/JobSchedulerImpl.java
index e30b96f..4ac44f7 100644
--- a/core/java/android/app/JobSchedulerImpl.java
+++ b/core/java/android/app/JobSchedulerImpl.java
@@ -20,6 +20,8 @@
 import android.app.job.JobInfo;
 import android.app.job.JobScheduler;
 import android.app.job.IJobScheduler;
+import android.app.job.JobWorkItem;
+import android.content.Intent;
 import android.os.RemoteException;
 
 import java.util.List;
@@ -46,6 +48,15 @@
     }
 
     @Override
+    public int enqueue(JobInfo job, JobWorkItem work) {
+        try {
+            return mBinder.enqueue(job, work);
+        } catch (RemoteException e) {
+            return JobScheduler.RESULT_FAILURE;
+        }
+    }
+
+    @Override
     public int scheduleAsPackage(JobInfo job, String packageName, int userId, String tag) {
         try {
             return mBinder.scheduleAsPackage(job, packageName, userId, tag);
diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java
index c2c40df..abc50ae 100644
--- a/core/java/android/app/SystemServiceRegistry.java
+++ b/core/java/android/app/SystemServiceRegistry.java
@@ -784,7 +784,7 @@
         registerService(Context.RADIO_SERVICE, RadioManager.class,
                 new CachedServiceFetcher<RadioManager>() {
             @Override
-            public RadioManager createService(ContextImpl ctx) {
+            public RadioManager createService(ContextImpl ctx) throws ServiceNotFoundException {
                 return new RadioManager(ctx);
             }});
 
diff --git a/core/java/android/app/admin/SecurityLog.java b/core/java/android/app/admin/SecurityLog.java
index 790a952..2b590e0 100644
--- a/core/java/android/app/admin/SecurityLog.java
+++ b/core/java/android/app/admin/SecurityLog.java
@@ -27,6 +27,16 @@
 import java.lang.annotation.RetentionPolicy;
 import java.util.Collection;
 
+/**
+ * Definitions for working with security logs.
+ *
+ * <p>Device owner apps can control the logging with
+ * {@link DevicePolicyManager#setSecurityLoggingEnabled}. When security logs are enabled, device
+ * owner apps receive periodic callbacks from {@link DeviceAdminReceiver#onSecurityLogsAvailable},
+ * at which time new batch of logs can be collected via
+ * {@link DevicePolicyManager#retrieveSecurityLogs}. {@link SecurityEvent} describes the type and
+ * format of security logs being collected.
+ */
 public class SecurityLog {
 
     private static final String PROPERTY_LOGGING_ENABLED = "persist.logd.security";
diff --git a/core/java/android/app/job/IJobCallback.aidl b/core/java/android/app/job/IJobCallback.aidl
index 2d3948f..e7695e2 100644
--- a/core/java/android/app/job/IJobCallback.aidl
+++ b/core/java/android/app/job/IJobCallback.aidl
@@ -16,6 +16,8 @@
 
 package android.app.job;
 
+import android.app.job.JobWorkItem;
+
 /**
  * The server side of the JobScheduler IPC protocols.  The app-side implementation
  * invokes on this interface to indicate completion of the (asynchronous) instructions
@@ -43,6 +45,14 @@
      */
     void acknowledgeStopMessage(int jobId, boolean reschedule);
     /*
+     * Called to deqeue next work item for the job.
+     */
+    JobWorkItem dequeueWork(int jobId);
+    /*
+     * Called to report that job has completed processing a work item.
+     */
+    boolean completeWork(int jobId, int workId);
+    /*
      * Tell the job manager that the client is done with its execution, so that it can go on to
      * the next one and stop attributing wakelock time to us etc.
      *
diff --git a/core/java/android/app/job/IJobScheduler.aidl b/core/java/android/app/job/IJobScheduler.aidl
index b6eec27..e94da0c 100644
--- a/core/java/android/app/job/IJobScheduler.aidl
+++ b/core/java/android/app/job/IJobScheduler.aidl
@@ -17,6 +17,7 @@
 package android.app.job;
 
 import android.app.job.JobInfo;
+import android.app.job.JobWorkItem;
 
  /**
   * IPC interface that supports the app-facing {@link #JobScheduler} api.
@@ -24,6 +25,7 @@
   */
 interface IJobScheduler {
     int schedule(in JobInfo job);
+    int enqueue(in JobInfo job, in JobWorkItem work);
     int scheduleAsPackage(in JobInfo job, String packageName, int userId, String tag);
     void cancel(int jobId);
     void cancelAll();
diff --git a/core/java/android/app/job/JobInfo.java b/core/java/android/app/job/JobInfo.java
index 8220ff9..412e445 100644
--- a/core/java/android/app/job/JobInfo.java
+++ b/core/java/android/app/job/JobInfo.java
@@ -23,6 +23,7 @@
 import android.content.ClipData;
 import android.content.ComponentName;
 import android.net.Uri;
+import android.os.BaseBundle;
 import android.os.Bundle;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -442,6 +443,130 @@
         return hasLateConstraint;
     }
 
+    private static boolean kindofEqualsBundle(BaseBundle a, BaseBundle b) {
+        return (a == b) || (a != null && a.kindofEquals(b));
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (!(o instanceof JobInfo)) {
+            return false;
+        }
+        JobInfo j = (JobInfo) o;
+        if (jobId != j.jobId) {
+            return false;
+        }
+        // XXX won't be correct if one is parcelled and the other not.
+        if (!kindofEqualsBundle(extras, j.extras)) {
+            return false;
+        }
+        // XXX won't be correct if one is parcelled and the other not.
+        if (!kindofEqualsBundle(transientExtras, j.transientExtras)) {
+            return false;
+        }
+        // XXX for now we consider two different clip data objects to be different,
+        // regardless of whether their contents are the same.
+        if (clipData != j.clipData) {
+            return false;
+        }
+        if (clipGrantFlags != j.clipGrantFlags) {
+            return false;
+        }
+        if (!Objects.equals(service, j.service)) {
+            return false;
+        }
+        if (constraintFlags != j.constraintFlags) {
+            return false;
+        }
+        if (!Objects.deepEquals(triggerContentUris, j.triggerContentUris)) {
+            return false;
+        }
+        if (triggerContentUpdateDelay != j.triggerContentUpdateDelay) {
+            return false;
+        }
+        if (triggerContentMaxDelay != j.triggerContentMaxDelay) {
+            return false;
+        }
+        if (hasEarlyConstraint != j.hasEarlyConstraint) {
+            return false;
+        }
+        if (hasLateConstraint != j.hasLateConstraint) {
+            return false;
+        }
+        if (networkType != j.networkType) {
+            return false;
+        }
+        if (minLatencyMillis != j.minLatencyMillis) {
+            return false;
+        }
+        if (maxExecutionDelayMillis != j.maxExecutionDelayMillis) {
+            return false;
+        }
+        if (isPeriodic != j.isPeriodic) {
+            return false;
+        }
+        if (isPersisted != j.isPersisted) {
+            return false;
+        }
+        if (intervalMillis != j.intervalMillis) {
+            return false;
+        }
+        if (flexMillis != j.flexMillis) {
+            return false;
+        }
+        if (initialBackoffMillis != j.initialBackoffMillis) {
+            return false;
+        }
+        if (backoffPolicy != j.backoffPolicy) {
+            return false;
+        }
+        if (priority != j.priority) {
+            return false;
+        }
+        if (flags != j.flags) {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int hashCode = jobId;
+        if (extras != null) {
+            hashCode = 31*hashCode + extras.hashCode();
+        }
+        if (transientExtras != null) {
+            hashCode = 31*hashCode + transientExtras.hashCode();
+        }
+        if (clipData != null) {
+            hashCode = 31*hashCode + clipData.hashCode();
+        }
+        hashCode = 31*hashCode + clipGrantFlags;
+        if (service != null) {
+            hashCode = 31*hashCode + service.hashCode();
+        }
+        hashCode = 31*hashCode + constraintFlags;
+        if (triggerContentUris != null) {
+            hashCode = 31*hashCode + triggerContentUris.hashCode();
+        }
+        hashCode = 31*hashCode + Long.hashCode(triggerContentUpdateDelay);
+        hashCode = 31*hashCode + Long.hashCode(triggerContentMaxDelay);
+        hashCode = 31*hashCode + Boolean.hashCode(hasEarlyConstraint);
+        hashCode = 31*hashCode + Boolean.hashCode(hasLateConstraint);
+        hashCode = 31*hashCode + networkType;
+        hashCode = 31*hashCode + Long.hashCode(minLatencyMillis);
+        hashCode = 31*hashCode + Long.hashCode(maxExecutionDelayMillis);
+        hashCode = 31*hashCode + Boolean.hashCode(isPeriodic);
+        hashCode = 31*hashCode + Boolean.hashCode(isPersisted);
+        hashCode = 31*hashCode + Long.hashCode(intervalMillis);
+        hashCode = 31*hashCode + Long.hashCode(flexMillis);
+        hashCode = 31*hashCode + Long.hashCode(initialBackoffMillis);
+        hashCode = 31*hashCode + backoffPolicy;
+        hashCode = 31*hashCode + priority;
+        hashCode = 31*hashCode + flags;
+        return hashCode;
+    }
+
     private JobInfo(Parcel in) {
         jobId = in.readInt();
         extras = in.readPersistableBundle();
diff --git a/core/java/android/app/job/JobParameters.java b/core/java/android/app/job/JobParameters.java
index 8d52d3b..016a0fa 100644
--- a/core/java/android/app/job/JobParameters.java
+++ b/core/java/android/app/job/JobParameters.java
@@ -24,6 +24,7 @@
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.os.PersistableBundle;
+import android.os.RemoteException;
 
 /**
  * Contains the parameters used to configure/identify your job. You do not create this object
@@ -155,6 +156,53 @@
         return mTriggeredContentAuthorities;
     }
 
+    /**
+     * Dequeue the next pending {@link JobWorkItem} from these JobParameters associated with their
+     * currently running job.  Calling this method when there is no more work available and all
+     * previously dequeued work has been completed will result in the system taking care of
+     * stopping the job for you --
+     * you should not call {@link JobService#jobFinished(JobParameters, boolean)} yourself
+     * (otherwise you risk losing an upcoming JobWorkItem that is being enqueued at the same time).
+     *
+     * @return Returns a new {@link JobWorkItem} if there is one pending, otherwise null.
+     * If null is returned, the system will also stop the job if all work has also been completed.
+     * (This means that for correct operation, you must always call dequeueWork() after you have
+     * completed other work, to check either for more work or allow the system to stop the job.)
+     */
+    public JobWorkItem dequeueWork() {
+        try {
+            return getCallback().dequeueWork(getJobId());
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Report the completion of executing a {@link JobWorkItem} previously returned by
+     * {@link #dequeueWork()}.  This tells the system you are done with the
+     * work associated with that item, so it will not be returned again.  Note that if this
+     * is the last work in the queue, completing it here will <em>not</em> finish the overall
+     * job -- for that to happen, you still need to call {@link #dequeueWork()}
+     * again.
+     *
+     * <p>If you are enqueueing work into a job, you must call this method for each piece
+     * of work you process.  Do <em>not</em> call
+     * {@link JobService#jobFinished(JobParameters, boolean)}
+     * or else you can lose work in your queue.</p>
+     *
+     * @param work The work you have completed processing, as previously returned by
+     * {@link #dequeueWork()}
+     */
+    public void completeWork(JobWorkItem work) {
+        try {
+            if (!getCallback().completeWork(getJobId(), work.getWorkId())) {
+                throw new IllegalArgumentException("Given work is not active: " + work);
+            }
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
     /** @hide */
     public IJobCallback getCallback() {
         return IJobCallback.Stub.asInterface(callback);
diff --git a/core/java/android/app/job/JobScheduler.java b/core/java/android/app/job/JobScheduler.java
index 1b640d0..e0afe03 100644
--- a/core/java/android/app/job/JobScheduler.java
+++ b/core/java/android/app/job/JobScheduler.java
@@ -19,6 +19,10 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.SystemApi;
+import android.content.ClipData;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.PersistableBundle;
 
 import java.util.List;
 
@@ -59,6 +63,10 @@
     public static final int RESULT_SUCCESS = 1;
 
     /**
+     * Schedule a job to be executed.  Will replace any currently scheduled job with the same
+     * ID with the new information in the {@link JobInfo}.  If a job with the given ID is currently
+     * running, it will be stopped.
+     *
      * @param job The job you wish scheduled. See
      * {@link android.app.job.JobInfo.Builder JobInfo.Builder} for more detail on the sorts of jobs
      * you can schedule.
@@ -67,6 +75,42 @@
     public abstract int schedule(JobInfo job);
 
     /**
+     * Similar to {@link #schedule}, but allows you to enqueue work for an existing job.  If a job
+     * with the same ID is already scheduled, it will be replaced with the new {@link JobInfo}, but
+     * any previously enqueued work will remain and be dispatched the next time it runs.  If a job
+     * with the same ID is already running, the new work will be enqueued for it.
+     *
+     * <p>The work you enqueue is later retrieved through
+     * {@link JobParameters#dequeueWork() JobParameters.dequeueWork()}.  Be sure to see there
+     * about how to process work; the act of enqueueing work changes how you should handle the
+     * overall lifecycle of an executing job.</p>
+     *
+     * <p>It is strongly encouraged that you use the same {@link JobInfo} for all work you
+     * enqueue.  This will allow the system to optimal schedule work along with any pending
+     * and/or currently running work.  If the JobInfo changes from the last time the job was
+     * enqueued, the system will need to update the associated JobInfo, which can cause a disruption
+     * in exection.  In particular, this can result in any currently running job that is processing
+     * previous work to be stopped and restarted with the new JobInfo.</p>
+     *
+     * <p>It is recommended that you avoid using
+     * {@link JobInfo.Builder#setExtras(PersistableBundle)} or
+     * {@link JobInfo.Builder#setTransientExtras(Bundle)} with a JobInfo you are using to
+     * enqueue work.  The system will try to compare these extras with the previous JobInfo,
+     * but there are situations where it may get this wrong and count the JobInfo as changing.
+     * (That said, you should be relatively safe with a simple set of consistent data in these
+     * fields.)  You should never use {@link JobInfo.Builder#setClipData(ClipData, int)} with
+     * work you are enqueue, since currently this will always be treated as a different JobInfo,
+     * even if the ClipData contents is exactly the same.</p>
+     *
+     * @param job The job you wish to enqueue work for. See
+     * {@link android.app.job.JobInfo.Builder JobInfo.Builder} for more detail on the sorts of jobs
+     * you can schedule.
+     * @param work New work to enqueue.  This will be available later when the job starts running.
+     * @return An int representing ({@link #RESULT_SUCCESS} or {@link #RESULT_FAILURE}).
+     */
+    public abstract int enqueue(JobInfo job, JobWorkItem work);
+
+    /**
      *
      * @param job The job to be scheduled.
      * @param packageName The package on behalf of which the job is to be scheduled. This will be
diff --git a/core/java/android/app/job/JobWorkItem.aidl b/core/java/android/app/job/JobWorkItem.aidl
new file mode 100644
index 0000000..e8fe47d
--- /dev/null
+++ b/core/java/android/app/job/JobWorkItem.aidl
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2017 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 android.app.job;
+
+/** @hide */
+parcelable JobWorkItem;
diff --git a/core/java/android/app/job/JobWorkItem.java b/core/java/android/app/job/JobWorkItem.java
new file mode 100644
index 0000000..4bb057e
--- /dev/null
+++ b/core/java/android/app/job/JobWorkItem.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2017 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 android.app.job;
+
+import android.content.Intent;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+/**
+ * A unit of work that can be enqueued for a job using
+ * {@link JobScheduler#enqueue JobScheduler.enqueue}.
+ */
+final public class JobWorkItem implements Parcelable {
+    final Intent mIntent;
+    int mWorkId;
+
+    /**
+     * Create a new piece of work.
+     * @param intent The general Intent describing this work.
+     */
+    public JobWorkItem(Intent intent) {
+        mIntent = intent;
+    }
+
+    /**
+     * Return the Intent associated with this work.
+     */
+    public Intent getIntent() {
+        return mIntent;
+    }
+
+    /**
+     * @hide
+     */
+    public void setWorkId(int id) {
+        mWorkId = id;
+    }
+
+    /**
+     * @hide
+     */
+    public int getWorkId() {
+        return mWorkId;
+    }
+
+    public String toString() {
+        return "JobWorkItem{id=" + mWorkId + " intent=" + mIntent + "}";
+    }
+
+    public int describeContents() {
+        return 0;
+    }
+
+    public void writeToParcel(Parcel out, int flags) {
+        if (mIntent != null) {
+            out.writeInt(1);
+            mIntent.writeToParcel(out, 0);
+        } else {
+            out.writeInt(0);
+        }
+        out.writeInt(mWorkId);
+    }
+
+    public static final Parcelable.Creator<JobWorkItem> CREATOR
+            = new Parcelable.Creator<JobWorkItem>() {
+        public JobWorkItem createFromParcel(Parcel in) {
+            return new JobWorkItem(in);
+        }
+
+        public JobWorkItem[] newArray(int size) {
+            return new JobWorkItem[size];
+        }
+    };
+
+    public JobWorkItem(Parcel in) {
+        if (in.readInt() != 0) {
+            mIntent = Intent.CREATOR.createFromParcel(in);
+        } else {
+            mIntent = null;
+        }
+        mWorkId = in.readInt();
+    }
+}
diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java
index 950c76c..a6e91bb 100644
--- a/core/java/android/content/pm/PackageManager.java
+++ b/core/java/android/content/pm/PackageManager.java
@@ -1829,6 +1829,14 @@
     public static final String FEATURE_VULKAN_HARDWARE_VERSION = "android.hardware.vulkan.version";
 
     /**
+     * The device includes broadcast radio tuner.
+     *
+     * @hide FutureFeature
+     */
+    @SdkConstant(SdkConstantType.FEATURE)
+    public static final String FEATURE_RADIO = "android.hardware.radio";
+
+    /**
      * Feature for {@link #getSystemAvailableFeatures} and
      * {@link #hasSystemFeature}: The device includes an accelerometer.
      */
diff --git a/core/java/android/hardware/radio/IRadioService.aidl b/core/java/android/hardware/radio/IRadioService.aidl
new file mode 100644
index 0000000..3c83114
--- /dev/null
+++ b/core/java/android/hardware/radio/IRadioService.aidl
@@ -0,0 +1,28 @@
+/**
+ * Copyright (C) 2017 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 android.hardware.radio;
+
+import android.hardware.radio.ITuner;
+
+/**
+ * API to the broadcast radio service.
+ *
+ * {@hide}
+ */
+interface IRadioService {
+    ITuner openTuner();
+}
diff --git a/core/java/android/hardware/radio/ITuner.aidl b/core/java/android/hardware/radio/ITuner.aidl
new file mode 100644
index 0000000..73f6dc2
--- /dev/null
+++ b/core/java/android/hardware/radio/ITuner.aidl
@@ -0,0 +1,24 @@
+/**
+ * Copyright (C) 2017 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 android.hardware.radio;
+
+import android.hardware.radio.RadioManager;
+
+/** {@hide} */
+interface ITuner {
+    int getProgramInformation(out RadioManager.ProgramInfo[] infoOut);
+}
diff --git a/core/java/android/hardware/radio/RadioManager.aidl b/core/java/android/hardware/radio/RadioManager.aidl
new file mode 100644
index 0000000..d79ae4f
--- /dev/null
+++ b/core/java/android/hardware/radio/RadioManager.aidl
@@ -0,0 +1,20 @@
+/**
+ * Copyright (C) 2017 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 android.hardware.radio;
+
+/** @hide */
+parcelable RadioManager.ProgramInfo;
diff --git a/core/java/android/hardware/radio/RadioManager.java b/core/java/android/hardware/radio/RadioManager.java
index ac2bd95..99412de 100644
--- a/core/java/android/hardware/radio/RadioManager.java
+++ b/core/java/android/hardware/radio/RadioManager.java
@@ -17,12 +17,19 @@
 package android.hardware.radio;
 
 import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.annotation.SystemApi;
 import android.content.Context;
 import android.os.Handler;
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.os.ServiceManager.ServiceNotFoundException;
+import android.os.SystemProperties;
 import android.text.TextUtils;
+import android.util.Log;
+
 import java.util.List;
 import java.util.Arrays;
 
@@ -35,6 +42,7 @@
  */
 @SystemApi
 public class RadioManager {
+    private static final String TAG = "RadioManager";
 
     /** Method return status: successful operation */
     public static final int STATUS_OK = 0;
@@ -1434,23 +1442,40 @@
     public RadioTuner openTuner(int moduleId, BandConfig config, boolean withAudio,
             RadioTuner.Callback callback, Handler handler) {
         if (callback == null) {
-            return null;
+            throw new IllegalArgumentException("callback must not be empty");
         }
-        RadioModule module = new RadioModule(moduleId, config, withAudio, callback, handler);
-        if (module != null) {
-            if (!module.initCheck()) {
-                module = null;
+
+        if (mService != null) {
+            ITuner tuner;
+            try {
+                tuner = mService.openTuner();
+            } catch (RemoteException e) {
+                throw e.rethrowFromSystemServer();
             }
+            return new TunerAdapter(tuner);
         }
+
+        RadioModule module = new RadioModule(moduleId, config, withAudio, callback, handler);
+        if (!module.initCheck()) {
+            Log.e(TAG, "Failed to open tuner");
+            module = null;
+        }
+
         return (RadioTuner)module;
     }
 
-    private final Context mContext;
+    @NonNull private final Context mContext;
+    // TODO(b/36863239): NonNull when transitioned from native service
+    @Nullable private final IRadioService mService;
 
     /**
      * @hide
      */
-    public RadioManager(Context context) {
+    public RadioManager(@NonNull Context context) throws ServiceNotFoundException {
         mContext = context;
+
+        boolean isServiceJava = SystemProperties.getBoolean("config.enable_java_radio", false);
+        mService = isServiceJava ? IRadioService.Stub.asInterface(
+                ServiceManager.getServiceOrThrow(Context.RADIO_SERVICE)) : null;
     }
 }
diff --git a/core/java/android/hardware/radio/TunerAdapter.java b/core/java/android/hardware/radio/TunerAdapter.java
new file mode 100644
index 0000000..1822e07b
--- /dev/null
+++ b/core/java/android/hardware/radio/TunerAdapter.java
@@ -0,0 +1,143 @@
+/**
+ * Copyright (C) 2017 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 android.hardware.radio;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.os.RemoteException;
+import android.util.Log;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Implements the RadioTuner interface by forwarding calls to radio service.
+ */
+class TunerAdapter extends RadioTuner {
+    private static final String TAG = "radio.TunerAdapter";
+
+    @NonNull private final ITuner mTuner;
+
+    TunerAdapter(ITuner tuner) {
+        if (tuner == null) {
+            throw new NullPointerException();
+        }
+        mTuner = tuner;
+    }
+
+    @Override
+    public void close() {
+        // TODO(b/36863239): forward to mTuner
+        Log.w(TAG, "Close call not implemented");
+    }
+
+    @Override
+    public int setConfiguration(RadioManager.BandConfig config) {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public int getConfiguration(RadioManager.BandConfig[] config) {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public int setMute(boolean mute) {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public boolean getMute() {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public int step(int direction, boolean skipSubChannel) {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public int scan(int direction, boolean skipSubChannel) {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public int tune(int channel, int subChannel) {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public int cancel() {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public int getProgramInformation(RadioManager.ProgramInfo[] info) {
+        if (info == null || info.length != 1) {
+            throw new IllegalArgumentException("The argument must be an array of length 1");
+        }
+        try {
+            return mTuner.getProgramInformation(info);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    @Override
+    public boolean startBackgroundScan() {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public @NonNull List<RadioManager.ProgramInfo> getProgramList(@Nullable String filter) {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public boolean isAnalogForced() {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public void setAnalogForced(boolean isForced) {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public boolean isAntennaConnected() {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+
+    @Override
+    public boolean hasControl() {
+        // TODO(b/36863239): forward to mTuner
+        throw new RuntimeException("Not implemented");
+    }
+}
diff --git a/core/java/android/os/BaseBundle.java b/core/java/android/os/BaseBundle.java
index 6f388e2..65025fb 100644
--- a/core/java/android/os/BaseBundle.java
+++ b/core/java/android/os/BaseBundle.java
@@ -325,6 +325,23 @@
     }
 
     /**
+     * @hide This kind-of does an equality comparison.  Kind-of.
+     */
+    public boolean kindofEquals(BaseBundle other) {
+        if (other == null) {
+            return false;
+        }
+        if (isParcelled() != other.isParcelled()) {
+            // Big kind-of here!
+            return false;
+        } else if (isParcelled()) {
+            return mParcelledData.compareData(other.mParcelledData) == 0;
+        } else {
+            return mMap.equals(other.mMap);
+        }
+    }
+
+    /**
      * Removes all elements from the mapping of this Bundle.
      */
     public void clear() {
diff --git a/core/java/android/os/Parcel.java b/core/java/android/os/Parcel.java
index 76128e6..c3836a3 100644
--- a/core/java/android/os/Parcel.java
+++ b/core/java/android/os/Parcel.java
@@ -315,6 +315,7 @@
     private static native byte[] nativeMarshall(long nativePtr);
     private static native long nativeUnmarshall(
             long nativePtr, byte[] data, int offset, int length);
+    private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
     private static native long nativeAppendFrom(
             long thisNativePtr, long otherNativePtr, int offset, int length);
     @FastNative
@@ -487,6 +488,11 @@
         updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
     }
 
+    /** @hide */
+    public final int compareData(Parcel other) {
+        return nativeCompareData(mNativePtr, other.mNativePtr);
+    }
+
     /**
      * Report whether the parcel contains any marshalled file descriptors.
      */
diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java
index a6bf2d2..526c2ce 100644
--- a/core/java/android/os/UserManager.java
+++ b/core/java/android/os/UserManager.java
@@ -64,7 +64,7 @@
  */
 public class UserManager {
 
-    private static String TAG = "UserManager";
+    private static final String TAG = "UserManager";
     private final IUserManager mService;
     private final Context mContext;
 
@@ -218,6 +218,23 @@
     public static final String DISALLOW_BLUETOOTH = "no_bluetooth";
 
     /**
+     * Specifies if bluetooth sharing is disallowed on the device. Device owner and profile owner
+     * can set this restriction. When it is set by device owner, all users on this device will be
+     * affected.
+     *
+     * <p>Default is <code>true</code> for managed profiles and false for otherwise. When a device
+     * upgrades to {@link android.os.Build.VERSION_CODES#O}, the system sets it for all existing
+     * managed profiles.
+     *
+     * <p>Key for user restrictions.
+     * <p>Type: Boolean
+     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
+     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
+     * @see #getUserRestrictions()
+     */
+    public static final String DISALLOW_BLUETOOTH_SHARING = "no_bluetooth_sharing";
+
+    /**
      * Specifies if a user is disallowed from transferring files over
      * USB. This can only be set by device owners and profile owners on the primary user.
      * The default value is <code>false</code>.
diff --git a/core/java/android/provider/FontsContract.java b/core/java/android/provider/FontsContract.java
index 4deb4ab..f2aed5d 100644
--- a/core/java/android/provider/FontsContract.java
+++ b/core/java/android/provider/FontsContract.java
@@ -62,6 +62,15 @@
     public static final class Columns implements BaseColumns {
         /**
          * Constant used to request data from a font provider. The cursor returned from the query
+         * may populate this column with a long for the font file ID. The client will request a file
+         * descriptor to "file/FILE_ID" with this ID immediately under the top-level content URI. If
+         * not present, the client will request a file descriptor to the top-level URI with the
+         * given base font ID. Note that several results may return the same file ID, e.g. for TTC
+         * files with different indices.
+         */
+        public static final String FILE_ID = "file_id";
+        /**
+         * Constant used to request data from a font provider. The cursor returned from the query
          * should have this column populated with an int for the ttc index for the resulting font.
          */
         public static final String TTC_INDEX = "font_ttc_index";
@@ -282,12 +291,16 @@
     public void getFontFromProvider(FontRequest request, ResultReceiver receiver,
             String authority) {
         ArrayList<FontResult> result = null;
-        Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
+        final Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
                 .authority(authority)
                 .build();
+        final Uri fileBaseUri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
+                .authority(authority)
+                .appendPath("file")
+                .build();
         try (Cursor cursor = mContext.getContentResolver().query(uri, new String[] { Columns._ID,
-                        Columns.TTC_INDEX, Columns.VARIATION_SETTINGS, Columns.STYLE,
-                        Columns.WEIGHT, Columns.ITALIC, Columns.RESULT_CODE },
+                        Columns.FILE_ID, Columns.TTC_INDEX, Columns.VARIATION_SETTINGS,
+                        Columns.STYLE, Columns.WEIGHT, Columns.ITALIC, Columns.RESULT_CODE },
                 "query = ?", new String[] { request.getQuery() }, null);) {
             // TODO: Should we restrict the amount of fonts that can be returned?
             // TODO: Write documentation explaining that all results should be from the same family.
@@ -296,6 +309,7 @@
                 int resultCode = -1;
                 result = new ArrayList<>();
                 final int idColumnIndex = cursor.getColumnIndexOrThrow(Columns._ID);
+                final int fileIdColumnIndex = cursor.getColumnIndex(Columns.FILE_ID);
                 final int ttcIndexColumnIndex = cursor.getColumnIndex(Columns.TTC_INDEX);
                 final int vsColumnIndex = cursor.getColumnIndex(Columns.VARIATION_SETTINGS);
                 final int weightColumnIndex = cursor.getColumnIndex(Columns.WEIGHT);
@@ -319,8 +333,14 @@
                         receiver.send(resultCode, null);
                         return;
                     }
-                    long id = cursor.getLong(idColumnIndex);
-                    Uri fileUri = ContentUris.withAppendedId(uri, id);
+                    Uri fileUri;
+                    if (fileIdColumnIndex == -1) {
+                        long id = cursor.getLong(idColumnIndex);
+                        fileUri = ContentUris.withAppendedId(uri, id);
+                    } else {
+                        long id = cursor.getLong(fileIdColumnIndex);
+                        fileUri = ContentUris.withAppendedId(fileBaseUri, id);
+                    }
                     try {
                         ParcelFileDescriptor pfd =
                                 mContext.getContentResolver().openFileDescriptor(fileUri, "r");
diff --git a/core/java/android/text/FontConfig.java b/core/java/android/text/FontConfig.java
index 8537d8f..ed58390 100644
--- a/core/java/android/text/FontConfig.java
+++ b/core/java/android/text/FontConfig.java
@@ -23,8 +23,6 @@
 import android.annotation.Nullable;
 import android.graphics.fonts.FontVariationAxis;
 import android.net.Uri;
-import android.os.Parcel;
-import android.os.Parcelable;
 
 import java.lang.annotation.Retention;
 
@@ -33,7 +31,7 @@
  * Font configuration descriptions for System fonts.
  * @hide
  */
-public final class FontConfig implements Parcelable {
+public final class FontConfig {
     private final @NonNull Family[] mFamilies;
     private final @NonNull Alias[] mAliases;
 
@@ -57,37 +55,9 @@
     }
 
     /**
-     * @hide
-     */
-    public FontConfig(Parcel in) {
-        mFamilies = in.readTypedArray(Family.CREATOR);
-        mAliases = in.readTypedArray(Alias.CREATOR);
-    }
-
-    @Override
-    public void writeToParcel(Parcel out, int flag) {
-        out.writeTypedArray(mFamilies, flag);
-        out.writeTypedArray(mAliases, flag);
-    }
-
-    @Override
-    public int describeContents() {
-        return 0;
-    }
-
-    public static final Parcelable.Creator<FontConfig> CREATOR = new Parcelable.Creator() {
-        public FontConfig createFromParcel(Parcel in) {
-            return new FontConfig(in);
-        }
-        public FontConfig[] newArray(int size) {
-            return new FontConfig[size];
-        }
-    };
-
-    /**
      * Class that holds information about a Font.
      */
-    public static final class Font implements Parcelable {
+    public static final class Font {
         private final @NonNull String mFontName;
         private final int mTtcIndex;
         private final @NonNull FontVariationAxis[] mAxes;
@@ -152,57 +122,15 @@
             return mUri;
         }
 
-        /**
-         * @hide
-         */
         public void setUri(@NonNull Uri uri) {
             mUri = uri;
         }
-
-        /**
-         * @hide
-         */
-        public Font(Parcel in) {
-            mFontName = in.readString();
-            mTtcIndex = in.readInt();
-            mAxes = in.createTypedArray(FontVariationAxis.CREATOR);
-            mWeight = in.readInt();
-            mIsItalic = in.readInt() == 1;
-            mUri = in.readTypedObject(Uri.CREATOR);
-        }
-
-        @Override
-        public void writeToParcel(Parcel out, int flag) {
-            out.writeString(mFontName);
-            out.writeInt(mTtcIndex);
-            out.writeTypedArray(mAxes, flag);
-            out.writeInt(mWeight);
-            out.writeInt(mIsItalic ? 1 : 0);
-            out.writeTypedObject(mUri, flag);
-        }
-
-        @Override
-        public int describeContents() {
-            return 0;
-        }
-
-        public static final Creator<Font> CREATOR = new Creator<Font>() {
-            @Override
-            public Font createFromParcel(Parcel in) {
-                return new Font(in);
-            }
-
-            @Override
-            public Font[] newArray(int size) {
-                return new Font[size];
-            }
-        };
     }
 
     /**
      * Class that holds information about a Font alias.
      */
-    public static final class Alias implements Parcelable {
+    public static final class Alias {
         private final @NonNull String mName;
         private final @NonNull String mToName;
         private final int mWeight;
@@ -233,45 +161,12 @@
         public int getWeight() {
             return mWeight;
         }
-
-        /**
-         * @hide
-         */
-        public Alias(Parcel in) {
-            mName = in.readString();
-            mToName = in.readString();
-            mWeight = in.readInt();
-        }
-
-        @Override
-        public void writeToParcel(Parcel out, int flag) {
-            out.writeString(mName);
-            out.writeString(mToName);
-            out.writeInt(mWeight);
-        }
-
-        @Override
-        public int describeContents() {
-            return 0;
-        }
-
-        public static final Creator<Alias> CREATOR = new Creator<Alias>() {
-            @Override
-            public Alias createFromParcel(Parcel in) {
-                return new Alias(in);
-            }
-
-            @Override
-            public Alias[] newArray(int size) {
-                return new Alias[size];
-            }
-        };
     }
 
     /**
      * Class that holds information about a Font family.
      */
-    public static final class Family implements Parcelable {
+    public static final class Family {
         private final @NonNull String mName;
         private final @NonNull Font[] mFonts;
         private final @NonNull String mLanguage;
@@ -343,40 +238,5 @@
         public @Variant int getVariant() {
             return mVariant;
         }
-
-        /**
-         * @hide
-         */
-        public Family(Parcel in) {
-            mName = in.readString();
-            mFonts = in.readTypedArray(Font.CREATOR);
-            mLanguage = in.readString();
-            mVariant = in.readInt();
-        }
-
-        @Override
-        public void writeToParcel(Parcel out, int flag) {
-            out.writeString(mName);
-            out.writeTypedArray(mFonts, flag);
-            out.writeString(mLanguage);
-            out.writeInt(mVariant);
-        }
-
-        @Override
-        public int describeContents() {
-            return 0;
-        }
-
-        public static final Creator<Family> CREATOR = new Creator<Family>() {
-            @Override
-            public Family createFromParcel(Parcel in) {
-                return new Family(in);
-            }
-
-            @Override
-            public Family[] newArray(int size) {
-                return new Family[size];
-            }
-        };
     }
 }
diff --git a/core/java/android/text/SpannableStringBuilder.java b/core/java/android/text/SpannableStringBuilder.java
index 5f01f7b..d41dfdc 100644
--- a/core/java/android/text/SpannableStringBuilder.java
+++ b/core/java/android/text/SpannableStringBuilder.java
@@ -98,7 +98,7 @@
                 if (en > end - start)
                     en = end - start;
 
-                setSpan(false, spans[i], st, en, fl);
+                setSpan(false, spans[i], st, en, fl, false/*enforceParagraph*/);
             }
             restoreInvariants();
         }
@@ -355,7 +355,8 @@
                 }
 
                 if (spanStart != ost || spanEnd != oen) {
-                    setSpan(false, mSpans[i], spanStart, spanEnd, mSpanFlags[i]);
+                    setSpan(false, mSpans[i], spanStart, spanEnd, mSpanFlags[i],
+                            true/*enforceParagraph*/);
                     changed = true;
                 }
             }
@@ -430,13 +431,8 @@
                     int copySpanEnd = en - csStart + start;
                     int copySpanFlags = sp.getSpanFlags(spans[i]) | SPAN_ADDED;
 
-                    int flagsStart = (copySpanFlags & START_MASK) >> START_SHIFT;
-                    int flagsEnd = copySpanFlags & END_MASK;
-
-                    if(!isInvalidParagraphStart(copySpanStart, flagsStart) &&
-                            !isInvalidParagraphEnd(copySpanEnd, flagsEnd)) {
-                        setSpan(false, spans[i], copySpanStart, copySpanEnd, copySpanFlags);
-                    }
+                    setSpan(false, spans[i], copySpanStart, copySpanEnd, copySpanFlags,
+                            false/*enforceParagraph*/);
                 }
             }
             restoreInvariants();
@@ -558,7 +554,7 @@
 
                 changed = true;
                 setSpan(false, Selection.SELECTION_START, selectionStart, selectionStart,
-                        Spanned.SPAN_POINT_POINT);
+                        Spanned.SPAN_POINT_POINT, true/*enforceParagraph*/);
             }
             if (selectionEnd > start && selectionEnd < end) {
                 final long diff = selectionEnd - start;
@@ -567,7 +563,7 @@
 
                 changed = true;
                 setSpan(false, Selection.SELECTION_END, selectionEnd, selectionEnd,
-                        Spanned.SPAN_POINT_POINT);
+                        Spanned.SPAN_POINT_POINT, true/*enforceParagraph*/);
             }
             if (changed) {
                 restoreInvariants();
@@ -673,23 +669,34 @@
      * inserted at the start or end of the span's range.
      */
     public void setSpan(Object what, int start, int end, int flags) {
-        setSpan(true, what, start, end, flags);
+        setSpan(true, what, start, end, flags, true/*enforceParagraph*/);
     }
 
     // Note: if send is false, then it is the caller's responsibility to restore
     // invariants. If send is false and the span already exists, then this method
     // will not change the index of any spans.
-    private void setSpan(boolean send, Object what, int start, int end, int flags) {
+    private void setSpan(boolean send, Object what, int start, int end, int flags,
+            boolean enforceParagraph) {
         checkRange("setSpan", start, end);
 
         int flagsStart = (flags & START_MASK) >> START_SHIFT;
-        if(isInvalidParagraphStart(start, flagsStart)) {
-            throw new RuntimeException("PARAGRAPH span must start at paragraph boundary");
+        if (isInvalidParagraph(start, flagsStart)) {
+            if (!enforceParagraph) {
+                // do not set the span
+                return;
+            }
+            throw new RuntimeException("PARAGRAPH span must start at paragraph boundary"
+                    + " (" + start + " follows " + charAt(start - 1) + ")");
         }
 
         int flagsEnd = flags & END_MASK;
-        if(isInvalidParagraphEnd(end, flagsEnd)) {
-            throw new RuntimeException("PARAGRAPH span must end at paragraph boundary");
+        if (isInvalidParagraph(end, flagsEnd)) {
+            if (!enforceParagraph) {
+                // do not set the span
+                return;
+            }
+            throw new RuntimeException("PARAGRAPH span must end at paragraph boundary"
+                    + " (" + end + " follows " + charAt(end - 1) + ")");
         }
 
         // 0-length Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
@@ -767,26 +774,8 @@
         }
     }
 
-    private final boolean isInvalidParagraphStart(int start, int flagsStart) {
-        if (flagsStart == PARAGRAPH) {
-            if (start != 0 && start != length()) {
-                char c = charAt(start - 1);
-
-                if (c != '\n') return true;
-            }
-        }
-        return false;
-    }
-
-    private final boolean isInvalidParagraphEnd(int end, int flagsEnd) {
-        if (flagsEnd == PARAGRAPH) {
-            if (end != 0 && end != length()) {
-                char c = charAt(end - 1);
-
-                if (c != '\n') return true;
-            }
-        }
-        return false;
+    private boolean isInvalidParagraph(int index, int flag) {
+        return flag == PARAGRAPH && index != 0 && index != length() && charAt(index - 1) != '\n';
     }
 
     /**
diff --git a/core/java/android/text/SpannableStringInternal.java b/core/java/android/text/SpannableStringInternal.java
index 4b02df86..366ec14 100644
--- a/core/java/android/text/SpannableStringInternal.java
+++ b/core/java/android/text/SpannableStringInternal.java
@@ -65,7 +65,7 @@
             if (en > end)
                 en = end;
 
-            setSpan(spans[i], st - start, en - start, fl);
+            setSpan(spans[i], st - start, en - start, fl, false/*enforceParagraph*/);
         }
     }
 
@@ -149,28 +149,36 @@
     }
 
     /* package */ void setSpan(Object what, int start, int end, int flags) {
+        setSpan(what, start, end, flags, true/*enforceParagraph*/);
+    }
+
+    private boolean isIndexFollowsNextLine(int index) {
+        return index != 0 && index != length() && charAt(index - 1) != '\n';
+    }
+
+    private void setSpan(Object what, int start, int end, int flags, boolean enforceParagraph) {
         int nstart = start;
         int nend = end;
 
         checkRange("setSpan", start, end);
 
         if ((flags & Spannable.SPAN_PARAGRAPH) == Spannable.SPAN_PARAGRAPH) {
-            if (start != 0 && start != length()) {
-                char c = charAt(start - 1);
-
-                if (c != '\n')
-                    throw new RuntimeException(
-                            "PARAGRAPH span must start at paragraph boundary" +
-                            " (" + start + " follows " + c + ")");
+            if (isIndexFollowsNextLine(start)) {
+                if (!enforceParagraph) {
+                    // do not set the span
+                    return;
+                }
+                throw new RuntimeException("PARAGRAPH span must start at paragraph boundary"
+                        + " (" + start + " follows " + charAt(start - 1) + ")");
             }
 
-            if (end != 0 && end != length()) {
-                char c = charAt(end - 1);
-
-                if (c != '\n')
-                    throw new RuntimeException(
-                            "PARAGRAPH span must end at paragraph boundary" +
-                            " (" + end + " follows " + c + ")");
+            if (isIndexFollowsNextLine(end)) {
+                if (!enforceParagraph) {
+                    // do not set the span
+                    return;
+                }
+                throw new RuntimeException("PARAGRAPH span must end at paragraph boundary"
+                        + " (" + end + " follows " + charAt(end - 1) + ")");
             }
         }
 
diff --git a/core/java/android/transition/ChangeBounds.java b/core/java/android/transition/ChangeBounds.java
index b6d8aa4..546f93a 100644
--- a/core/java/android/transition/ChangeBounds.java
+++ b/core/java/android/transition/ChangeBounds.java
@@ -472,9 +472,9 @@
         private int mTop;
         private int mRight;
         private int mBottom;
-        private boolean mIsTopLeftSet;
-        private boolean mIsBottomRightSet;
         private View mView;
+        private int mTopLeftCalls;
+        private int mBottomRightCalls;
 
         public ViewBounds(View view) {
             mView = view;
@@ -483,8 +483,8 @@
         public void setTopLeft(PointF topLeft) {
             mLeft = Math.round(topLeft.x);
             mTop = Math.round(topLeft.y);
-            mIsTopLeftSet = true;
-            if (mIsBottomRightSet) {
+            mTopLeftCalls++;
+            if (mTopLeftCalls == mBottomRightCalls) {
                 setLeftTopRightBottom();
             }
         }
@@ -492,16 +492,16 @@
         public void setBottomRight(PointF bottomRight) {
             mRight = Math.round(bottomRight.x);
             mBottom = Math.round(bottomRight.y);
-            mIsBottomRightSet = true;
-            if (mIsTopLeftSet) {
+            mBottomRightCalls++;
+            if (mTopLeftCalls == mBottomRightCalls) {
                 setLeftTopRightBottom();
             }
         }
 
         private void setLeftTopRightBottom() {
             mView.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
-            mIsTopLeftSet = false;
-            mIsBottomRightSet = false;
+            mTopLeftCalls = 0;
+            mBottomRightCalls = 0;
         }
     }
 }
diff --git a/core/java/com/android/internal/policy/DecorView.java b/core/java/com/android/internal/policy/DecorView.java
index 8e6e63b..c4540f5 100644
--- a/core/java/com/android/internal/policy/DecorView.java
+++ b/core/java/com/android/internal/policy/DecorView.java
@@ -2263,8 +2263,9 @@
     @Override
     public void requestKeyboardShortcuts(List<KeyboardShortcutGroup> list, int deviceId) {
         final PanelFeatureState st = mWindow.getPanelState(FEATURE_OPTIONS_PANEL, false);
-        if (!mWindow.isDestroyed() && st != null && mWindow.getCallback() != null) {
-            mWindow.getCallback().onProvideKeyboardShortcuts(list, st.menu, deviceId);
+        final Menu menu = st != null ? st.menu : null;
+        if (!mWindow.isDestroyed() && mWindow.getCallback() != null) {
+            mWindow.getCallback().onProvideKeyboardShortcuts(list, menu, deviceId);
         }
     }
 
diff --git a/core/jni/android_os_Parcel.cpp b/core/jni/android_os_Parcel.cpp
index 8f7908a..d740a76 100644
--- a/core/jni/android_os_Parcel.cpp
+++ b/core/jni/android_os_Parcel.cpp
@@ -617,6 +617,21 @@
     return parcel->getOpenAshmemSize();
 }
 
+static jint android_os_Parcel_compareData(JNIEnv* env, jclass clazz, jlong thisNativePtr,
+                                          jlong otherNativePtr)
+{
+    Parcel* thisParcel = reinterpret_cast<Parcel*>(thisNativePtr);
+    if (thisParcel == NULL) {
+       return 0;
+    }
+    Parcel* otherParcel = reinterpret_cast<Parcel*>(otherNativePtr);
+    if (otherParcel == NULL) {
+       return thisParcel->getOpenAshmemSize();
+    }
+
+    return thisParcel->compareData(*otherParcel);
+}
+
 static jlong android_os_Parcel_appendFrom(JNIEnv* env, jclass clazz, jlong thisNativePtr,
                                           jlong otherNativePtr, jint offset, jint length)
 {
@@ -781,6 +796,7 @@
 
     {"nativeMarshall",            "(J)[B", (void*)android_os_Parcel_marshall},
     {"nativeUnmarshall",          "(J[BII)J", (void*)android_os_Parcel_unmarshall},
+    {"nativeCompareData",         "(JJ)I", (void*)android_os_Parcel_compareData},
     {"nativeAppendFrom",          "(JJII)J", (void*)android_os_Parcel_appendFrom},
     // @FastNative
     {"nativeHasFileDescriptors",  "(J)Z", (void*)android_os_Parcel_hasFileDescriptors},
diff --git a/core/res/res/drawable/autofilled_highlight.xml b/core/res/res/drawable/autofilled_highlight.xml
index c7aacb9..3a2815c 100644
--- a/core/res/res/drawable/autofilled_highlight.xml
+++ b/core/res/res/drawable/autofilled_highlight.xml
@@ -15,12 +15,6 @@
   * limitations under the License.
   -->
 
-<inset xmlns:android="http://schemas.android.com/apk/res/android"
-    android:insetLeft="4dp"
-    android:insetRight="4dp"
-    android:insetBottom="4dp"
-    android:insetTop="4dp">
-    <shape>
-        <solid android:color="@color/autofilled_highlight" />
-    </shape>
-</inset>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <solid android:color="@color/autofilled_highlight" />
+</shape>
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index febbb31..1df08c3 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -2798,7 +2798,7 @@
     <string name="config_defaultCellBroadcastReceiverPkg" translatable="false">com.android.cellbroadcastreceiver</string>
 
     <!-- Specifies the path that is used by AdaptiveIconDrawable class to crop launcher icons. -->
-    <string name="config_icon_mask" translatable="false">"M50,0L100,0 100,100 0,100 0,0z"</string>
+    <string name="config_icon_mask" translatable="false">"M50,0L92,0 A8,8,0,0 1 100,8 L100,92 A8,8,0,0 1 92,100 L8,100 A8,8,0,0 1 0,92 L 0,8 A8,8,0,0 1 8,0z"</string>
 
     <!-- The component name, flattened to a string, for the default accessibility service to be
          enabled by the accessibility shortcut. This service must be trusted, as it can be activated
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index 786fea4..ed940b3 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -2830,6 +2830,10 @@
         <public name="autofilled_highlight" />
     </public-group>
 
+    <public-group type="string" first-id="0x01040019">
+        <public name="paste_as_plain_text" />
+    </public-group>
+
 
   <!-- ===============================================================
        DO NOT ADD UN-GROUPED ITEMS HERE
diff --git a/core/res/res/xml/sms_short_codes.xml b/core/res/res/xml/sms_short_codes.xml
index 1ae922a..644638d 100644
--- a/core/res/res/xml/sms_short_codes.xml
+++ b/core/res/res/xml/sms_short_codes.xml
@@ -55,7 +55,7 @@
     <shortcode country="be" premium="\\d{4}" free="8\\d{3}|116\\d{3}" />
 
     <!-- Bulgaria: 4-5 digits, plus EU -->
-    <shortcode country="bg" pattern="\\d{4,5}" premium="18(?:16|423)|19(?:1[56]|35)" free="116\\d{3}" />
+    <shortcode country="bg" pattern="\\d{4,5}" premium="18(?:16|423)|19(?:1[56]|35)" free="116\\d{3}|1988|1490" />
 
     <!-- Bahrain: 1-5 digits (standard system default, not country specific) -->
     <shortcode country="bh" pattern="\\d{1,5}" free="81181" />
@@ -95,7 +95,7 @@
 
     <!-- Spain: 5-6 digits: 25xxx, 27xxx, 280xx, 35xxx, 37xxx, 795xxx, 797xxx, 995xxx, 997xxx, plus EU.
          http://www.legallink.es/?q=en/content/which-current-regulatory-status-premium-rate-services-spain -->
-    <shortcode country="es" premium="[23][57]\\d{3}|280\\d{2}|[79]9[57]\\d{3}" free="116\\d{3}|22791|222145" />
+    <shortcode country="es" premium="[23][57]\\d{3}|280\\d{2}|[79]9[57]\\d{3}" free="116\\d{3}|22791|222145|22189" />
 
     <!-- Finland: 5-6 digits, premium 0600, 0700: http://en.wikipedia.org/wiki/Telephone_numbers_in_Finland -->
     <shortcode country="fi" pattern="\\d{5,6}" premium="0600.*|0700.*|171(?:59|63)" free="116\\d{3}|14789" />
@@ -103,12 +103,12 @@
     <!-- France: 5 digits, free: 3xxxx, premium [4-8]xxxx, plus EU:
          http://clients.txtnation.com/entries/161972-france-premium-sms-short-code-requirements,
          visual voicemail code for Orange: 21101 -->
-    <shortcode country="fr" premium="[4-8]\\d{4}" free="3\\d{4}|116\\d{3}|21101" />
+    <shortcode country="fr" premium="[4-8]\\d{4}" free="3\\d{4}|116\\d{3}|21101|20366" />
 
     <!-- United Kingdom (Great Britain): 4-6 digits, common codes [5-8]xxxx, plus EU:
          http://www.short-codes.com/media/Co-regulatoryCodeofPracticeforcommonshortcodes170206.pdf,
          visual voicemail code for EE: 887 -->
-    <shortcode country="gb" pattern="\\d{4,6}" premium="[5-8]\\d{4}" free="116\\d{3}|2020|35890|61002|61202|887|83669|34664|40406" />
+    <shortcode country="gb" pattern="\\d{4,6}" premium="[5-8]\\d{4}" free="116\\d{3}|2020|35890|61002|61202|887|83669|34664|40406|60174" />
 
     <!-- Georgia: 4 digits, known premium codes listed -->
     <shortcode country="ge" pattern="\\d{4}" premium="801[234]|888[239]" />
@@ -116,12 +116,15 @@
     <!-- Greece: 5 digits (54xxx, 19yxx, x=0-9, y=0-5): http://www.cmtelecom.com/premium-sms/greece -->
     <shortcode country="gr" pattern="\\d{5}" premium="54\\d{3}|19[0-5]\\d{2}" free="116\\d{3}|12115" />
 
+    <!-- Croatia -->
+    <shortcode country="hr" free="13062" />
+
     <!-- Hungary: 4 or 10 digits starting with 1 or 0, plus EU:
          http://clients.txtnation.com/entries/209633-hungary-premium-sms-short-code-regulations -->
     <shortcode country="hu" pattern="[01](?:\\d{3}|\\d{9})" premium="0691227910|1784" free="116\\d{3}" />
 
     <!-- India: 1-5 digits (standard system default, not country specific) -->
-    <shortcode country="in" pattern="\\d{1,5}" free="59336" />
+    <shortcode country="in" pattern="\\d{1,5}" free="59336|53969" />
 
     <!-- Indonesia: 1-5 digits (standard system default, not country specific) -->
     <shortcode country="id" pattern="\\d{1,5}" free="99477|6006|46645" />
@@ -153,45 +156,48 @@
     <shortcode country="kw" pattern="\\d{1,5}" free="1378|50420|94006" />
 
     <!-- Lithuania: 3-5 digits, known premium codes listed, plus EU -->
-    <shortcode country="lt" pattern="\\d{3,5}" premium="13[89]1|1394|16[34]5" free="116\\d{3}" />
+    <shortcode country="lt" pattern="\\d{3,5}" premium="13[89]1|1394|16[34]5" free="116\\d{3}|1399" />
 
     <!-- Luxembourg: 5 digits, 6xxxx, plus EU:
          http://www.luxgsm.lu/assets/files/filepage/file_1253803400.pdf -->
-    <shortcode country="lu" premium="6\\d{4}" free="116\\d{3}" />
+    <shortcode country="lu" premium="6\\d{4}" free="116\\d{3}|60231" />
 
     <!-- Latvia: 4 digits, known premium codes listed, plus EU -->
-    <shortcode country="lv" pattern="\\d{4}" premium="18(?:19|63|7[1-4])" free="116\\d{3}" />
+    <shortcode country="lv" pattern="\\d{4}" premium="18(?:19|63|7[1-4])" free="116\\d{3}|1399" />
 
     <!-- Mexico: 4-5 digits (not confirmed), known premium codes listed -->
     <shortcode country="mx" pattern="\\d{4,5}" premium="53035|7766" free="46645|5050|26259|50025|50052" />
 
     <!-- Malaysia: 5 digits: http://www.skmm.gov.my/attachment/Consumer_Regulation/Mobile_Content_Services_FAQs.pdf -->
-    <shortcode country="my" pattern="\\d{5}" premium="32298|33776" free="22099" />
+    <shortcode country="my" pattern="\\d{5}" premium="32298|33776" free="22099|28288" />
 
     <!-- The Netherlands, 4 digits, known premium codes listed, plus EU -->
-    <shortcode country="nl" pattern="\\d{4}" premium="4466|5040" free="116\\d{3}|2223|6225" />
+    <shortcode country="nl" pattern="\\d{4}" premium="4466|5040" free="116\\d{3}|2223|6225|2223" />
 
     <!-- Norway: 4-5 digits (not confirmed), known premium codes listed -->
     <shortcode country="no" pattern="\\d{4,5}" premium="2201|222[67]" />
 
     <!-- New Zealand: 3-4 digits, known premium codes listed -->
-    <shortcode country="nz" pattern="\\d{3,4}" premium="3903|8995|4679" />
+    <shortcode country="nz" pattern="\\d{3,4}" premium="3903|8995|4679" free="3067|3068|4053" />
 
     <!-- Philippines -->
     <shortcode country="ph" free="2147|5495|5496" />
 
+    <!-- Pakistan -->
+    <shortcode country="pk" free="2057" />
+
     <!-- Poland: 4-5 digits (not confirmed), known premium codes listed, plus EU -->
     <shortcode country="pl" pattern="\\d{4,5}" premium="74240|79(?:10|866)|92525" free="116\\d{3}|8012|80921" />
 
     <!-- Portugal: 5 digits, plus EU:
          http://clients.txtnation.com/entries/158326-portugal-premium-sms-short-code-regulations -->
-    <shortcode country="pt" premium="6[1289]\\d{3}" free="116\\d{3}" />
+    <shortcode country="pt" premium="6[1289]\\d{3}" free="116\\d{3}|1262" />
 
     <!-- Qatar: 1-5 digits (standard system default, not country specific) -->
     <shortcode country="qa" pattern="\\d{1,5}" free="92451" />
 
     <!-- Romania: 4 digits, plus EU: http://www.simplus.ro/en/resources/glossary-of-terms/ -->
-    <shortcode country="ro" pattern="\\d{4}" premium="12(?:63|66|88)|13(?:14|80)" free="116\\d{3}|3654" />
+    <shortcode country="ro" pattern="\\d{4}" premium="12(?:63|66|88)|13(?:14|80)" free="116\\d{3}|3654|8360" />
 
     <!-- Russia: 4 digits, known premium codes listed: http://smscoin.net/info/pricing-russia/ -->
     <shortcode country="ru" pattern="\\d{4}" premium="1(?:1[56]1|899)|2(?:09[57]|322|47[46]|880|990)|3[589]33|4161|44(?:4[3-9]|81)|77(?:33|81)|8424" />
@@ -207,7 +213,7 @@
     <shortcode country="sg" pattern="7\\d{4}" premium="73800" standard="74688|70134" />
 
     <!-- Slovenia: 4 digits (premium=3xxx, 6xxx, 8xxx), plus EU: http://www.cmtelecom.com/premium-sms/slovenia -->
-    <shortcode country="si" pattern="\\d{4}" premium="[368]\\d{3}" free="116\\d{3}" />
+    <shortcode country="si" pattern="\\d{4}" premium="[368]\\d{3}" free="116\\d{3}|3133" />
 
     <!-- Slovakia: 4 digits (premium), plus EU: http://www.cmtelecom.com/premium-sms/slovakia -->
     <shortcode country="sk" premium="\\d{4}" free="116\\d{3}|8000" />
@@ -219,7 +225,7 @@
     <shortcode country="tj" pattern="\\d{4}" premium="11[3-7]1|4161|4333|444[689]" />
 
     <!-- Turkey -->
-    <shortcode country="tr" free="7529|5528" />
+    <shortcode country="tr" free="7529|5528|6493" />
 
     <!-- Ukraine: 4 digits, known premium codes listed -->
     <shortcode country="ua" pattern="\\d{4}" premium="444[3-9]|70[579]4|7540" />
diff --git a/graphics/java/android/graphics/Canvas.java b/graphics/java/android/graphics/Canvas.java
index 7289429..2a2e14b 100644
--- a/graphics/java/android/graphics/Canvas.java
+++ b/graphics/java/android/graphics/Canvas.java
@@ -157,10 +157,12 @@
 
     /**
      * Specify a bitmap for the canvas to draw into. All canvas state such as
-     * layers, filters, and the save/restore stack are reset with the exception
-     * of the current matrix and clip stack. Additionally, as a side-effect
+     * layers, filters, and the save/restore stack are reset. Additionally,
      * the canvas' target density is updated to match that of the bitmap.
      *
+     * Prior to API level {@value Build.VERSION_CODES#O} the current matrix and
+     * clip stack were preserved.
+     *
      * @param bitmap Specifies a mutable bitmap for the canvas to draw into.
      * @see #setDensity(int)
      * @see #getDensity()
diff --git a/graphics/java/android/graphics/fonts/FontVariationAxis.java b/graphics/java/android/graphics/fonts/FontVariationAxis.java
index 91ec0e8..fb2a3a8 100644
--- a/graphics/java/android/graphics/fonts/FontVariationAxis.java
+++ b/graphics/java/android/graphics/fonts/FontVariationAxis.java
@@ -18,8 +18,6 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.os.Parcel;
-import android.os.Parcelable;
 import android.text.TextUtils;
 
 import java.util.ArrayList;
@@ -28,7 +26,7 @@
 /**
  * Class that holds information about single font variation axis.
  */
-public final class FontVariationAxis implements Parcelable {
+public final class FontVariationAxis {
     private final int mTag;
     private final String mTagString;
     private final float mStyleValue;
@@ -74,39 +72,6 @@
     }
 
     /**
-     * @hide
-     */
-    public FontVariationAxis(Parcel in) {
-        mTag = in.readInt();
-        mTagString = in.readString();
-        mStyleValue = in.readFloat();
-    }
-
-    @Override
-    public void writeToParcel(Parcel out, int flag) {
-        out.writeInt(mTag);
-        out.writeString(mTagString);
-        out.writeFloat(mStyleValue);
-    }
-
-    @Override
-    public int describeContents() {
-        return 0;
-    }
-
-    public static final Creator<FontVariationAxis> CREATOR = new Creator<FontVariationAxis>() {
-        @Override
-        public FontVariationAxis createFromParcel(Parcel in) {
-            return new FontVariationAxis(in);
-        }
-
-        @Override
-        public FontVariationAxis[] newArray(int size) {
-            return new FontVariationAxis[size];
-        }
-    };
-
-    /**
      * Returns a valid font variation setting string for this object.
      */
     @Override
diff --git a/libs/hwui/SkiaCanvas.cpp b/libs/hwui/SkiaCanvas.cpp
index 5c7d8d8..c902a73 100644
--- a/libs/hwui/SkiaCanvas.cpp
+++ b/libs/hwui/SkiaCanvas.cpp
@@ -80,38 +80,12 @@
 // Canvas state operations: Replace Bitmap
 // ----------------------------------------------------------------------------
 
-class ClipCopier : public SkCanvas::ClipVisitor {
-public:
-    explicit ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
-
-    virtual void clipRect(const SkRect& rect, SkClipOp op, bool antialias) {
-        m_dstCanvas->clipRect(rect, op, antialias);
-    }
-    virtual void clipRRect(const SkRRect& rrect, SkClipOp op, bool antialias) {
-        m_dstCanvas->clipRRect(rrect, op, antialias);
-    }
-    virtual void clipPath(const SkPath& path, SkClipOp op, bool antialias) {
-        m_dstCanvas->clipPath(path, op, antialias);
-    }
-
-private:
-    SkCanvas* m_dstCanvas;
-};
-
 void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
     sk_sp<SkColorSpace> cs = bitmap.refColorSpace();
     std::unique_ptr<SkCanvas> newCanvas = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
     std::unique_ptr<SkCanvas> newCanvasWrapper = SkCreateColorSpaceXformCanvas(newCanvas.get(),
             cs == nullptr ? SkColorSpace::MakeSRGB() : std::move(cs));
 
-    if (!bitmap.isNull()) {
-        // Copy the canvas matrix & clip state.
-        newCanvasWrapper->setMatrix(mCanvas->getTotalMatrix());
-
-        ClipCopier copier(newCanvasWrapper.get());
-        mCanvas->replayClips(&copier);
-    }
-
     // deletes the previously owned canvas (if any)
     mCanvasOwned = std::move(newCanvas);
     mCanvasWrapper = std::move(newCanvasWrapper);
diff --git a/media/java/android/media/AudioTrack.java b/media/java/android/media/AudioTrack.java
index 7f5d3a0..bf5939f 100644
--- a/media/java/android/media/AudioTrack.java
+++ b/media/java/android/media/AudioTrack.java
@@ -2523,8 +2523,11 @@
             mAvSyncHeader = ByteBuffer.allocate(16);
             mAvSyncHeader.order(ByteOrder.BIG_ENDIAN);
             mAvSyncHeader.putInt(0x55550001);
-            mAvSyncHeader.putInt(sizeInBytes);
-            mAvSyncHeader.putLong(timestamp);
+        }
+
+        if (mAvSyncBytesRemaining == 0) {
+            mAvSyncHeader.putInt(4, sizeInBytes);
+            mAvSyncHeader.putLong(8, timestamp);
             mAvSyncHeader.position(0);
             mAvSyncBytesRemaining = sizeInBytes;
         }
@@ -2556,9 +2559,6 @@
         }
 
         mAvSyncBytesRemaining -= ret;
-        if (mAvSyncBytesRemaining == 0) {
-            mAvSyncHeader = null;
-        }
 
         return ret;
     }
diff --git a/media/java/android/media/VolumeShaper.java b/media/java/android/media/VolumeShaper.java
index af11e07..1dda6a4 100644
--- a/media/java/android/media/VolumeShaper.java
+++ b/media/java/android/media/VolumeShaper.java
@@ -301,7 +301,7 @@
                 .setInterpolatorType(INTERPOLATOR_TYPE_LINEAR)
                 .setCurve(new float[] {0.f, 1.f} /* times */,
                         new float[] {0.f, 1.f} /* volumes */)
-                .setDurationMs(1000.)
+                .setDurationMillis(1000.)
                 .build();
 
         /**
@@ -314,7 +314,7 @@
                 .setInterpolatorType(INTERPOLATOR_TYPE_CUBIC)
                 .setCurve(new float[] {0.f, 1.f} /* times */,
                         new float[] {0.f, 1.f}  /* volumes */)
-                .setDurationMs(1000.)
+                .setDurationMillis(1000.)
                 .build();
 
         /**
@@ -348,12 +348,12 @@
             SINE_RAMP = new VolumeShaper.Configuration.Builder()
                 .setInterpolatorType(INTERPOLATOR_TYPE_CUBIC)
                 .setCurve(times, sines)
-                .setDurationMs(1000.)
+                .setDurationMillis(1000.)
                 .build();
             SCURVE_RAMP = new VolumeShaper.Configuration.Builder()
                 .setInterpolatorType(INTERPOLATOR_TYPE_CUBIC)
                 .setCurve(times, scurve)
-                .setDurationMs(1000.)
+                .setDurationMillis(1000.)
                 .build();
         }
 
@@ -569,7 +569,7 @@
         /**
          * Returns the duration of the volume shape in milliseconds.
          */
-        public double getDurationMs() {
+        public double getDurationMillis() {
             return mDurationMs;
         }
 
@@ -700,7 +700,7 @@
          *             .setInterpolatorType(VolumeShaper.Configuration.INTERPOLATOR_TYPE_LINEAR)
          *             .setCurve(new float[] { 0.f, 1.f }, // times
          *                       new float[] { 0.f, 1.f }) // volumes
-         *             .setDurationMs(1000.)
+         *             .setDurationMillis(1000.)
          *             .build();
          * </pre>
          * <p>
@@ -731,7 +731,7 @@
                 mId = configuration.getId();
                 mOptionFlags = configuration.getAllOptionFlags();
                 mInterpolatorType = configuration.getInterpolatorType();
-                mDurationMs = configuration.getDurationMs();
+                mDurationMs = configuration.getDurationMillis();
                 mTimes = configuration.getTimes().clone();
                 mVolumes = configuration.getVolumes().clone();
             }
@@ -805,17 +805,17 @@
              *
              * If omitted, the default duration is 1 second.
              *
-             * @param durationMs
+             * @param durationMillis
              * @return the same {@code Builder} instance.
-             * @throws IllegalArgumentException if {@code durationMs}
+             * @throws IllegalArgumentException if {@code durationMillis}
              *         is not strictly positive.
              */
-            public @NonNull Builder setDurationMs(double durationMs) {
-                if (durationMs <= 0.) {
+            public @NonNull Builder setDurationMillis(double durationMillis) {
+                if (durationMillis <= 0.) {
                     throw new IllegalArgumentException(
-                            "duration: " + durationMs + " not positive");
+                            "duration: " + durationMillis + " not positive");
                 }
-                mDurationMs = durationMs;
+                mDurationMs = durationMillis;
                 return this;
             }
 
@@ -833,7 +833,7 @@
              * time (x) coordinates should be monotonically increasing, from 0.f to 1.f;
              * volume (y) coordinates must be within 0.f to 1.f.
              * <p>
-             * The time scale is set by {@link #setDurationMs}.
+             * The time scale is set by {@link #setDurationMillis}.
              * <p>
              * @param times an array of float values representing
              *        the time line of the volume curve.
diff --git a/media/java/android/media/tv/TvContract.java b/media/java/android/media/tv/TvContract.java
index e82dd82..6635b5f 100644
--- a/media/java/android/media/tv/TvContract.java
+++ b/media/java/android/media/tv/TvContract.java
@@ -589,6 +589,36 @@
      * @hide
      */
     interface ProgramColumns {
+        /** @hide */
+        @IntDef({
+                REVIEW_RATING_STYLE_STARS,
+                REVIEW_RATING_STYLE_THUMBS_UP_DOWN,
+                REVIEW_RATING_STYLE_PERCENTAGE,
+        })
+        @Retention(RetentionPolicy.SOURCE)
+        @interface ReviewRatingStyle {}
+
+        /**
+         * The review rating style for five star rating.
+         *
+         * @see #COLUMN_REVIEW_RATING_STYLE
+         */
+        int REVIEW_RATING_STYLE_STARS = 0;
+
+        /**
+         * The review rating style for thumbs-up and thumbs-down rating.
+         *
+         * @see #COLUMN_REVIEW_RATING_STYLE
+         */
+        int REVIEW_RATING_STYLE_THUMBS_UP_DOWN = 1;
+
+        /**
+         * The review rating style for 0 to 100 point system.
+         *
+         * @see #COLUMN_REVIEW_RATING_STYLE
+         */
+        int REVIEW_RATING_STYLE_PERCENTAGE = 2;
+
         /**
          * The title of this TV program.
          *
@@ -851,6 +881,33 @@
          * <p>Type: INTEGER
          */
         String COLUMN_VERSION_NUMBER = "version_number";
+
+        /**
+         * The review rating score style used for {@link #COLUMN_REVIEW_RATING}.
+         *
+         * <p> The value should match one of the followings: {@link #REVIEW_RATING_STYLE_STARS},
+         * {@link #REVIEW_RATING_STYLE_THUMBS_UP_DOWN}, and {@link #REVIEW_RATING_STYLE_PERCENTAGE}.
+         *
+         * <p>Type: INTEGER
+         * @see #COLUMN_REVIEW_RATING
+         */
+        String COLUMN_REVIEW_RATING_STYLE = "review_rating_style";
+
+        /**
+         * The review rating score for this program.
+         *
+         * <p>The format of the value is dependent on {@link #COLUMN_REVIEW_RATING_STYLE}. If the
+         * style is {@link #REVIEW_RATING_STYLE_STARS}, the value should be a real number between
+         * 0.0 and 5.0. (e.g. "4.5") If the style is {@link #REVIEW_RATING_STYLE_THUMBS_UP_DOWN},
+         * the value should be two integers, one for thumbs-up count and the other for thumbs-down
+         * count, with a comma between them. (e.g. "200,40") If the style is
+         * {@link #REVIEW_RATING_STYLE_PERCENTAGE}, the value shoule be a real number between 0 and
+         * 100. (e.g. "99.9")
+         *
+         * <p>Type: TEXT
+         * @see #COLUMN_REVIEW_RATING_STYLE
+         */
+        String COLUMN_REVIEW_RATING = "review_rating";
     }
 
     /**
@@ -1096,36 +1153,6 @@
          */
         int INTERACTION_TYPE_VIEWERS = 6;
 
-        /** @hide */
-        @IntDef({
-                REVIEW_RATING_STYLE_STARS,
-                REVIEW_RATING_STYLE_THUMBS_UP_DOWN,
-                REVIEW_RATING_STYLE_PERCENTAGE,
-        })
-        @Retention(RetentionPolicy.SOURCE)
-        public @interface ReviewRatingStyle {}
-
-        /**
-         * The review rating style for five star rating.
-         *
-         * @see #COLUMN_REVIEW_RATING_STYLE
-         */
-        int REVIEW_RATING_STYLE_STARS = 0;
-
-        /**
-         * The review rating style for thumbs-up and thumbs-down rating.
-         *
-         * @see #COLUMN_REVIEW_RATING_STYLE
-         */
-        int REVIEW_RATING_STYLE_THUMBS_UP_DOWN = 1;
-
-        /**
-         * The review rating style for 0 to 100 point system.
-         *
-         * @see #COLUMN_REVIEW_RATING_STYLE
-         */
-        int REVIEW_RATING_STYLE_PERCENTAGE = 2;
-
         /**
          * The type of this program content.
          *
@@ -1375,33 +1402,6 @@
         String COLUMN_AUTHOR = "author";
 
         /**
-         * The review rating score style used for {@link #COLUMN_REVIEW_RATING}.
-         *
-         * <p> The value should match one of the followings: {@link #REVIEW_RATING_STYLE_STARS},
-         * {@link #REVIEW_RATING_STYLE_THUMBS_UP_DOWN}, and {@link #REVIEW_RATING_STYLE_PERCENTAGE}.
-         *
-         * <p>Type: INTEGER
-         * @see #COLUMN_REVIEW_RATING
-         */
-        String COLUMN_REVIEW_RATING_STYLE = "review_rating_style";
-
-        /**
-         * The review rating score for this program.
-         *
-         * <p>The format of the value is dependent on {@link #COLUMN_REVIEW_RATING_STYLE}. If the
-         * style is {@link #REVIEW_RATING_STYLE_STARS}, the value should be a real number between
-         * 0.0 and 5.0. (e.g. "4.5") If the style is {@link #REVIEW_RATING_STYLE_THUMBS_UP_DOWN},
-         * the value should be two integers, one for thumbs-up count and the other for thumbs-down
-         * count, with a comma between them. (e.g. "200,40") If the style is
-         * {@link #REVIEW_RATING_STYLE_PERCENTAGE}, the value shoule be a real number between 0 and
-         * 100. (e.g. "99.9")
-         *
-         * <p>Type: TEXT
-         * @see #COLUMN_REVIEW_RATING_STYLE
-         */
-        String COLUMN_REVIEW_RATING = "review_rating";
-
-        /**
          * The flag indicating whether this TV program is browsable or not.
          *
          * <p>This column can only be set by applications having proper system permission. For
diff --git a/packages/PrintSpooler/res/values-af/strings.xml b/packages/PrintSpooler/res/values-af/strings.xml
index e0702af..5b8a341 100644
--- a/packages/PrintSpooler/res/values-af/strings.xml
+++ b/packages/PrintSpooler/res/values-af/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Meer inligting oor hierdie drukker"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Druktake wat tans loop"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Mislukte druktake"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Kon nie lêer skep nie"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Sommige drukdienste is gedeaktiveer"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Soek tans vir drukkers"</string>
diff --git a/packages/PrintSpooler/res/values-am/strings.xml b/packages/PrintSpooler/res/values-am/strings.xml
index b8e595e..832b855 100644
--- a/packages/PrintSpooler/res/values-am/strings.xml
+++ b/packages/PrintSpooler/res/values-am/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"ተጨማሪ የዚህ አታሚ መረጃ"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"የህትመት ስራዎችን በማሄድ ላይ"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"የህትመት ስራዎች አልተሳኩም"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ፋይል መፍጠር አልተቻለም"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"አንዳንድ የህትመት አገልግሎቶች ተሰናክለዋል"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"አታሚዎችን በመፈለግ ላይ"</string>
diff --git a/packages/PrintSpooler/res/values-ar/strings.xml b/packages/PrintSpooler/res/values-ar/strings.xml
index cbbd017..eab784d 100644
--- a/packages/PrintSpooler/res/values-ar/strings.xml
+++ b/packages/PrintSpooler/res/values-ar/strings.xml
@@ -65,10 +65,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"مزيد من المعلومات حول هذه الطابعة"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"مهام الطباعة الجارية"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"مهام الطباعة التي تعذَّر تنفيذها"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"تعذَّر إنشاء الملف"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"بعض خدمات الطباعة معطَّلة"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"البحث عن طابعات"</string>
diff --git a/packages/PrintSpooler/res/values-az/strings.xml b/packages/PrintSpooler/res/values-az/strings.xml
index e57a9e7..4193afc 100644
--- a/packages/PrintSpooler/res/values-az/strings.xml
+++ b/packages/PrintSpooler/res/values-az/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Bu printer haqqında daha ətraflı məlumat"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Çap işləri çalışır"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Çap işləri alınmadı"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Fayl yaradıla bilmədi"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Bəzi çap xidmətləri deaktiv edilib."</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Printer axtarılır"</string>
diff --git a/packages/PrintSpooler/res/values-b+sr+Latn/strings.xml b/packages/PrintSpooler/res/values-b+sr+Latn/strings.xml
index bfb05be5..49fe52b 100644
--- a/packages/PrintSpooler/res/values-b+sr+Latn/strings.xml
+++ b/packages/PrintSpooler/res/values-b+sr+Latn/strings.xml
@@ -62,10 +62,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Još informacija o ovom štampaču"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Aktivni zadaci štampanja"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Neuspeli zadaci štampanja"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Pravljenje datoteke nije uspelo"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Neke usluge štampanja su onemogućene"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Pretraga štampača"</string>
diff --git a/packages/PrintSpooler/res/values-be/strings.xml b/packages/PrintSpooler/res/values-be/strings.xml
index 44c35ed..c04756c 100644
--- a/packages/PrintSpooler/res/values-be/strings.xml
+++ b/packages/PrintSpooler/res/values-be/strings.xml
@@ -63,10 +63,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Больш падрабязная інфармацыя пра гэты прынтар"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Заданні друку, якія выконваюцца"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Заданні друку са збоямі"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Не ўдалося стварыць файл"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Некаторыя службы друку адключаны"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Пошук прынтараў"</string>
diff --git a/packages/PrintSpooler/res/values-bg/strings.xml b/packages/PrintSpooler/res/values-bg/strings.xml
index 4f1c598..d44b4ce 100644
--- a/packages/PrintSpooler/res/values-bg/strings.xml
+++ b/packages/PrintSpooler/res/values-bg/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Още информация за този принтер"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Активни задания за отпечатване"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Неуспешни задания за отпечатване"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Файлът не можа да бъде създаден"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Някои услуги за отпечатване са деактивирани"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Търсене на принтери"</string>
diff --git a/packages/PrintSpooler/res/values-bn/strings.xml b/packages/PrintSpooler/res/values-bn/strings.xml
index 33e4794..88ba6ee 100644
--- a/packages/PrintSpooler/res/values-bn/strings.xml
+++ b/packages/PrintSpooler/res/values-bn/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"এই মুদ্রকটির বিষয়ে আরো তথ্য"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"এগুলি প্রিন্ট হচ্ছে"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"এগুলি প্রিন্ট করা যায়নি"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ফাইল তৈরি করা গেল না"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"কিছু মুদ্রণ পরিষেবা অক্ষম করা আছে"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"মুদ্রকগুলি অনুসন্ধান করা হচ্ছে"</string>
diff --git a/packages/PrintSpooler/res/values-bs/strings.xml b/packages/PrintSpooler/res/values-bs/strings.xml
index f8e1d12..d3f1b80 100644
--- a/packages/PrintSpooler/res/values-bs/strings.xml
+++ b/packages/PrintSpooler/res/values-bs/strings.xml
@@ -62,10 +62,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Više informacija o ovom štampaču"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Tekući zadaci za štampanje"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Neizvršeni zadaci za štampanje"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Nije uspjelo kreiranje fajla"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Neke usluge za štampanje su isključene"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Traženje štampača"</string>
diff --git a/packages/PrintSpooler/res/values-ca/strings.xml b/packages/PrintSpooler/res/values-ca/strings.xml
index fa6cfeb..361e420 100644
--- a/packages/PrintSpooler/res/values-ca/strings.xml
+++ b/packages/PrintSpooler/res/values-ca/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Més informació sobre aquesta impressora"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Tasques d\'impressió actives"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Tasques d\'impressió amb problemes"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"No s\'ha pogut crear el fitxer"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Alguns serveis d\'impressió estan desactivats"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Cerca d\'impressores"</string>
diff --git a/packages/PrintSpooler/res/values-cs/strings.xml b/packages/PrintSpooler/res/values-cs/strings.xml
index 4647c1a..1f38e3c 100644
--- a/packages/PrintSpooler/res/values-cs/strings.xml
+++ b/packages/PrintSpooler/res/values-cs/strings.xml
@@ -63,10 +63,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Další informace o této tiskárně"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Spuštěné tiskové úlohy"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Nezdařené tiskové úlohy"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Soubor nelze vytvořit"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Některé tiskové služby nejsou aktivovány"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Vyhledávání tiskáren"</string>
diff --git a/packages/PrintSpooler/res/values-da/strings.xml b/packages/PrintSpooler/res/values-da/strings.xml
index f0a5be6..5f116dc 100644
--- a/packages/PrintSpooler/res/values-da/strings.xml
+++ b/packages/PrintSpooler/res/values-da/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Flere oplysninger om denne printer"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Aktive udskriftsjobs"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Mislykkede udskriftsjobs"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Filen kunne ikke oprettes"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Nogle udskrivningstjenester er deaktiveret"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Søger efter printere"</string>
diff --git a/packages/PrintSpooler/res/values-de/strings.xml b/packages/PrintSpooler/res/values-de/strings.xml
index b482350..c02e3c7 100644
--- a/packages/PrintSpooler/res/values-de/strings.xml
+++ b/packages/PrintSpooler/res/values-de/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Weitere Informationen über diesen Drucker"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Laufende Druckaufträge"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Fehlgeschlagene Druckaufträge"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Datei konnte nicht erstellt werden"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Einige Druckdienste sind deaktiviert"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Suche nach Druckern"</string>
diff --git a/packages/PrintSpooler/res/values-el/strings.xml b/packages/PrintSpooler/res/values-el/strings.xml
index ea1477e..9021f7c 100644
--- a/packages/PrintSpooler/res/values-el/strings.xml
+++ b/packages/PrintSpooler/res/values-el/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Περισσότερες πληροφορίες σχετικά με αυτόν τον εκτυπωτή"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Εργασίες εκτύπωσης σε εξέλιξη"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Αποτυχημένες εργασίες εκτύπωσης"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Δεν ήταν δυνατή η δημιουργία του αρχείου"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Ορισμένες υπηρ. εκτύπωσης είναι απενεργοποιημένες"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Αναζήτηση για εκτυπωτές"</string>
diff --git a/packages/PrintSpooler/res/values-en-rAU/strings.xml b/packages/PrintSpooler/res/values-en-rAU/strings.xml
index 5939182..7fbfeb3 100644
--- a/packages/PrintSpooler/res/values-en-rAU/strings.xml
+++ b/packages/PrintSpooler/res/values-en-rAU/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"More information about this printer"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Running print jobs"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Failed print jobs"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Could not create file"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Some print services are disabled"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Searching for printers"</string>
diff --git a/packages/PrintSpooler/res/values-en-rGB/strings.xml b/packages/PrintSpooler/res/values-en-rGB/strings.xml
index 5939182..7fbfeb3 100644
--- a/packages/PrintSpooler/res/values-en-rGB/strings.xml
+++ b/packages/PrintSpooler/res/values-en-rGB/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"More information about this printer"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Running print jobs"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Failed print jobs"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Could not create file"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Some print services are disabled"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Searching for printers"</string>
diff --git a/packages/PrintSpooler/res/values-en-rIN/strings.xml b/packages/PrintSpooler/res/values-en-rIN/strings.xml
index 5939182..7fbfeb3 100644
--- a/packages/PrintSpooler/res/values-en-rIN/strings.xml
+++ b/packages/PrintSpooler/res/values-en-rIN/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"More information about this printer"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Running print jobs"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Failed print jobs"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Could not create file"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Some print services are disabled"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Searching for printers"</string>
diff --git a/packages/PrintSpooler/res/values-es-rUS/strings.xml b/packages/PrintSpooler/res/values-es-rUS/strings.xml
index 7ee41ee..441ae73 100644
--- a/packages/PrintSpooler/res/values-es-rUS/strings.xml
+++ b/packages/PrintSpooler/res/values-es-rUS/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Más información sobre esta impresora"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Trabajos de impresión activos"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Trabajos de impresión con errores"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"No se pudo crear el archivo"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Hay servicios de impresión inhabilitados"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Buscando impresoras"</string>
diff --git a/packages/PrintSpooler/res/values-es/strings.xml b/packages/PrintSpooler/res/values-es/strings.xml
index f970b83..c1ff282 100644
--- a/packages/PrintSpooler/res/values-es/strings.xml
+++ b/packages/PrintSpooler/res/values-es/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Más información sobre esta impresora"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Trabajos de impresión activos"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Trabajos de impresión con errores"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"No se ha podido crear el archivo"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Algunos servicios de impresión están inhabilitados"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Buscando impresoras"</string>
diff --git a/packages/PrintSpooler/res/values-et/strings.xml b/packages/PrintSpooler/res/values-et/strings.xml
index 92979e9..eebc569 100644
--- a/packages/PrintSpooler/res/values-et/strings.xml
+++ b/packages/PrintSpooler/res/values-et/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Lisateave selle printeri kohta"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Toimivad printimistööd"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Ebaõnnestunud printimistööd"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Faili ei õnnestunud luua"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Mõned printimisteenused on keelatud"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Printerite otsimine"</string>
diff --git a/packages/PrintSpooler/res/values-eu/strings.xml b/packages/PrintSpooler/res/values-eu/strings.xml
index df34e73..8fdea1f 100644
--- a/packages/PrintSpooler/res/values-eu/strings.xml
+++ b/packages/PrintSpooler/res/values-eu/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Informazio gehiago inprimagailuari buruz"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Abian diren inprimatze-lanak"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Huts egin duten inprimatze-lanak"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Ezin izan da sortu fitxategia"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Desgaituta daude inprimatzeko zerbitzu batzuk"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Inprimagailuak bilatzen"</string>
diff --git a/packages/PrintSpooler/res/values-fa/strings.xml b/packages/PrintSpooler/res/values-fa/strings.xml
index 06cdc23..596947d 100644
--- a/packages/PrintSpooler/res/values-fa/strings.xml
+++ b/packages/PrintSpooler/res/values-fa/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"اطلاعات بیشتر درباره چاپگر"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"کارهای چاپی درحال اجرا"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"کارهای چاپی ناموفق"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"فایل ایجاد نشد"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"بعضی از خدمات چاپ غیرفعال هستند"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"درحال جستجوی چاپگرها"</string>
diff --git a/packages/PrintSpooler/res/values-fi/strings.xml b/packages/PrintSpooler/res/values-fi/strings.xml
index 53b9695..724d1d7 100644
--- a/packages/PrintSpooler/res/values-fi/strings.xml
+++ b/packages/PrintSpooler/res/values-fi/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Lisätietoja tästä tulostimesta"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Käynnissä olevat tulostustyöt"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Epäonnistuneet tulostustyöt"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Tiedoston luominen epäonnistui."</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Osa tulostuspalveluista on poistettu käytöstä."</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Etsitään tulostimia"</string>
diff --git a/packages/PrintSpooler/res/values-fr-rCA/strings.xml b/packages/PrintSpooler/res/values-fr-rCA/strings.xml
index a483b9a..9329d2a 100644
--- a/packages/PrintSpooler/res/values-fr-rCA/strings.xml
+++ b/packages/PrintSpooler/res/values-fr-rCA/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Plus d\'information sur cette imprimante"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Tâches d\'impression en cours"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Tâches d\'impression échouées"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Impossible de créer le fichier"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Certains services d\'impression sont désactivés"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Recherche d\'imprimantes en cours..."</string>
diff --git a/packages/PrintSpooler/res/values-fr/strings.xml b/packages/PrintSpooler/res/values-fr/strings.xml
index 56920a4..d2a477d 100644
--- a/packages/PrintSpooler/res/values-fr/strings.xml
+++ b/packages/PrintSpooler/res/values-fr/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Plus d\'informations sur cette imprimante"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Tâches d\'impression en cours"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Tâches d\'impression non abouties"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Impossible de créer le fichier"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Certains services d\'impression sont désactivés."</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Recherche d\'imprimantes en cours"</string>
diff --git a/packages/PrintSpooler/res/values-gl/strings.xml b/packages/PrintSpooler/res/values-gl/strings.xml
index 75df019..4019d8d 100644
--- a/packages/PrintSpooler/res/values-gl/strings.xml
+++ b/packages/PrintSpooler/res/values-gl/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Máis información sobre esta impresora"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Executando traballos de impresión"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Erro nos traballos de impresión"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Non se puido crear o arquivo"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Algúns servizos de impresión están desactivados"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Busca de impresoras"</string>
diff --git a/packages/PrintSpooler/res/values-gu/strings.xml b/packages/PrintSpooler/res/values-gu/strings.xml
index 292c4fa..f5d698d 100644
--- a/packages/PrintSpooler/res/values-gu/strings.xml
+++ b/packages/PrintSpooler/res/values-gu/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"આ પ્રિન્ટર વિશે વધુ માહિતી"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"ચાલી રહેલા છાપવાનાં કાર્યો"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"નિષ્ફળ થયેલ છાપવાના કાર્યો"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ફાઇલ બનાવી શક્યાં નથી"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"કેટલીક છાપવાની સેવાઓ અક્ષમ કરેલ છે"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"પ્રિન્ટર્સ માટે શોધી રહ્યું છે"</string>
diff --git a/packages/PrintSpooler/res/values-hi/strings.xml b/packages/PrintSpooler/res/values-hi/strings.xml
index 82dadcd..809431d 100644
--- a/packages/PrintSpooler/res/values-hi/strings.xml
+++ b/packages/PrintSpooler/res/values-hi/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"इस प्रिंटर के बारे में अधिक जानकारी"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"चल रहे प्रिंट कार्य"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"असफल रहे प्रिंट कार्य"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"फ़ाइल नहीं बनाई जा सकी"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"कुछ प्रिंट सेवाएं अक्षम हैं"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"प्रिंटर खोज रहा है"</string>
diff --git a/packages/PrintSpooler/res/values-hr/strings.xml b/packages/PrintSpooler/res/values-hr/strings.xml
index d657c51..217dbaa 100644
--- a/packages/PrintSpooler/res/values-hr/strings.xml
+++ b/packages/PrintSpooler/res/values-hr/strings.xml
@@ -62,10 +62,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Više informacija o ovom pisaču"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Zadaci ispisa u tijeku"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Neuspjeli zadaci ispisa"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Izrada datoteke nije uspjela"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Neke su usluge ispisa onemogućene"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Traženje pisača"</string>
diff --git a/packages/PrintSpooler/res/values-hu/strings.xml b/packages/PrintSpooler/res/values-hu/strings.xml
index 4b25171..b9a0d9d 100644
--- a/packages/PrintSpooler/res/values-hu/strings.xml
+++ b/packages/PrintSpooler/res/values-hu/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"További információ erről a nyomtatóról"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Folyamatban lévő nyomtatási feladatok"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Nem sikerült nyomtatási feladatok"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Nem sikerült létrehozni a fájlt"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Egyes nyomtatási szolgáltatások le vannak tiltva"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Nyomtatók keresése"</string>
diff --git a/packages/PrintSpooler/res/values-hy/strings.xml b/packages/PrintSpooler/res/values-hy/strings.xml
index 1162cb1..56045be 100644
--- a/packages/PrintSpooler/res/values-hy/strings.xml
+++ b/packages/PrintSpooler/res/values-hy/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Հավելյալ տեղեկություններ այս տպիչի մասին"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Ընթացիկ տպելու առաջադրանքները"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Ձախողված տպելու առաջադրանքները"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Չհաջողվեց ստեղծել ֆայլ"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Տպելու որոշ ծառայությունները կասեցված են"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Տպիչների որոնում"</string>
diff --git a/packages/PrintSpooler/res/values-in/strings.xml b/packages/PrintSpooler/res/values-in/strings.xml
index 9139f02..1e536f1 100644
--- a/packages/PrintSpooler/res/values-in/strings.xml
+++ b/packages/PrintSpooler/res/values-in/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Informasi selengkapnya tentang printer ini"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Menjalankan proses pencetakan"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Proses pencetakan yang gagal"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Tidak dapat membuat file"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Beberapa layanan cetak dinonaktifkan"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Mencari printer"</string>
diff --git a/packages/PrintSpooler/res/values-is/strings.xml b/packages/PrintSpooler/res/values-is/strings.xml
index 419ba60..eb7f01d 100644
--- a/packages/PrintSpooler/res/values-is/strings.xml
+++ b/packages/PrintSpooler/res/values-is/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Frekari upplýsingar um þennan prentara"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Prentverk í gangi"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Prentverk sem mistókust"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Ekki tókst að búa til skrá"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Hluti prentþjónustunnar er óvirkur"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Leitar að prentara"</string>
diff --git a/packages/PrintSpooler/res/values-it/strings.xml b/packages/PrintSpooler/res/values-it/strings.xml
index 84e3145..d898b1e 100644
--- a/packages/PrintSpooler/res/values-it/strings.xml
+++ b/packages/PrintSpooler/res/values-it/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Ulteriori informazioni su questa stampante"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Processi di stampa in esecuzione"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Processi di stampa non riusciti"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Impossibile creare il file"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Alcuni servizi di stampa sono disattivati"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Ricerca di stampanti"</string>
diff --git a/packages/PrintSpooler/res/values-iw/strings.xml b/packages/PrintSpooler/res/values-iw/strings.xml
index 6227f46..7448079 100644
--- a/packages/PrintSpooler/res/values-iw/strings.xml
+++ b/packages/PrintSpooler/res/values-iw/strings.xml
@@ -63,10 +63,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"מידע נוסף על מדפסת זו"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"עבודות הדפסה פועלות"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"עבודות הדפסה שנכשלו"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"לא ניתן היה ליצור קובץ"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"שירותי הדפסה מסוימים מושבתים"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"מחפש מדפסות"</string>
diff --git a/packages/PrintSpooler/res/values-ja/strings.xml b/packages/PrintSpooler/res/values-ja/strings.xml
index 7e6f5e4..8529606 100644
--- a/packages/PrintSpooler/res/values-ja/strings.xml
+++ b/packages/PrintSpooler/res/values-ja/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"このプリンタの詳細"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"アクティブな印刷ジョブ"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"エラーが発生した印刷ジョブ"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ファイルを作成できませんでした"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"一部の印刷サービスは無効になっています"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"プリンタの検索中"</string>
diff --git a/packages/PrintSpooler/res/values-ka/strings.xml b/packages/PrintSpooler/res/values-ka/strings.xml
index edfab76..f11c0e1 100644
--- a/packages/PrintSpooler/res/values-ka/strings.xml
+++ b/packages/PrintSpooler/res/values-ka/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> — <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"დამატებითი ინფორმაცია ამ პრინტერის შესახებ"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"ბეჭდვის გაშვებული დავალებები"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"ბეჭდვის შეუსრულებელი დავალებები"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ფაილი ვერ შეიქმნა"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"ბეჭდვის ზოგიერთი სერვისი გათიშულია"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"მიმდინარეობს პრინტერების ძიება"</string>
diff --git a/packages/PrintSpooler/res/values-kk/strings.xml b/packages/PrintSpooler/res/values-kk/strings.xml
index 59b0cd0..a822d1c 100644
--- a/packages/PrintSpooler/res/values-kk/strings.xml
+++ b/packages/PrintSpooler/res/values-kk/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Осы принтер туралы қосымша ақпарат"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Басып шығару тапсырмасы орындалуда"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Басып шығару тапсырмасы орындалмады"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Файл жасалмады"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Кейбір басып шығару қызметтері өшірілген."</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Принтерлерді іздеу"</string>
diff --git a/packages/PrintSpooler/res/values-km/strings.xml b/packages/PrintSpooler/res/values-km/strings.xml
index 8072600..2bc7baa 100644
--- a/packages/PrintSpooler/res/values-km/strings.xml
+++ b/packages/PrintSpooler/res/values-km/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"ព័ត៌មានបន្ថែមអំពីម៉ាស៊ីបោះពុម្ពនេះ"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"កំពុង​ដំណើរការ​កិច្ចការ​បោះពុម្ព"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"មិនអាច​ដំណើរការ​កិច្ចការ​បោះពុម្ព​បាន​ទេ"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"មិនអាចបង្កើតឯកសារបានទេ"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"សេវាកម្មបោះពុម្ពមួយចំនួនត្រូវបានបិទដំណើរការ"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"ស្វែងរក​ម៉ាស៊ីន​បោះពុម្ព"</string>
diff --git a/packages/PrintSpooler/res/values-kn/strings.xml b/packages/PrintSpooler/res/values-kn/strings.xml
index e01dcf7b..c8eaf6e 100644
--- a/packages/PrintSpooler/res/values-kn/strings.xml
+++ b/packages/PrintSpooler/res/values-kn/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"ಈ ಪ್ರಿಂಟರ್ ಬಗ್ಗೆ ಇನ್ನಷ್ಟು ಮಾಹಿತಿ"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"ಜಾರಿಯಲ್ಲಿರುವ ಮುದ್ರಣ ಕಾರ್ಯಗಳು"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"ವಿಫಲಗೊಂಡಿರುವ ಮುದ್ರಣ ಕಾರ್ಯಗಳು"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ಫೈಲ್‌ ರಚಿಸಲು ಸಾಧ್ಯವಾಗಲಿಲ್ಲ"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"ಕೆಲವು ಮುದ್ರಣ ಸೇವೆಗಳನ್ನು ನಿಷ್ಕ್ರಿಯಗೊಳಿಸಲಾಗಿದೆ"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"ಪ್ರಿಂಟರ್‌‌ಗಳಿಗಾಗಿ ಹುಡುಕಲಾಗುತ್ತಿದೆ"</string>
diff --git a/packages/PrintSpooler/res/values-ko/strings.xml b/packages/PrintSpooler/res/values-ko/strings.xml
index 36d25fb..8219b5b 100644
--- a/packages/PrintSpooler/res/values-ko/strings.xml
+++ b/packages/PrintSpooler/res/values-ko/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"이 프린터에 대한 정보 더보기"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"실행 중인 인쇄 작업"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"실패한 인쇄 작업"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"파일을 만들 수 없습니다."</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"프린트 서비스 일부가 사용 중지되었습니다."</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"프린터 검색 중"</string>
diff --git a/packages/PrintSpooler/res/values-ky/strings.xml b/packages/PrintSpooler/res/values-ky/strings.xml
index 06cf8df..655201e 100644
--- a/packages/PrintSpooler/res/values-ky/strings.xml
+++ b/packages/PrintSpooler/res/values-ky/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Бул принтер жөнүндө көбүрөөк маалымат"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Басып чыгаруу тапшырмалары аткарылууда"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Басып чыгарылбай калган тапшырмалар"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Файл түзүлбөй койду"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Басып чыгаруу кызматтарынын айрымы өчүрүлгөн"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Принтерлер изделүүдө"</string>
diff --git a/packages/PrintSpooler/res/values-lo/strings.xml b/packages/PrintSpooler/res/values-lo/strings.xml
index 79b4fff..cee6376 100644
--- a/packages/PrintSpooler/res/values-lo/strings.xml
+++ b/packages/PrintSpooler/res/values-lo/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"ຂໍ້ມູນເພີ່ມເຕີມກ່ຽວກັບເຄື່ອງພິມນີ້"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"ກຳລັງແລ່ນວຽກ"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"ພິມວຽກບໍ່ສຳເລັດ"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ບໍ່ສາມາດສ້າງໄຟລ໌ໄດ້"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"ບາງການບໍລິການພິມຖືກປິດການນຳໃຊ້"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"ກຳລັງຊອກຫາເຄື່ອງພິມ"</string>
diff --git a/packages/PrintSpooler/res/values-lt/strings.xml b/packages/PrintSpooler/res/values-lt/strings.xml
index b174341..9ed807f 100644
--- a/packages/PrintSpooler/res/values-lt/strings.xml
+++ b/packages/PrintSpooler/res/values-lt/strings.xml
@@ -63,10 +63,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"„<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g>“ – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Daugiau informacijos apie šį spausdintuvą"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Vykdomos spausdinimo užduotys"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Spausdinimo užduotys su triktimis"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Nepavyko sukurti failo"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Kai kurios spausdinimo paslaugos išjungtos"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Ieškoma spausdintuvų"</string>
diff --git a/packages/PrintSpooler/res/values-lv/strings.xml b/packages/PrintSpooler/res/values-lv/strings.xml
index 92b5d0e..bf9fb3b 100644
--- a/packages/PrintSpooler/res/values-lv/strings.xml
+++ b/packages/PrintSpooler/res/values-lv/strings.xml
@@ -62,10 +62,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> — <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Plašāka informācija par šo printeri"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Tiek veikti drukas darbi"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Neizdevās veikt drukas darbus"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Nevarēja izveidot failu."</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Daži drukas pakalpojumi ir atspējoti."</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Printeru meklēšana"</string>
diff --git a/packages/PrintSpooler/res/values-mk/strings.xml b/packages/PrintSpooler/res/values-mk/strings.xml
index 302f87f..11d7867 100644
--- a/packages/PrintSpooler/res/values-mk/strings.xml
+++ b/packages/PrintSpooler/res/values-mk/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Повеќе информации за овој печатач"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Тековни работи за печатење"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Неуспешни работи за печатење"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Не можеше да се создаде датотека"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Некои услуги за печатење се оневозможени"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Пребарување печатачи"</string>
diff --git a/packages/PrintSpooler/res/values-ml/strings.xml b/packages/PrintSpooler/res/values-ml/strings.xml
index e6add8c..05be7a7 100644
--- a/packages/PrintSpooler/res/values-ml/strings.xml
+++ b/packages/PrintSpooler/res/values-ml/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"ഈ പ്രിന്ററിനെ കുറിച്ചുള്ള കൂടുതൽ വിവരങ്ങൾ"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"നടന്നുകൊണ്ടിരിക്കുന്ന പ്രിന്റ് ജോലികൾ"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"പരാജയപ്പെട്ട പ്രിന്റ് ജോലികൾ"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ഫയൽ സൃഷ്ടിക്കാൻ കഴിഞ്ഞില്ല"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"ചില പ്രിന്റ് സേവനങ്ങൾ പ്രവർത്തനരഹിതമാക്കി"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"പ്രിന്ററുകൾക്കായി തിരയുന്നു"</string>
diff --git a/packages/PrintSpooler/res/values-mn/strings.xml b/packages/PrintSpooler/res/values-mn/strings.xml
index b7d33bb..fa99354 100644
--- a/packages/PrintSpooler/res/values-mn/strings.xml
+++ b/packages/PrintSpooler/res/values-mn/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Энэ хэвлэгчийн талаарх дэлгэрэнгүй мэдээлэл"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Хэвлэж байна"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Хэвлэж чадсангүй"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Файл үүсгэж чадсангүй"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Зарим хэвлэх үйлчилгээг идэвхгүй болгосон байна"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Принтер хайж байна"</string>
diff --git a/packages/PrintSpooler/res/values-mr/strings.xml b/packages/PrintSpooler/res/values-mr/strings.xml
index 482c085..8981cd8 100644
--- a/packages/PrintSpooler/res/values-mr/strings.xml
+++ b/packages/PrintSpooler/res/values-mr/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"या प्रिंटर विषयी अधिक माहिती"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"प्रिंट कार्ये चालवणे"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"अयशस्वी प्रिंट कार्ये"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"फाईल तयार करणेे शक्य झाले नाही"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"काही मुद्रण सेवा अक्षम केल्या आहेत"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"प्रिंटर शोधत आहे"</string>
diff --git a/packages/PrintSpooler/res/values-ms/strings.xml b/packages/PrintSpooler/res/values-ms/strings.xml
index 65f2622..75ff205 100644
--- a/packages/PrintSpooler/res/values-ms/strings.xml
+++ b/packages/PrintSpooler/res/values-ms/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Maklumat lanjut tentang pencetak ini"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Tugas cetak yang sedang berjalan"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Tugas cetak yang gagal"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Tidak dapat membuat fail"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Sesetengah perkhidmatan cetak dilumpuhkan"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Mencari pencetak"</string>
diff --git a/packages/PrintSpooler/res/values-my/strings.xml b/packages/PrintSpooler/res/values-my/strings.xml
index 87c0b88..c581e59 100644
--- a/packages/PrintSpooler/res/values-my/strings.xml
+++ b/packages/PrintSpooler/res/values-my/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"ဤပရင်တာ အကြောင်း ပိုမိုလေ့လာပါ"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"လုပ်နေဆဲ ပရင့်ထုတ်မှုများ"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"မအောင်မြင်သည့် ပရင့်ထုတ်မှုများ"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ဖိုင်အမည်ကို ထည့်၍မရပါ"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"အချို့ပုံနှိပ်ဝန်ဆောင်မှုများကို ပိတ်ထားပါသည်"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"စာထုတ်စက်များကို ရှာနေပါသည်"</string>
diff --git a/packages/PrintSpooler/res/values-nb/strings.xml b/packages/PrintSpooler/res/values-nb/strings.xml
index 884e873..6b04c2e 100644
--- a/packages/PrintSpooler/res/values-nb/strings.xml
+++ b/packages/PrintSpooler/res/values-nb/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Mer informasjon om denne printeren"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Utskriftsjobber som er i gang"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Mislykkede utskriftsjobber"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Kunne ikke opprette filen"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Noen utskriftstjenester er slått av"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Søker etter skrivere"</string>
diff --git a/packages/PrintSpooler/res/values-ne/strings.xml b/packages/PrintSpooler/res/values-ne/strings.xml
index e746b34..c5f5978 100644
--- a/packages/PrintSpooler/res/values-ne/strings.xml
+++ b/packages/PrintSpooler/res/values-ne/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"यस प्रिन्टरको बारेमा थप जानकारी"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"चलिरहेका छपाइका कार्यहरू"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"कार्यहरूलाई छाप्न सकिएन"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"फाइल सिर्जना गर्न सकिएन"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"केही मुद्रण सम्बन्धी सेवाहरूलाई असक्षम गरिएको छ"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"प्रिन्टरहरू खोज्दै"</string>
diff --git a/packages/PrintSpooler/res/values-nl/strings.xml b/packages/PrintSpooler/res/values-nl/strings.xml
index 7f41bb5..8048914 100644
--- a/packages/PrintSpooler/res/values-nl/strings.xml
+++ b/packages/PrintSpooler/res/values-nl/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Meer informatie over deze printer"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Actieve afdruktaken"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Mislukte afdruktaken"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Kan bestand niet maken"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Sommige afdrukservices zijn uitgeschakeld"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Printers zoeken"</string>
diff --git a/packages/PrintSpooler/res/values-pa/strings.xml b/packages/PrintSpooler/res/values-pa/strings.xml
index c5d9bf4..6fd021b 100644
--- a/packages/PrintSpooler/res/values-pa/strings.xml
+++ b/packages/PrintSpooler/res/values-pa/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"ਇਸ ਪ੍ਰਿੰਟਰ ਬਾਰੇ ਹੋਰ ਜਾਣਕਾਰੀ"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"ਚੱਲ ਰਹੇ ਪ੍ਰਿੰਟ ਕੰਮ"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"ਪ੍ਰਿੰਟ ਦੇ ਕੰਮ ਅਸਫਲ ਰਹੇ"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ਫ਼ਾਈਲ ਨੂੰ ਬਣਾਇਆ ਨਹੀਂ ਜਾ ਸਕਿਆ"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"ਕੁਝ ਪ੍ਰਿੰਟ ਸੇਵਾਵਾਂ ਅਯੋਗ ਬਣਾਈਆਂ ਗਈਆਂ ਹਨ"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"ਪ੍ਰਿੰਟਰ ਖੋਜ ਰਿਹਾ ਹੈ"</string>
diff --git a/packages/PrintSpooler/res/values-pl/strings.xml b/packages/PrintSpooler/res/values-pl/strings.xml
index 2f0f983..a960fe2 100644
--- a/packages/PrintSpooler/res/values-pl/strings.xml
+++ b/packages/PrintSpooler/res/values-pl/strings.xml
@@ -63,10 +63,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Więcej informacji o tej drukarce"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Realizowane zadania drukowania"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Nieudane zadania drukowania"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Nie udało się utworzyć pliku"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Niektóre usługi drukowania są wyłączone"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Szukanie drukarek"</string>
diff --git a/packages/PrintSpooler/res/values-pt-rBR/strings.xml b/packages/PrintSpooler/res/values-pt-rBR/strings.xml
index 5c68daf..a2f5ab0 100644
--- a/packages/PrintSpooler/res/values-pt-rBR/strings.xml
+++ b/packages/PrintSpooler/res/values-pt-rBR/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Mais informações sobre essa impressora"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Executando trabalhos de impressão"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Falha em trabalhos de impressão"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Não foi possível criar o arquivo"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Alguns serviços de impressão estão desativados"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Procurando impressoras"</string>
diff --git a/packages/PrintSpooler/res/values-pt-rPT/strings.xml b/packages/PrintSpooler/res/values-pt-rPT/strings.xml
index 86abd03..5da31bd 100644
--- a/packages/PrintSpooler/res/values-pt-rPT/strings.xml
+++ b/packages/PrintSpooler/res/values-pt-rPT/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Mais informações acerca desta impressora"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Tarefas de impressão em execução"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Tarefas de impressão falhadas"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Não foi possível criar o ficheiro"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Alguns serviços de impressão estão desativados"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"A procurar impressoras"</string>
diff --git a/packages/PrintSpooler/res/values-pt/strings.xml b/packages/PrintSpooler/res/values-pt/strings.xml
index 5c68daf..a2f5ab0 100644
--- a/packages/PrintSpooler/res/values-pt/strings.xml
+++ b/packages/PrintSpooler/res/values-pt/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Mais informações sobre essa impressora"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Executando trabalhos de impressão"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Falha em trabalhos de impressão"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Não foi possível criar o arquivo"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Alguns serviços de impressão estão desativados"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Procurando impressoras"</string>
diff --git a/packages/PrintSpooler/res/values-ro/strings.xml b/packages/PrintSpooler/res/values-ro/strings.xml
index 0530fc4..e0fb0b8 100644
--- a/packages/PrintSpooler/res/values-ro/strings.xml
+++ b/packages/PrintSpooler/res/values-ro/strings.xml
@@ -62,10 +62,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Mai multe informații despre această imprimantă"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Sarcini de printare în rulare"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Sarcini de printare nereușite"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Fișierul nu a putut fi creat"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Unele servicii de printare sunt dezactivate"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Se caută imprimante"</string>
diff --git a/packages/PrintSpooler/res/values-ru/strings.xml b/packages/PrintSpooler/res/values-ru/strings.xml
index 33f3166..e0aef53 100644
--- a/packages/PrintSpooler/res/values-ru/strings.xml
+++ b/packages/PrintSpooler/res/values-ru/strings.xml
@@ -63,10 +63,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Подробные сведения о принтере"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Активные задания печати"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Проблемные задания печати"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Не удалось создать файл"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Некоторые службы печати отключены"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Поиск принтеров…"</string>
diff --git a/packages/PrintSpooler/res/values-si/strings.xml b/packages/PrintSpooler/res/values-si/strings.xml
index 56eb909..fe17ee0 100644
--- a/packages/PrintSpooler/res/values-si/strings.xml
+++ b/packages/PrintSpooler/res/values-si/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"මෙම මුද්‍රණ යන්ත්‍රය ගැන තවත් තොරතුරු"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"කෙරෙමින් පවතින මුද්‍රණ කාර්යයන්"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"අසාර්තක වූ මුද්‍රණ කාර්යයන්"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ගොනුව සෑදීමට නොහැකි විය"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"සමහර මුද්‍රණ සේවා අබලයි"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"මුද්‍රණ යන්ත්‍ර සොයමින්"</string>
diff --git a/packages/PrintSpooler/res/values-sk/strings.xml b/packages/PrintSpooler/res/values-sk/strings.xml
index 4c480f0..17a029a 100644
--- a/packages/PrintSpooler/res/values-sk/strings.xml
+++ b/packages/PrintSpooler/res/values-sk/strings.xml
@@ -63,10 +63,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Ďalšie informácie o tejto tlačiarni"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Prebiehajúce tlačové úlohy"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Neúspešné tlačové úlohy"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Súbor nie je možné vytvoriť"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Niektoré tlačové služby sú zakázané"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Vyhľadávanie tlačiarní"</string>
diff --git a/packages/PrintSpooler/res/values-sl/strings.xml b/packages/PrintSpooler/res/values-sl/strings.xml
index 94202dd..3217756 100644
--- a/packages/PrintSpooler/res/values-sl/strings.xml
+++ b/packages/PrintSpooler/res/values-sl/strings.xml
@@ -63,10 +63,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Več informacij o tem tiskalniku"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Tiskalna opravila, ki se izvajajo"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Neuspela tiskalna opravila"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Datoteke ni bilo mogoče ustvariti"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Nekatere tiskalne storitve so onemogočene"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Iskanje tiskalnikov"</string>
diff --git a/packages/PrintSpooler/res/values-sq/strings.xml b/packages/PrintSpooler/res/values-sq/strings.xml
index c2d9480..0fd5cbb 100644
--- a/packages/PrintSpooler/res/values-sq/strings.xml
+++ b/packages/PrintSpooler/res/values-sq/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Më shumë informacione mbi këtë printer"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Punët e printimit që po ekzekutohen"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Punët e printimit që kanë dështuar"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Skedari nuk mund të krijohej"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Disa shërbime printimi janë çaktivizuar"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Po kërkon për printerë"</string>
diff --git a/packages/PrintSpooler/res/values-sr/strings.xml b/packages/PrintSpooler/res/values-sr/strings.xml
index 372986d..c20a5af 100644
--- a/packages/PrintSpooler/res/values-sr/strings.xml
+++ b/packages/PrintSpooler/res/values-sr/strings.xml
@@ -62,10 +62,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Још информација о овом штампачу"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Активни задаци штампања"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Неуспели задаци штампања"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Прављење датотеке није успело"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Неке услуге штампања су онемогућене"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Претрага штампача"</string>
diff --git a/packages/PrintSpooler/res/values-sv/strings.xml b/packages/PrintSpooler/res/values-sv/strings.xml
index a769a6e..9dac4cc 100644
--- a/packages/PrintSpooler/res/values-sv/strings.xml
+++ b/packages/PrintSpooler/res/values-sv/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Mer information om den här skrivaren"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Utskriftsjobb som pågår"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Utskriftsjobb som misslyckats"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Det gick inte att skapa filen"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Några utskriftstjänster har inaktiverats"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Söker efter skrivare"</string>
diff --git a/packages/PrintSpooler/res/values-sw/strings.xml b/packages/PrintSpooler/res/values-sw/strings.xml
index 2bee953..8aaf6f4 100644
--- a/packages/PrintSpooler/res/values-sw/strings.xml
+++ b/packages/PrintSpooler/res/values-sw/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Maelezo zaidi kuhusu printa hii"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Kazi ambazo zinachapishwa"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Kazi ambazo hazikuchapishwa"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Imeshindwa kuunda faili"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Baadhi ya huduma za uchapishaji haziruhusiwi"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Inatafuta printa"</string>
diff --git a/packages/PrintSpooler/res/values-ta/strings.xml b/packages/PrintSpooler/res/values-ta/strings.xml
index 124a1e6..47faf17 100644
--- a/packages/PrintSpooler/res/values-ta/strings.xml
+++ b/packages/PrintSpooler/res/values-ta/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"இந்தப் பிரிண்டர் பற்றிய கூடுதல் தகவல்"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"இயக்கத்திலுள்ள அச்சுப் பணிகள்"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"தோல்வியடைந்த அச்சுப் பணிகள்"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"கோப்பை உருவாக்க முடியவில்லை"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"சில அச்சுப் பொறிகள் முடக்கப்பட்டன"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"அச்சுப்பொறிகளைத் தேடுகிறது"</string>
diff --git a/packages/PrintSpooler/res/values-te/strings.xml b/packages/PrintSpooler/res/values-te/strings.xml
index f07f8f0..e5a0879 100644
--- a/packages/PrintSpooler/res/values-te/strings.xml
+++ b/packages/PrintSpooler/res/values-te/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"ఈ ప్రింటర్ గురించి మరింత సమాచారం"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"జరుగుతున్న ముద్రణలు"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"విఫలమైన ముద్రణలు"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ఫైల్‌ను సృష్టించలేకపోయాము"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"కొన్ని ముద్రణ సేవలు నిలిపివేయబడ్డాయి"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"ప్రింటర్‌ల కోసం శోధిస్తోంది"</string>
diff --git a/packages/PrintSpooler/res/values-th/strings.xml b/packages/PrintSpooler/res/values-th/strings.xml
index c230cac..5b73fa9 100644
--- a/packages/PrintSpooler/res/values-th/strings.xml
+++ b/packages/PrintSpooler/res/values-th/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"ข้อมูลเพิ่มเติมเกี่ยวกับเครื่องพิมพ์นี้"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"กำลังดำเนินการงานพิมพ์"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"งานพิมพ์ล้มเหลว"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"ไม่สามารถสร้างไฟล์ได้"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"บริการพิมพ์บางอย่างปิดใช้อยู่"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"กำลังค้นหาเครื่องพิมพ์"</string>
diff --git a/packages/PrintSpooler/res/values-tl/strings.xml b/packages/PrintSpooler/res/values-tl/strings.xml
index deafac3..ffc7738 100644
--- a/packages/PrintSpooler/res/values-tl/strings.xml
+++ b/packages/PrintSpooler/res/values-tl/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Higit pang impormasyon tungkol sa printer na ito"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Mga pag-print na walang naging problema"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Mga hindi matagumpay na pag-print"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Hindi makagawa ng file"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Naka-disable ang ilang serbisyo sa pag-print"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Naghahanap ng mga printer"</string>
diff --git a/packages/PrintSpooler/res/values-tr/strings.xml b/packages/PrintSpooler/res/values-tr/strings.xml
index b21dde2..3ff3f38 100644
--- a/packages/PrintSpooler/res/values-tr/strings.xml
+++ b/packages/PrintSpooler/res/values-tr/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Bu yazıcıyla ilgili daha fazla bilgi"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Devam eden yazdırma işleri"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Başarısız yazdırma işleri"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Dosya oluşturulamadı"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Bazı yazdırma hizmetleri devre dışı bırakıldı"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Yazıcılar aranıyor"</string>
diff --git a/packages/PrintSpooler/res/values-uk/strings.xml b/packages/PrintSpooler/res/values-uk/strings.xml
index 1e145d6..b5d426e 100644
--- a/packages/PrintSpooler/res/values-uk/strings.xml
+++ b/packages/PrintSpooler/res/values-uk/strings.xml
@@ -63,10 +63,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Докладніше про цей принтер"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Активні завдання друку"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Невиконані завдання друку"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Не вдалося створити файл"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Деякі служби друку вимкнено"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Пошук принтерів"</string>
diff --git a/packages/PrintSpooler/res/values-ur/strings.xml b/packages/PrintSpooler/res/values-ur/strings.xml
index 10938c8..026a41e 100644
--- a/packages/PrintSpooler/res/values-ur/strings.xml
+++ b/packages/PrintSpooler/res/values-ur/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"اس پرنٹر کے بارے میں مزید معلومات"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"چلنے والے پرنٹ جابز"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"ناکام پرنٹ جابز"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"فائل تخلیق نہیں ہو سکی"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"پرنٹ کی کچھ سروسز غیر فعال ہیں"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"پرنٹرز تلاش کر رہا ہے"</string>
diff --git a/packages/PrintSpooler/res/values-uz/strings.xml b/packages/PrintSpooler/res/values-uz/strings.xml
index 8e5f807..8921f8e 100644
--- a/packages/PrintSpooler/res/values-uz/strings.xml
+++ b/packages/PrintSpooler/res/values-uz/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> – <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Printer haqida batafsil ma’lumot"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Faol chop etish vazifalari"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Muammoli chop etish vazifalari"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Faylni yaratib bo‘lmadi"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Ayrim chop etish xizmatlari o‘chirib qo‘yilgan"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Printerlar qidirilmoqda"</string>
diff --git a/packages/PrintSpooler/res/values-vi/strings.xml b/packages/PrintSpooler/res/values-vi/strings.xml
index 627e858..a181424 100644
--- a/packages/PrintSpooler/res/values-vi/strings.xml
+++ b/packages/PrintSpooler/res/values-vi/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Thông tin khác về máy in này"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Lệnh in đang chạy"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Lệnh in bị lỗi"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Không thể tạo tệp"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Một số dịch vụ in đã bị tắt"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Đang tìm kiếm máy in"</string>
diff --git a/packages/PrintSpooler/res/values-zh-rCN/strings.xml b/packages/PrintSpooler/res/values-zh-rCN/strings.xml
index e657093..54e88c0 100644
--- a/packages/PrintSpooler/res/values-zh-rCN/strings.xml
+++ b/packages/PrintSpooler/res/values-zh-rCN/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"此打印机的详细信息"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"进行中的打印作业"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"失败的打印作业"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"无法创建文件"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"部分打印服务已停用"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"正在搜索打印机"</string>
diff --git a/packages/PrintSpooler/res/values-zh-rHK/strings.xml b/packages/PrintSpooler/res/values-zh-rHK/strings.xml
index 4a3002e..9a98cee 100644
--- a/packages/PrintSpooler/res/values-zh-rHK/strings.xml
+++ b/packages/PrintSpooler/res/values-zh-rHK/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"此打印機詳情"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"正在執行列印工作"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"列印工作失敗"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"無法建立檔案"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"部分列印服務已停用"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"正在搜尋打印機"</string>
diff --git a/packages/PrintSpooler/res/values-zh-rTW/strings.xml b/packages/PrintSpooler/res/values-zh-rTW/strings.xml
index 946a31d..f159605 100644
--- a/packages/PrintSpooler/res/values-zh-rTW/strings.xml
+++ b/packages/PrintSpooler/res/values-zh-rTW/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"查看這台印表機的詳細資訊"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"執行中的列印工作"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"失敗的列印工作"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"無法建立檔案"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"已停用部分列印服務"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"正在搜尋印表機"</string>
diff --git a/packages/PrintSpooler/res/values-zu/strings.xml b/packages/PrintSpooler/res/values-zu/strings.xml
index 1f505ce..9b1d5bb 100644
--- a/packages/PrintSpooler/res/values-zu/strings.xml
+++ b/packages/PrintSpooler/res/values-zu/strings.xml
@@ -61,10 +61,8 @@
     </plurals>
     <string name="printer_extended_description_template" msgid="1366699227703381874">"<xliff:g id="PRINT_SERVICE_LABEL">%1$s</xliff:g> - <xliff:g id="PRINTER_DESCRIPTION">%2$s</xliff:g>"</string>
     <string name="printer_info_desc" msgid="7181988788991581654">"Olunye ulwazi mayelana nale phrinta"</string>
-    <!-- no translation found for notification_channel_progress (872788690775721436) -->
-    <skip />
-    <!-- no translation found for notification_channel_failure (9042250774797916414) -->
-    <skip />
+    <string name="notification_channel_progress" msgid="872788690775721436">"Yenza imisebenzi yokuphrinta"</string>
+    <string name="notification_channel_failure" msgid="9042250774797916414">"Imisebenzi yokuphrinta ehlulekile"</string>
     <string name="could_not_create_file" msgid="3425025039427448443">"Ayikwazanga ukufala ifayela"</string>
     <string name="print_services_disabled_toast" msgid="9089060734685174685">"Amanye amasevisi okuphrinta akhutshaziwe"</string>
     <string name="print_searching_for_printers" msgid="6550424555079932867">"Isesha amaphrinta"</string>
diff --git a/packages/SystemUI/plugin/src/com/android/systemui/plugins/statusbar/NotificationMenuRowPlugin.java b/packages/SystemUI/plugin/src/com/android/systemui/plugins/statusbar/NotificationMenuRowPlugin.java
index 8dde357..4a7d0fd 100644
--- a/packages/SystemUI/plugin/src/com/android/systemui/plugins/statusbar/NotificationMenuRowPlugin.java
+++ b/packages/SystemUI/plugin/src/com/android/systemui/plugins/statusbar/NotificationMenuRowPlugin.java
@@ -37,7 +37,7 @@
 public interface NotificationMenuRowPlugin extends Plugin {
 
     public static final String ACTION = "com.android.systemui.action.PLUGIN_NOTIFICATION_MENU_ROW";
-    public static final int VERSION = 1;
+    public static final int VERSION = 2;
 
     @ProvidesInterface(version = OnMenuEventListener.VERSION)
     public interface OnMenuEventListener {
@@ -89,6 +89,8 @@
 
     public void onHeightUpdate();
 
+    public void onNotificationUpdated();
+
     public boolean onTouchEvent(View view, MotionEvent ev, float velocity);
 
     public default boolean useDefaultMenuItems() {
diff --git a/packages/SystemUI/res/drawable/qs_dual_tile_caret.xml b/packages/SystemUI/res/drawable/qs_dual_tile_caret.xml
index 0b2bd22..e27bc7a 100644
--- a/packages/SystemUI/res/drawable/qs_dual_tile_caret.xml
+++ b/packages/SystemUI/res/drawable/qs_dual_tile_caret.xml
@@ -14,8 +14,8 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="24.0dp"
-        android:height="24.0dp"
+        android:width="18.0dp"
+        android:height="18.0dp"
         android:viewportWidth="48.0"
         android:viewportHeight="48.0">
 
diff --git a/packages/SystemUI/res/layout/pip_menu_activity.xml b/packages/SystemUI/res/layout/pip_menu_activity.xml
index 44ced17..8c66bab 100644
--- a/packages/SystemUI/res/layout/pip_menu_activity.xml
+++ b/packages/SystemUI/res/layout/pip_menu_activity.xml
@@ -26,16 +26,6 @@
       android:layout_height="match_parent"
       android:forceHasOverlappingRendering="false">
 
-      <ImageView
-          android:id="@+id/dismiss"
-          android:layout_width="@dimen/pip_action_size"
-          android:layout_height="@dimen/pip_action_size"
-          android:layout_gravity="top|end"
-          android:padding="@dimen/pip_action_padding"
-          android:contentDescription="@string/pip_phone_close"
-          android:src="@drawable/ic_close_white"
-          android:background="?android:selectableItemBackgroundBorderless" />
-
       <!-- The margins for this container is calculated in the code depending on whether the
            actions_container is visible. -->
       <FrameLayout
@@ -67,4 +57,15 @@
               android:showDividers="middle" />
       </FrameLayout>
   </FrameLayout>
+
+  <ImageView
+      android:id="@+id/dismiss"
+      android:layout_width="@dimen/pip_action_size"
+      android:layout_height="@dimen/pip_action_size"
+      android:layout_gravity="top|end"
+      android:padding="@dimen/pip_action_padding"
+      android:contentDescription="@string/pip_phone_close"
+      android:src="@drawable/ic_close_white"
+      android:background="?android:selectableItemBackgroundBorderless" />
+
 </FrameLayout>
diff --git a/packages/SystemUI/res/layout/qs_tile_label.xml b/packages/SystemUI/res/layout/qs_tile_label.xml
index 64caefd..9b53a97 100644
--- a/packages/SystemUI/res/layout/qs_tile_label.xml
+++ b/packages/SystemUI/res/layout/qs_tile_label.xml
@@ -21,7 +21,7 @@
     android:clipChildren="false"
     android:clipToPadding="false"
     android:minHeight="48dp"
-    android:paddingTop="8dp">
+    android:paddingTop="12dp">
     <LinearLayout
         android:id="@+id/label_group"
         android:layout_width="wrap_content"
@@ -31,6 +31,10 @@
         android:clipChildren="false"
         android:clipToPadding="false"
         android:orientation="horizontal">
+        <Space
+            android:id="@+id/expand_space"
+            android:layout_width="22dp"
+            android:layout_height="0dp" />
 
         <TextView
             android:id="@+id/tile_label"
@@ -44,14 +48,6 @@
             android:textAppearance="@style/TextAppearance.QS.TileLabel"
             android:textColor="?android:attr/textColorPrimary"/>
 
-        <ImageView
-            android:id="@+id/expand_indicator"
-            android:layout_marginStart="4dp"
-            android:layout_width="12dp"
-            android:layout_height="match_parent"
-            android:src="@drawable/qs_dual_tile_caret"
-            android:tint="?android:attr/textColorPrimary" />
-
         <ImageView android:id="@+id/restricted_padlock"
             android:layout_width="@dimen/qs_tile_text_size"
             android:layout_height="match_parent"
@@ -60,6 +56,14 @@
             android:layout_marginLeft="@dimen/restricted_padlock_pading"
             android:scaleType="centerInside"
             android:visibility="gone" />
+
+        <ImageView
+            android:id="@+id/expand_indicator"
+            android:layout_marginStart="4dp"
+            android:layout_width="18dp"
+            android:layout_height="match_parent"
+            android:src="@drawable/qs_dual_tile_caret"
+            android:tint="?android:attr/textColorPrimary" />
     </LinearLayout>
 
     <TextView
@@ -85,6 +89,7 @@
         android:layout_alignStart="@id/label_group"
         android:layout_alignEnd="@id/label_group"
         android:layout_below="@id/label_group"
+        android:visibility="gone"
         android:alpha="?android:attr/disabledAlpha"
         android:background="?android:attr/colorForeground"/>
 
diff --git a/packages/SystemUI/res/layout/zen_mode_panel.xml b/packages/SystemUI/res/layout/zen_mode_panel.xml
index c0be676..8707840 100644
--- a/packages/SystemUI/res/layout/zen_mode_panel.xml
+++ b/packages/SystemUI/res/layout/zen_mode_panel.xml
@@ -18,104 +18,155 @@
 <com.android.systemui.volume.ZenModePanel xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/zen_mode_panel"
     android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:clipChildren="false"
-    android:orientation="vertical" >
-
-    <com.android.systemui.volume.SegmentedButtons
-        android:id="@+id/zen_buttons"
-        android:background="@drawable/segmented_buttons_background"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_marginStart="16dp"
-        android:layout_marginEnd="16dp"
-        android:layout_marginTop="8dp"
-        android:layout_marginBottom="8dp" />
-
-    <RelativeLayout
-        android:id="@+id/zen_introduction"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_marginStart="16dp"
-        android:layout_marginEnd="16dp"
-        android:paddingTop="8dp"
-        android:paddingBottom="8dp"
-        android:background="@drawable/zen_introduction_message_background"
-        android:theme="@*android:style/ThemeOverlay.DeviceDefault.Accent.Light">
-
-        <ImageView
-            android:id="@+id/zen_introduction_confirm"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_marginEnd="8dp"
-            android:layout_alignParentEnd="true"
-            android:background="@drawable/btn_borderless_rect"
-            android:clickable="true"
-            android:contentDescription="@string/accessibility_desc_close"
-            android:scaleType="center"
-            android:src="@drawable/ic_close"
-            android:tint="@android:color/white" />
-
-        <TextView
-            android:id="@+id/zen_introduction_message"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:layout_marginTop="12dp"
-            android:layout_marginStart="24dp"
-            android:textDirection="locale"
-            android:lineSpacingMultiplier="1.20029"
-            android:layout_toStartOf="@id/zen_introduction_confirm"
-            android:textAppearance="@style/TextAppearance.QS.Introduction" />
-
-        <TextView
-            android:id="@+id/zen_introduction_customize"
-            style="@style/QSBorderlessButton"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_alignParentEnd="true"
-            android:layout_marginEnd="12dp"
-            android:layout_below="@id/zen_introduction_message"
-            android:clickable="true"
-            android:focusable="true"
-            android:text="@string/zen_priority_customize_button"
-            android:textAppearance="@style/TextAppearance.QS.DetailButton.White" />
-
-        <View
-            android:layout_width="0dp"
-            android:layout_height="16dp"
-            android:layout_below="@id/zen_introduction_message"
-            android:layout_alignParentEnd="true" />
-
-    </RelativeLayout>
+    android:layout_height="match_parent"
+    android:clipChildren="false" >
 
     <LinearLayout
-        android:id="@+id/zen_conditions"
+        android:id="@+id/edit_container"
         android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_marginTop="8dp"
-        android:layout_marginEnd="4dp"
-        android:layout_marginStart="4dp"
-        android:paddingBottom="@dimen/zen_mode_condition_detail_bottom_padding"
-        android:orientation="horizontal" >
+        android:layout_height="match_parent"
+        android:background="?android:attr/colorPrimary"
+        android:clipChildren="false"
+        android:orientation="vertical">
+
+        <com.android.systemui.volume.SegmentedButtons
+            android:id="@+id/zen_buttons"
+            android:background="@drawable/segmented_buttons_background"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginStart="16dp"
+            android:layout_marginEnd="16dp"
+            android:layout_marginTop="8dp"
+            android:layout_marginBottom="8dp" />
+
+        <RelativeLayout
+            android:id="@+id/zen_introduction"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginStart="16dp"
+            android:layout_marginEnd="16dp"
+            android:paddingTop="8dp"
+            android:paddingBottom="8dp"
+            android:background="@drawable/zen_introduction_message_background"
+            android:theme="@*android:style/ThemeOverlay.DeviceDefault.Accent.Light">
+
+            <ImageView
+                android:id="@+id/zen_introduction_confirm"
+                android:layout_width="48dp"
+                android:layout_height="48dp"
+                android:layout_marginEnd="8dp"
+                android:layout_alignParentEnd="true"
+                android:background="@drawable/btn_borderless_rect"
+                android:clickable="true"
+                android:contentDescription="@string/accessibility_desc_close"
+                android:scaleType="center"
+                android:src="@drawable/ic_close"
+                android:tint="@android:color/white" />
+
+            <TextView
+                android:id="@+id/zen_introduction_message"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="12dp"
+                android:layout_marginStart="24dp"
+                android:textDirection="locale"
+                android:lineSpacingMultiplier="1.20029"
+                android:layout_toStartOf="@id/zen_introduction_confirm"
+                android:textAppearance="@style/TextAppearance.QS.Introduction" />
+
+            <TextView
+                android:id="@+id/zen_introduction_customize"
+                style="@style/QSBorderlessButton"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_alignParentEnd="true"
+                android:layout_marginEnd="12dp"
+                android:layout_below="@id/zen_introduction_message"
+                android:clickable="true"
+                android:focusable="true"
+                android:text="@string/zen_priority_customize_button"
+                android:textAppearance="@style/TextAppearance.QS.DetailButton.White" />
+
+            <View
+                android:layout_width="0dp"
+                android:layout_height="16dp"
+                android:layout_below="@id/zen_introduction_message"
+                android:layout_alignParentEnd="true" />
+
+        </RelativeLayout>
+
+        <LinearLayout
+            android:id="@+id/zen_conditions"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="8dp"
+            android:layout_marginEnd="4dp"
+            android:layout_marginStart="4dp"
+            android:paddingBottom="@dimen/zen_mode_condition_detail_bottom_padding"
+            android:orientation="horizontal" >
             <RadioGroup
-                    android:id="@+id/zen_radio_buttons"
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content" />
+                android:id="@+id/zen_radio_buttons"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content" />
             <LinearLayout
-                    android:id="@+id/zen_radio_buttons_content"
-                    android:layout_width="fill_parent"
-                    android:layout_height="fill_parent"
-                    android:orientation="vertical"/>
+                android:id="@+id/zen_radio_buttons_content"
+                android:layout_width="fill_parent"
+                android:layout_height="fill_parent"
+                android:orientation="vertical"/>
+        </LinearLayout>
+
+        <TextView
+            android:id="@+id/zen_alarm_warning"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginStart="18dp"
+            android:layout_marginEnd="16dp"
+            android:textDirection="locale"
+            android:lineSpacingMultiplier="1.20029"
+            android:textAppearance="@style/TextAppearance.QS.Warning" />
     </LinearLayout>
 
-    <TextView
-        android:id="@+id/zen_alarm_warning"
+    <LinearLayout
+        android:id="@android:id/empty"
         android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_marginStart="18dp"
+        android:layout_height="match_parent"
+        android:layout_gravity="center"
+        android:background="?android:attr/colorPrimary"
+        android:gravity="center"
+        android:orientation="vertical">
+
+        <ImageView
+            android:id="@android:id/icon"
+            android:layout_width="56dp"
+            android:layout_height="56dp"
+            android:alpha="?android:attr/disabledAlpha"
+            android:tint="?android:attr/colorForeground" />
+
+        <TextView
+            android:id="@android:id/title"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="20dp"
+            android:textAppearance="@style/TextAppearance.QS.DetailEmpty"/>
+    </LinearLayout>
+
+    <LinearLayout
+        android:id="@+id/auto_rule"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:background="?android:attr/colorPrimary"
+        android:layout_marginStart="16dp"
         android:layout_marginEnd="16dp"
-        android:textDirection="locale"
-        android:lineSpacingMultiplier="1.20029"
-        android:textAppearance="@style/TextAppearance.QS.Warning" />
+        android:layout_marginTop="16dp"
+        android:layout_marginBottom="8dp"
+        android:orientation="vertical">
+
+        <TextView
+            android:id="@android:id/title"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:textAppearance="@style/TextAppearance.QS.DetailItemPrimary"/>
+
+    </LinearLayout>
 
 </com.android.systemui.volume.ZenModePanel>
diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml
index 809648a..6837340 100644
--- a/packages/SystemUI/res/values/dimens.xml
+++ b/packages/SystemUI/res/values/dimens.xml
@@ -218,8 +218,8 @@
     <!-- The size of the gesture span needed to activate the "pull" notification expansion -->
     <dimen name="pull_span_min">25dp</dimen>
 
-    <dimen name="qs_tile_height">88dp</dimen>
-    <dimen name="qs_tile_margin">28dp</dimen>
+    <dimen name="qs_tile_height">106dp</dimen>
+    <dimen name="qs_tile_margin">19dp</dimen>
     <dimen name="qs_tile_margin_top">16dp</dimen>
     <dimen name="qs_quick_tile_size">48dp</dimen>
     <dimen name="qs_quick_tile_padding">12dp</dimen>
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index 7bd9526..d68487c 100644
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -1975,13 +1975,13 @@
     <string name="dnd_is_off">Do Not Disturb is off</string>
 
     <!-- Prompt for when Do not disturb is on from automatic rule in QS [CHAR LIMIT=NONE] -->
-    <string name="qs_dnd_prompt_auto_rule">Do Not Disturb was turned on by an automatic rule (<xliff:g name="rule">%s</xliff:g>). Keep current settings?</string>
+    <string name="qs_dnd_prompt_auto_rule">Do Not Disturb was turned on by an automatic rule (<xliff:g name="rule">%s</xliff:g>).</string>
 
     <!-- Prompt for when Do not disturb is on from app in QS [CHAR LIMIT=NONE] -->
-    <string name="qs_dnd_prompt_app">Do Not Disturb was turned on by an app (<xliff:g name="app">%s</xliff:g>). Keep current settings?</string>
+    <string name="qs_dnd_prompt_app">Do Not Disturb was turned on by an app (<xliff:g name="app">%s</xliff:g>).</string>
 
     <!-- Prompt for when Do not disturb is on from automatic rule or app in QS [CHAR LIMIT=NONE] -->
-    <string name="qs_dnd_prompt_auto_rule_app">Do Not Disturb was turned on by an automatic rule or app. Keep current settings?</string>
+    <string name="qs_dnd_prompt_auto_rule_app">Do Not Disturb was turned on by an automatic rule or app.</string>
 
     <!-- Description of Do Not Disturb option in QS that ends at the specified time[CHAR LIMIT=20] -->
     <string name="qs_dnd_until">Until <xliff:g name="time">%s</xliff:g></string>
diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
index fbb075a..0eb469f 100644
--- a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
+++ b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
@@ -158,6 +158,7 @@
      * the main process.
      * <p>This method must only be called from the main thread.</p>
      */
+
     public void startServicesIfNeeded() {
         startServicesIfNeeded(SERVICES);
     }
diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java
index 4e7cf72..c06e56a 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java
@@ -21,11 +21,16 @@
 import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_CONTROLLER_MESSENGER;
 import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_DISMISS_FRACTION;
 import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_MOVEMENT_BOUNDS;
-import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_SHOW_MENU;
+import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_MENU_STATE;
 import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_STACK_BOUNDS;
 
+import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_NONE;
+import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_CLOSE;
+import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_FULL;
+
 import android.animation.Animator;
 import android.animation.AnimatorListenerAdapter;
+import android.animation.AnimatorSet;
 import android.animation.ObjectAnimator;
 import android.animation.ValueAnimator;
 import android.annotation.Nullable;
@@ -85,7 +90,7 @@
 
     private static final float DISABLED_ACTION_ALPHA = 0.54f;
 
-    private boolean mMenuVisible;
+    private int mMenuState;
     private boolean mAllowMenuTimeout = true;
 
     private final List<RemoteAction> mActions = new ArrayList<>();
@@ -98,7 +103,8 @@
     private ImageView mExpandButton;
     private int mBetweenActionPaddingLand;
 
-    private ObjectAnimator mMenuContainerAnimator;
+    private AnimatorSet mMenuContainerAnimator;
+
     private ValueAnimator.AnimatorUpdateListener mMenuBgUpdateListener =
             new ValueAnimator.AnimatorUpdateListener() {
                 @Override
@@ -119,7 +125,8 @@
             switch (msg.what) {
                 case MESSAGE_SHOW_MENU: {
                     final Bundle data = (Bundle) msg.obj;
-                    showMenu(data.getParcelable(EXTRA_STACK_BOUNDS),
+                    showMenu(data.getInt(EXTRA_MENU_STATE),
+                            data.getParcelable(EXTRA_STACK_BOUNDS),
                             data.getParcelable(EXTRA_MOVEMENT_BOUNDS),
                             data.getBoolean(EXTRA_ALLOW_TIMEOUT));
                     break;
@@ -170,9 +177,14 @@
         mMenuContainer = findViewById(R.id.menu_container);
         mMenuContainer.setAlpha(0);
         mMenuContainer.setOnClickListener((v) -> {
-            expandPip();
+            if (mMenuState == MENU_STATE_CLOSE) {
+                showPipMenu();
+            } else {
+                expandPip();
+            }
         });
         mDismissButton = findViewById(R.id.dismiss);
+        mDismissButton.setAlpha(0);
         mDismissButton.setOnClickListener((v) -> {
             dismissPip();
         });
@@ -236,7 +248,8 @@
                 break;
             case MotionEvent.ACTION_MOVE:
                 mDownDelta.set(ev.getX() - mDownPosition.x, ev.getY() - mDownPosition.y);
-                if (mDownDelta.length() > mViewConfig.getScaledTouchSlop() && mMenuVisible) {
+                if (mDownDelta.length() > mViewConfig.getScaledTouchSlop()
+                        && mMenuState != MENU_STATE_NONE) {
                     // Restore the input consumer and let that drive the movement of this menu
                     notifyRegisterInputConsumer();
                     cancelDelayedFinish();
@@ -259,17 +272,28 @@
         // Do nothing
     }
 
-    private void showMenu(Rect stackBounds, Rect movementBounds, boolean allowMenuTimeout) {
+    private void showMenu(int menuState, Rect stackBounds, Rect movementBounds,
+            boolean allowMenuTimeout) {
         mAllowMenuTimeout = allowMenuTimeout;
-        if (!mMenuVisible) {
+        if (mMenuState != menuState) {
+            cancelDelayedFinish();
             updateActionViews(stackBounds);
             if (mMenuContainerAnimator != null) {
                 mMenuContainerAnimator.cancel();
             }
-            notifyMenuVisibility(true);
+            notifyMenuStateChange(menuState);
             updateExpandButtonFromBounds(stackBounds, movementBounds);
-            mMenuContainerAnimator = ObjectAnimator.ofFloat(mMenuContainer, View.ALPHA,
+            mMenuContainerAnimator = new AnimatorSet();
+            ObjectAnimator menuAnim = ObjectAnimator.ofFloat(mMenuContainer, View.ALPHA,
                     mMenuContainer.getAlpha(), 1f);
+            menuAnim.addUpdateListener(mMenuBgUpdateListener);
+            ObjectAnimator dismissAnim = ObjectAnimator.ofFloat(mDismissButton, View.ALPHA,
+                    mDismissButton.getAlpha(), 1f);
+            if (menuState == MENU_STATE_FULL) {
+                mMenuContainerAnimator.playTogether(menuAnim, dismissAnim);
+            } else {
+                mMenuContainerAnimator.play(dismissAnim);
+            }
             mMenuContainerAnimator.setInterpolator(Interpolators.ALPHA_IN);
             mMenuContainerAnimator.setDuration(MENU_FADE_DURATION);
             if (allowMenuTimeout) {
@@ -280,7 +304,6 @@
                     }
                 });
             }
-            mMenuContainerAnimator.addUpdateListener(mMenuBgUpdateListener);
             mMenuContainerAnimator.start();
         } else {
             // If we are already visible, then just start the delayed dismiss and unregister any
@@ -297,13 +320,18 @@
     }
 
     private void hideMenu(final Runnable animationFinishedRunnable, boolean notifyMenuVisibility) {
-        if (mMenuVisible) {
+        if (mMenuState != MENU_STATE_NONE) {
             cancelDelayedFinish();
             if (notifyMenuVisibility) {
-                notifyMenuVisibility(false);
+                notifyMenuStateChange(MENU_STATE_NONE);
             }
-            mMenuContainerAnimator = ObjectAnimator.ofFloat(mMenuContainer, View.ALPHA,
+            mMenuContainerAnimator = new AnimatorSet();
+            ObjectAnimator menuAnim = ObjectAnimator.ofFloat(mMenuContainer, View.ALPHA,
                     mMenuContainer.getAlpha(), 0f);
+            menuAnim.addUpdateListener(mMenuBgUpdateListener);
+            ObjectAnimator dismissAnim = ObjectAnimator.ofFloat(mDismissButton, View.ALPHA,
+                    mDismissButton.getAlpha(), 0f);
+            mMenuContainerAnimator.playTogether(menuAnim, dismissAnim);
             mMenuContainerAnimator.setInterpolator(Interpolators.ALPHA_OUT);
             mMenuContainerAnimator.setDuration(MENU_FADE_DURATION);
             mMenuContainerAnimator.addListener(new AnimatorListenerAdapter() {
@@ -312,11 +340,9 @@
                     if (animationFinishedRunnable != null) {
                         animationFinishedRunnable.run();
                     }
-
                     finish();
                 }
             });
-            mMenuContainerAnimator.addUpdateListener(mMenuBgUpdateListener);
             mMenuContainerAnimator.start();
         } else {
             // If the menu is not visible, just finish now
@@ -332,11 +358,12 @@
             mActions.clear();
             mActions.addAll(actions.getList());
         }
-        if (intent.getBooleanExtra(EXTRA_SHOW_MENU, false)) {
+        final int menuState = intent.getIntExtra(EXTRA_MENU_STATE, MENU_STATE_NONE);
+        if (menuState != MENU_STATE_NONE) {
             Rect stackBounds = intent.getParcelableExtra(EXTRA_STACK_BOUNDS);
             Rect movementBounds = intent.getParcelableExtra(EXTRA_MOVEMENT_BOUNDS);
             boolean allowMenuTimeout = intent.getBooleanExtra(EXTRA_ALLOW_TIMEOUT, true);
-            showMenu(stackBounds, movementBounds, allowMenuTimeout);
+            showMenu(menuState, stackBounds, movementBounds, allowMenuTimeout);
         }
     }
 
@@ -372,7 +399,7 @@
             return true;
         });
 
-        if (mActions.isEmpty()) {
+        if (mActions.isEmpty() || mMenuState == MENU_STATE_CLOSE) {
             actionsContainer.setVisibility(View.INVISIBLE);
         } else {
             actionsContainer.setVisibility(View.VISIBLE);
@@ -427,12 +454,17 @@
 
     private void updateDismissFraction(float fraction) {
         int alpha;
-        if (mMenuVisible) {
-            mMenuContainer.setAlpha(1 - fraction);
+        final float menuAlpha = 1 - fraction;
+        if (mMenuState == MENU_STATE_FULL) {
+            mMenuContainer.setAlpha(menuAlpha);
+            mDismissButton.setAlpha(menuAlpha);
             final float interpolatedAlpha =
-                    MENU_BACKGROUND_ALPHA * (1.0f - fraction) + DISMISS_BACKGROUND_ALPHA * fraction;
+                    MENU_BACKGROUND_ALPHA * menuAlpha + DISMISS_BACKGROUND_ALPHA * fraction;
             alpha = (int) (interpolatedAlpha * 255);
         } else {
+            if (mMenuState == MENU_STATE_CLOSE) {
+                mDismissButton.setAlpha(menuAlpha);
+            }
             alpha = (int) (fraction * DISMISS_BACKGROUND_ALPHA * 255);
         }
         mBackgroundDrawable.setAlpha(alpha);
@@ -450,11 +482,11 @@
         sendMessage(m, "Could not notify controller to unregister input consumer");
     }
 
-    private void notifyMenuVisibility(boolean visible) {
-        mMenuVisible = visible;
+    private void notifyMenuStateChange(int menuState) {
+        mMenuState = menuState;
         Message m = Message.obtain();
-        m.what = PipMenuActivityController.MESSAGE_MENU_VISIBILITY_CHANGED;
-        m.arg1 = visible ? 1 : 0;
+        m.what = PipMenuActivityController.MESSAGE_MENU_STATE_CHANGED;
+        m.arg1 = menuState;
         sendMessage(m, "Could not notify controller of PIP menu visibility");
     }
 
@@ -481,6 +513,12 @@
         }, false /* notifyMenuVisibility */);
     }
 
+    private void showPipMenu() {
+        Message m = Message.obtain();
+        m.what = PipMenuActivityController.MESSAGE_SHOW_MENU;
+        sendMessage(m, "Could not notify controller to show PIP menu");
+    }
+
     private void notifyActivityCallback(Messenger callback) {
         Message m = Message.obtain();
         m.what = PipMenuActivityController.MESSAGE_UPDATE_ACTIVITY_CALLBACK;
diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
index 875fb14..c41f898 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
@@ -57,16 +57,21 @@
     public static final String EXTRA_STACK_BOUNDS = "stack_bounds";
     public static final String EXTRA_MOVEMENT_BOUNDS = "movement_bounds";
     public static final String EXTRA_ALLOW_TIMEOUT = "allow_timeout";
-    public static final String EXTRA_SHOW_MENU = "show_menu";
     public static final String EXTRA_DISMISS_FRACTION = "dismiss_fraction";
+    public static final String EXTRA_MENU_STATE = "menu_state";
 
-    public static final int MESSAGE_MENU_VISIBILITY_CHANGED = 100;
+    public static final int MESSAGE_MENU_STATE_CHANGED = 100;
     public static final int MESSAGE_EXPAND_PIP = 101;
     public static final int MESSAGE_MINIMIZE_PIP = 102;
     public static final int MESSAGE_DISMISS_PIP = 103;
     public static final int MESSAGE_UPDATE_ACTIVITY_CALLBACK = 104;
     public static final int MESSAGE_REGISTER_INPUT_CONSUMER = 105;
     public static final int MESSAGE_UNREGISTER_INPUT_CONSUMER = 106;
+    public static final int MESSAGE_SHOW_MENU = 107;
+
+    public static final int MENU_STATE_NONE = 0;
+    public static final int MENU_STATE_CLOSE = 1;
+    public static final int MENU_STATE_FULL = 2;
 
     /**
      * A listener interface to receive notification on changes in PIP.
@@ -75,10 +80,10 @@
         /**
          * Called when the PIP menu visibility changes.
          *
-         * @param menuVisible whether or not the menu is visible
-         * @param resize whether or not to resize the PiP with the visibility change
+         * @param menuState the current state of the menu
+         * @param resize whether or not to resize the PiP with the state change
          */
-        void onPipMenuVisibilityChanged(boolean menuVisible, boolean resize);
+        void onPipMenuStateChanged(int menuState, boolean resize);
 
         /**
          * Called when the PIP requested to be expanded.
@@ -94,6 +99,11 @@
          * Called when the PIP requested to be dismissed.
          */
         void onPipDismiss();
+
+        /**
+         * Called when the PIP requested to show the menu.
+         */
+        void onPipShowMenu();
     }
 
     private Context mContext;
@@ -104,7 +114,7 @@
     private ArrayList<Listener> mListeners = new ArrayList<>();
     private ParceledListSlice mAppActions;
     private ParceledListSlice mMediaActions;
-    private boolean mMenuVisible;
+    private int mMenuState;
 
     // The dismiss fraction update is sent frequently, so use a temporary bundle for the message
     private Bundle mTmpDismissFractionData = new Bundle();
@@ -115,16 +125,16 @@
         @Override
         public void handleMessage(Message msg) {
             switch (msg.what) {
-                case MESSAGE_MENU_VISIBILITY_CHANGED: {
-                    boolean visible = msg.arg1 > 0;
-                    onMenuVisibilityChanged(visible, true /* resize */);
+                case MESSAGE_MENU_STATE_CHANGED: {
+                    int menuState = msg.arg1;
+                    onMenuStateChanged(menuState, true /* resize */);
                     break;
                 }
                 case MESSAGE_EXPAND_PIP: {
                     mListeners.forEach(l -> l.onPipExpand());
                     // Preemptively mark the menu as invisible once we expand the PiP, but don't
                     // resize as we will be animating the stack
-                    onMenuVisibilityChanged(false, false /* resize */);
+                    onMenuStateChanged(MENU_STATE_NONE, false /* resize */);
                     break;
                 }
                 case MESSAGE_MINIMIZE_PIP: {
@@ -135,7 +145,11 @@
                     mListeners.forEach(l -> l.onPipDismiss());
                     // Preemptively mark the menu as invisible once we dismiss the PiP, but don't
                     // resize as we'll be removing the stack in place
-                    onMenuVisibilityChanged(false, false /* resize */);
+                    onMenuStateChanged(MENU_STATE_NONE, false /* resize */);
+                    break;
+                }
+                case MESSAGE_SHOW_MENU: {
+                    mListeners.forEach(l -> l.onPipShowMenu());
                     break;
                 }
                 case MESSAGE_REGISTER_INPUT_CONSUMER: {
@@ -151,7 +165,7 @@
                     mStartActivityRequested = false;
                     // Mark the menu as invisible once the activity finishes as well
                     if (mToActivityMessenger == null) {
-                        onMenuVisibilityChanged(false, true /* resize */);
+                        onMenuStateChanged(MENU_STATE_NONE, true /* resize */);
                     }
                     break;
                 }
@@ -176,7 +190,7 @@
     }
 
     public void onActivityPinned() {
-        if (!mMenuVisible) {
+        if (mMenuState == MENU_STATE_NONE) {
             // If the menu is not visible, then re-register the input consumer if it is not already
             // registered
             mInputConsumerController.registerInputConsumer();
@@ -209,23 +223,25 @@
             try {
                 mToActivityMessenger.send(m);
             } catch (RemoteException e) {
-                Log.e(TAG, "Could not notify menu to show", e);
+                Log.e(TAG, "Could not notify menu to update dismiss fraction", e);
             }
         } else if (!mStartActivityRequested) {
-            startMenuActivity(null /* stackBounds */, null /* movementBounds */,
-                    false /* showMenu */, false /* allowMenuTimeout */);
+            startMenuActivity(MENU_STATE_NONE, null /* stackBounds */,
+                    null /* movementBounds */, false /* allowMenuTimeout */);
         }
     }
 
     /**
      * Shows the menu activity.
      */
-    public void showMenu(Rect stackBounds, Rect movementBounds, boolean allowMenuTimeout) {
+    public void showMenu(int menuState, Rect stackBounds, Rect movementBounds,
+            boolean allowMenuTimeout) {
         if (DEBUG) {
             Log.d(TAG, "showMenu() hasActivity=" + (mToActivityMessenger != null));
         }
         if (mToActivityMessenger != null) {
             Bundle data = new Bundle();
+            data.putInt(EXTRA_MENU_STATE, menuState);
             data.putParcelable(EXTRA_STACK_BOUNDS, stackBounds);
             data.putParcelable(EXTRA_MOVEMENT_BOUNDS, movementBounds);
             data.putBoolean(EXTRA_ALLOW_TIMEOUT, allowMenuTimeout);
@@ -238,7 +254,7 @@
                 Log.e(TAG, "Could not notify menu to show", e);
             }
         } else if (!mStartActivityRequested) {
-            startMenuActivity(stackBounds, movementBounds, true /* showMenu */, allowMenuTimeout);
+            startMenuActivity(menuState, stackBounds, movementBounds, allowMenuTimeout);
         }
     }
 
@@ -279,10 +295,10 @@
     }
 
     /**
-     * @return whether the menu is currently visible.
+     * @return the current menu state.
      */
-    public boolean isMenuVisible() {
-        return mMenuVisible;
+    public int getMenuState() {
+        return mMenuState;
     }
 
     /**
@@ -306,7 +322,7 @@
     /**
      * Starts the menu activity on the top task of the pinned stack.
      */
-    private void startMenuActivity(Rect stackBounds, Rect movementBounds, boolean showMenu,
+    private void startMenuActivity(int menuState, Rect stackBounds, Rect movementBounds,
             boolean allowMenuTimeout) {
         try {
             StackInfo pinnedStackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID);
@@ -321,7 +337,7 @@
                 if (movementBounds != null) {
                     intent.putExtra(EXTRA_MOVEMENT_BOUNDS, movementBounds);
                 }
-                intent.putExtra(EXTRA_SHOW_MENU, showMenu);
+                intent.putExtra(EXTRA_MENU_STATE, menuState);
                 intent.putExtra(EXTRA_ALLOW_TIMEOUT, allowMenuTimeout);
                 ActivityOptions options = ActivityOptions.makeCustomAnimation(mContext, 0, 0);
                 options.setLaunchTaskId(
@@ -378,19 +394,19 @@
     /**
      * Handles changes in menu visibility.
      */
-    private void onMenuVisibilityChanged(boolean visible, boolean resize) {
+    private void onMenuStateChanged(int menuState, boolean resize) {
         if (DEBUG) {
-            Log.d(TAG, "onMenuVisibilityChanged() mMenuVisible=" + mMenuVisible
-                    + " menuVisible=" + visible + " resize=" + resize);
+            Log.d(TAG, "onMenuStateChanged() mMenuState=" + mMenuState
+                    + " menuState=" + menuState + " resize=" + resize);
         }
-        if (visible) {
-            mInputConsumerController.unregisterInputConsumer();
-        } else {
+        if (menuState == MENU_STATE_NONE) {
             mInputConsumerController.registerInputConsumer();
+        } else {
+            mInputConsumerController.unregisterInputConsumer();
         }
-        if (visible != mMenuVisible) {
-            mListeners.forEach(l -> l.onPipMenuVisibilityChanged(visible, resize));
-            if (visible) {
+        if (menuState != mMenuState) {
+            mListeners.forEach(l -> l.onPipMenuStateChanged(menuState, resize));
+            if (menuState == MENU_STATE_FULL) {
                 // Once visible, start listening for media action changes. This call will trigger
                 // the menu actions to be updated again.
                 mMediaController.addListener(mMediaActionListener);
@@ -400,13 +416,13 @@
                 mMediaController.removeListener(mMediaActionListener);
             }
         }
-        mMenuVisible = visible;
+        mMenuState = menuState;
     }
 
     public void dump(PrintWriter pw, String prefix) {
         final String innerPrefix = prefix + "  ";
         pw.println(prefix + TAG);
-        pw.println(innerPrefix + "mMenuVisible=" + mMenuVisible);
+        pw.println(innerPrefix + "mMenuState=" + mMenuState);
         pw.println(innerPrefix + "mToActivityMessenger=" + mToActivityMessenger);
         pw.println(innerPrefix + "mListeners=" + mListeners.size());
     }
diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java
index 161bdac..c3c09a0 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java
@@ -16,6 +16,10 @@
 
 package com.android.systemui.pip.phone;
 
+import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_NONE;
+import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_CLOSE;
+import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_FULL;
+
 import android.animation.Animator;
 import android.animation.AnimatorListenerAdapter;
 import android.animation.ValueAnimator;
@@ -106,7 +110,7 @@
     private boolean mEnableMinimize = false;
 
     // Behaviour states
-    private boolean mIsMenuVisible;
+    private int mMenuState;
     private boolean mIsMinimized;
     private boolean mIsImeShowing;
     private int mImeHeight;
@@ -129,8 +133,8 @@
      */
     private class PipMenuListener implements PipMenuActivityController.Listener {
         @Override
-        public void onPipMenuVisibilityChanged(boolean menuVisible, boolean resize) {
-            setMenuVisibilityState(menuVisible, resize);
+        public void onPipMenuStateChanged(int menuState, boolean resize) {
+            setMenuState(menuState, resize);
         }
 
         @Override
@@ -152,6 +156,12 @@
             MetricsLogger.action(mContext, MetricsEvent.ACTION_PICTURE_IN_PICTURE_DISMISSED,
                     METRIC_VALUE_DISMISSED_BY_TAP);
         }
+
+        @Override
+        public void onPipShowMenu() {
+            mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
+                    mMovementBounds, true /* allowMenuTimeout */);
+        }
     }
 
     public PipTouchHandler(Context context, IActivityManager activityManager,
@@ -193,20 +203,21 @@
     public void showPictureInPictureMenu() {
         // Only show the menu if the user isn't currently interacting with the PiP
         if (!mTouchState.isUserInteracting()) {
-            mMenuController.showMenu(mMotionHelper.getBounds(), mMovementBounds,
-                    false /* allowMenuTimeout */);
+            mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
+                    mMovementBounds, false /* allowMenuTimeout */);
         }
     }
 
     public void onActivityPinned() {
         // Reset some states once we are pinned
-        if (mIsMenuVisible) {
-            mIsMenuVisible = false;
-        }
+        mMenuState = MENU_STATE_NONE;
+
         if (mIsMinimized) {
             setMinimizedStateInternal(false);
         }
         mDismissViewController.destroyDismissTarget();
+        mMenuController.showMenu(MENU_STATE_CLOSE, mMotionHelper.getBounds(),
+                mMovementBounds, true /* allowMenuTimeout */);
     }
 
     public void onPinnedStackAnimationEnded() {
@@ -266,7 +277,7 @@
                 // touching the screen
             } else {
                 final Rect bounds = new Rect(animatingBounds);
-                final Rect toMovementBounds = mIsMenuVisible
+                final Rect toMovementBounds = mMenuState == MENU_STATE_FULL
                         ? expandedMovementBounds
                         : normalMovementBounds;
                 if (mIsImeShowing) {
@@ -293,7 +304,7 @@
         // above
         mNormalMovementBounds = normalMovementBounds;
         mExpandedMovementBounds = expandedMovementBounds;
-        updateMovementBounds(mIsMenuVisible);
+        updateMovementBounds(mMenuState);
     }
 
     private void onRegistrationChanged(boolean isRegistered) {
@@ -303,8 +314,8 @@
     }
 
     private void onAccessibilityShowMenu() {
-        mMenuController.showMenu(mMotionHelper.getBounds(), mMovementBounds,
-                false /* allowMenuTimeout */);
+        mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
+                mMovementBounds, false /* allowMenuTimeout */);
     }
 
     private boolean handleTouchEvent(MotionEvent ev) {
@@ -336,7 +347,7 @@
             case MotionEvent.ACTION_UP: {
                 // Update the movement bounds again if the state has changed since the user started
                 // dragging (ie. when the IME shows)
-                updateMovementBounds(mIsMenuVisible);
+                updateMovementBounds(mMenuState);
 
                 for (PipTouchGesture gesture : mGestures) {
                     if (gesture.onUp(mTouchState)) {
@@ -378,7 +389,7 @@
                 break;
             }
         }
-        return !mIsMenuVisible;
+        return mMenuState == MENU_STATE_NONE;
     }
 
     /**
@@ -393,7 +404,7 @@
                 final float distance = bounds.bottom - target;
                 fraction = Math.min(distance / bounds.height(), 1f);
             }
-            if (Float.compare(fraction, 0f) != 0 || mMenuController.isMenuVisible()) {
+            if (Float.compare(fraction, 0f) != 0 || mMenuState != MENU_STATE_NONE) {
                 // Update if the fraction > 0, or if fraction == 0 and the menu was already visible
                 mMenuController.setDismissFraction(fraction);
             }
@@ -449,8 +460,8 @@
     /**
      * Sets the menu visibility.
      */
-    void setMenuVisibilityState(boolean menuVisible, boolean resize) {
-        if (menuVisible) {
+    void setMenuState(int menuState, boolean resize) {
+        if (menuState == MENU_STATE_FULL) {
             // Save the current snap fraction and if we do not drag or move the PiP, then
             // we store back to this snap fraction.  Otherwise, we'll reset the snap
             // fraction and snap to the closest edge
@@ -459,7 +470,7 @@
                 mSavedSnapFraction = mMotionHelper.animateToExpandedState(expandedBounds,
                         mMovementBounds, mExpandedMovementBounds);
             }
-        } else {
+        } else if (menuState == MENU_STATE_NONE) {
             // Try and restore the PiP to the closest edge, using the saved snap fraction
             // if possible
             if (resize) {
@@ -469,10 +480,12 @@
             }
             mSavedSnapFraction = -1f;
         }
-        mIsMenuVisible = menuVisible;
-        updateMovementBounds(menuVisible);
-        MetricsLogger.visibility(mContext, MetricsEvent.ACTION_PICTURE_IN_PICTURE_MENU,
-                menuVisible);
+        mMenuState = menuState;
+        updateMovementBounds(menuState);
+        if (menuState != MENU_STATE_CLOSE) {
+            MetricsLogger.visibility(mContext, MetricsEvent.ACTION_PICTURE_IN_PICTURE_MENU,
+                    menuState == MENU_STATE_FULL);
+        }
     }
 
     /**
@@ -501,7 +514,7 @@
 
             // If the menu is still visible, and we aren't minimized, then just poke the menu
             // so that it will timeout after the user stops touching it
-            if (mMenuController.isMenuVisible() && !mIsMinimized) {
+            if (mMenuState != MENU_STATE_NONE && !mIsMinimized) {
                 mMenuController.pokeMenu();
             }
 
@@ -599,7 +612,7 @@
                         !mIsMinimized && (mMotionHelper.shouldMinimizePip() || isFlingToEdge)) {
                     // Pip should be minimized
                     setMinimizedStateInternal(true);
-                    if (mMenuController.isMenuVisible()) {
+                    if (mMenuState == MENU_STATE_FULL) {
                         // If the user dragged the expanded PiP to the edge, then hiding the menu
                         // will trigger the PiP to be scaled back to the normal size with the
                         // minimize offset adjusted
@@ -617,11 +630,11 @@
                 }
 
                 AnimatorListenerAdapter postAnimationCallback = null;
-                if (mMenuController.isMenuVisible()) {
+                if (mMenuState != MENU_STATE_NONE) {
                     // If the menu is still visible, and we aren't minimized, then just poke the
                     // menu so that it will timeout after the user stops touching it
-                    mMenuController.showMenu(mMotionHelper.getBounds(), mMovementBounds,
-                            true /* allowMenuTimeout */);
+                    mMenuController.showMenu(mMenuState, mMotionHelper.getBounds(),
+                            mMovementBounds, true /* allowMenuTimeout */);
                 } else {
                     // If the menu is not visible, then we can still be showing the activity for the
                     // dismiss overlay, so just finish it after the animation completes
@@ -645,9 +658,9 @@
                 mMotionHelper.animateToClosestSnapTarget(mMovementBounds, null /* updateListener */,
                         null /* animatorListener */);
                 setMinimizedStateInternal(false);
-            } else if (!mIsMenuVisible) {
-                mMenuController.showMenu(mMotionHelper.getBounds(), mMovementBounds,
-                        true /* allowMenuTimeout */);
+            } else if (mMenuState != MENU_STATE_FULL) {
+                mMenuController.showMenu(MENU_STATE_FULL, mMotionHelper.getBounds(),
+                        mMovementBounds, true /* allowMenuTimeout */);
             } else {
                 mMotionHelper.expandPip();
             }
@@ -658,8 +671,8 @@
     /**
      * Updates the current movement bounds based on whether the menu is currently visible.
      */
-    private void updateMovementBounds(boolean isExpanded) {
-        mMovementBounds = isExpanded
+    private void updateMovementBounds(int menuState) {
+        mMovementBounds = menuState == MENU_STATE_FULL
                 ? mExpandedMovementBounds
                 : mNormalMovementBounds;
     }
@@ -672,7 +685,7 @@
         pw.println(innerPrefix + "mNormalMovementBounds=" + mNormalMovementBounds);
         pw.println(innerPrefix + "mExpandedBounds=" + mExpandedBounds);
         pw.println(innerPrefix + "mExpandedMovementBounds=" + mExpandedMovementBounds);
-        pw.println(innerPrefix + "mIsMenuVisible=" + mIsMenuVisible);
+        pw.println(innerPrefix + "mMenuState=" + mMenuState);
         pw.println(innerPrefix + "mIsMinimized=" + mIsMinimized);
         pw.println(innerPrefix + "mIsImeShowing=" + mIsImeShowing);
         pw.println(innerPrefix + "mImeHeight=" + mImeHeight);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSDetailItems.java b/packages/SystemUI/src/com/android/systemui/qs/QSDetailItems.java
index 65238b1..efc0668 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSDetailItems.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSDetailItems.java
@@ -81,8 +81,8 @@
         mItemList.setAdapter(mAdapter);
         mEmpty = findViewById(android.R.id.empty);
         mEmpty.setVisibility(GONE);
-        mEmptyText = (TextView) mEmpty.findViewById(android.R.id.title);
-        mEmptyIcon = (ImageView) mEmpty.findViewById(android.R.id.icon);
+        mEmptyText = mEmpty.findViewById(android.R.id.title);
+        mEmptyIcon = mEmpty.findViewById(android.R.id.icon);
     }
 
     @Override
@@ -104,8 +104,10 @@
     }
 
     public void setEmptyState(int icon, int text) {
-        mEmptyIcon.setImageResource(icon);
-        mEmptyText.setText(text);
+        mEmptyIcon.post(() -> {
+            mEmptyIcon.setImageResource(icon);
+            mEmptyText.setText(text);
+        });
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileImpl.java b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileImpl.java
index 1aa51b1..df2a9fb 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileImpl.java
@@ -73,6 +73,7 @@
 
     private String mTileSpec;
     private EnforcedAdmin mEnforcedAdmin;
+    private boolean mShowingDetail;
 
     public abstract TState newTileState();
 
@@ -286,11 +287,16 @@
     }
 
     private void handleShowDetail(boolean show) {
+        mShowingDetail = show;
         for (int i = 0; i < mCallbacks.size(); i++) {
             mCallbacks.get(i).onShowDetail(show);
         }
     }
 
+    protected boolean isShowingDetail() {
+        return mShowingDetail;
+    }
+
     private void handleToggleStateChanged(boolean state) {
         for (int i = 0; i < mCallbacks.size(); i++) {
             mCallbacks.get(i).onToggleStateChanged(state);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileView.java b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileView.java
index 5c99809..263dac0 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileView.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileView.java
@@ -42,6 +42,7 @@
     private int mState;
     private ViewGroup mLabelContainer;
     private View mExpandIndicator;
+    private View mExpandSpace;
 
     public QSTileView(Context context, QSIconView icon) {
         this(context, icon, false);
@@ -84,6 +85,7 @@
         mPadLock = mLabelContainer.findViewById(R.id.restricted_padlock);
         mDivider = mLabelContainer.findViewById(R.id.underline);
         mExpandIndicator = mLabelContainer.findViewById(R.id.expand_indicator);
+        mExpandSpace = mLabelContainer.findViewById(R.id.expand_space);
 
         addView(mLabelContainer);
     }
@@ -101,8 +103,8 @@
             mState = state.state;
             mLabel.setText(state.label);
         }
-        mDivider.setVisibility(state.dualTarget ? View.VISIBLE : View.INVISIBLE);
         mExpandIndicator.setVisibility(state.dualTarget ? View.VISIBLE : View.GONE);
+        mExpandSpace.setVisibility(state.dualTarget ? View.VISIBLE : View.GONE);
         mLabelContainer.setContentDescription(state.dualTarget ? state.dualLabelContentDescription
                 : null);
         if (state.dualTarget != mLabelContainer.isClickable()) {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java
index 75e9d5a..d27bb85 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java
@@ -35,10 +35,10 @@
 import com.android.systemui.R;
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.plugins.qs.DetailAdapter;
+import com.android.systemui.plugins.qs.QSTile.BooleanState;
 import com.android.systemui.qs.QSDetailItems;
 import com.android.systemui.qs.QSDetailItems.Item;
 import com.android.systemui.qs.QSHost;
-import com.android.systemui.plugins.qs.QSTile.BooleanState;
 import com.android.systemui.qs.tileimpl.QSTileImpl;
 import com.android.systemui.statusbar.policy.BluetoothController;
 
@@ -99,10 +99,6 @@
             return;
         }
         showDetail(true);
-        if (!mState.value) {
-            mState.value = true;
-            mController.setBluetoothEnabled(true);
-        }
     }
 
     @Override
@@ -176,6 +172,9 @@
         @Override
         public void onBluetoothStateChange(boolean enabled) {
             refreshState();
+            if (isShowingDetail()) {
+                mDetailAdapter.updateItems();
+            }
         }
 
         @Override
@@ -187,6 +186,9 @@
                 }
             });
             refreshState();
+            if (isShowingDetail()) {
+                mDetailAdapter.updateItems();
+            }
         }
     };
 
@@ -223,7 +225,6 @@
         public void setToggleState(boolean state) {
             MetricsLogger.action(mContext, MetricsEvent.QS_BLUETOOTH_TOGGLE, state);
             mController.setBluetoothEnabled(state);
-            showDetail(false);
         }
 
         @Override
@@ -235,8 +236,6 @@
         public View createDetailView(Context context, View convertView, ViewGroup parent) {
             mItems = QSDetailItems.convertOrInflate(context, convertView, parent);
             mItems.setTagSuffix("Bluetooth");
-            mItems.setEmptyState(R.drawable.ic_qs_bluetooth_detail_empty,
-                    R.string.quick_settings_bluetooth_detail_empty_text);
             mItems.setCallback(this);
             updateItems();
             setItemsVisible(mState.value);
@@ -250,6 +249,13 @@
 
         private void updateItems() {
             if (mItems == null) return;
+            if (mController.isBluetoothEnabled()) {
+                mItems.setEmptyState(R.drawable.ic_qs_bluetooth_detail_empty,
+                        R.string.quick_settings_bluetooth_detail_empty_text);
+            } else {
+                mItems.setEmptyState(R.drawable.ic_qs_bluetooth_detail_empty,
+                        R.string.bt_is_off);
+            }
             ArrayList<Item> items = new ArrayList<Item>();
             final Collection<CachedBluetoothDevice> devices = mController.getDevices();
             if (devices != null) {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java
index 3c2e897..ecbc4f7 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java
@@ -16,16 +16,25 @@
 
 package com.android.systemui.qs.tiles;
 
+import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
+import static android.provider.Settings.Global.ZEN_MODE_OFF;
+
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.net.Uri;
 import android.os.UserManager;
 import android.provider.Settings;
 import android.provider.Settings.Global;
+import android.service.notification.ZenModeConfig;
+import android.service.notification.ZenModeConfig.ZenRule;
 import android.service.quicksettings.Tile;
+import android.util.Slog;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnAttachStateChangeListener;
@@ -41,9 +50,9 @@
 import com.android.systemui.SysUIToast;
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.plugins.qs.DetailAdapter;
-import com.android.systemui.qs.QSHost;
 import com.android.systemui.plugins.qs.QSTile;
 import com.android.systemui.plugins.qs.QSTile.BooleanState;
+import com.android.systemui.qs.QSHost;
 import com.android.systemui.qs.tileimpl.QSTileImpl;
 import com.android.systemui.statusbar.policy.ZenModeController;
 import com.android.systemui.volume.ZenModePanel;
@@ -129,10 +138,9 @@
     @Override
     protected void handleClick() {
         if (mState.value) {
-            mController.setZen(Global.ZEN_MODE_OFF, null, TAG);
+            mController.setZen(ZEN_MODE_OFF, null, TAG);
         } else {
-            int zen = Prefs.getInt(mContext, Prefs.Key.DND_FAVORITE_ZEN, Global.ZEN_MODE_ALARMS);
-            mController.setZen(zen, null, TAG);
+            mController.setZen(ZEN_MODE_ALARMS, null, TAG);
         }
     }
 
@@ -147,8 +155,6 @@
             return;
         }
         showDetail(true);
-        int zen = Prefs.getInt(mContext, Prefs.Key.DND_FAVORITE_ZEN, Global.ZEN_MODE_ALARMS);
-        mController.setZen(zen, null, TAG);
     }
 
     @Override
@@ -159,7 +165,7 @@
     @Override
     protected void handleUpdateState(BooleanState state, Object arg) {
         final int zen = arg instanceof Integer ? (Integer) arg : mController.getZen();
-        final boolean newValue = zen != Global.ZEN_MODE_OFF;
+        final boolean newValue = zen != ZEN_MODE_OFF;
         final boolean valueChanged = state.value != newValue;
         state.dualTarget = true;
         state.value = newValue;
@@ -178,7 +184,7 @@
                 state.contentDescription = mContext.getString(
                         R.string.accessibility_quick_settings_dnd_none_on);
                 break;
-            case Global.ZEN_MODE_ALARMS:
+            case ZEN_MODE_ALARMS:
                 state.icon = ResourceIcon.get(R.drawable.ic_qs_dnd_on);
                 state.label = mContext.getString(R.string.quick_settings_dnd_alarms_label);
                 state.contentDescription = mContext.getString(
@@ -187,13 +193,10 @@
             default:
                 state.icon = TOTAL_SILENCE.equals(state.icon) ? mDisableTotalSilence : mDisable;
                 state.label = mContext.getString(R.string.quick_settings_dnd_label);
-                state.contentDescription =  mContext.getString(
+                state.contentDescription = mContext.getString(
                         R.string.accessibility_quick_settings_dnd);
                 break;
         }
-        if (mShowingDetail && !state.value) {
-            showDetail(false);
-        }
         if (valueChanged) {
             fireToggleStateChanged(state.value);
         }
@@ -249,6 +252,16 @@
     private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
         public void onZenChanged(int zen) {
             refreshState(zen);
+            if (isShowingDetail()) {
+                mDetailAdapter.updatePanel();
+            }
+        }
+
+        @Override
+        public void onConfigChanged(ZenModeConfig config) {
+            if (isShowingDetail()) {
+                mDetailAdapter.updatePanel();
+            }
         }
     };
 
@@ -263,6 +276,9 @@
 
     private final class DndDetailAdapter implements DetailAdapter, OnAttachStateChangeListener {
 
+        private ZenModePanel mZenPanel;
+        private boolean mAuto;
+
         @Override
         public CharSequence getTitle() {
             return mContext.getString(R.string.quick_settings_dnd_label);
@@ -282,8 +298,12 @@
         public void setToggleState(boolean state) {
             MetricsLogger.action(mContext, MetricsEvent.QS_DND_TOGGLE, state);
             if (!state) {
-                mController.setZen(Global.ZEN_MODE_OFF, null, TAG);
-                showDetail(false);
+                mController.setZen(ZEN_MODE_OFF, null, TAG);
+                mAuto = false;
+            } else {
+                int zen = Prefs.getInt(mContext, Prefs.Key.DND_FAVORITE_ZEN,
+                        ZEN_MODE_ALARMS);
+                mController.setZen(zen, null, TAG);
             }
         }
 
@@ -294,15 +314,65 @@
 
         @Override
         public View createDetailView(Context context, View convertView, ViewGroup parent) {
-            final ZenModePanel zmp = convertView != null ? (ZenModePanel) convertView
+            mZenPanel = convertView != null ? (ZenModePanel) convertView
                     : (ZenModePanel) LayoutInflater.from(context).inflate(
                             R.layout.zen_mode_panel, parent, false);
             if (convertView == null) {
-                zmp.init(mController);
-                zmp.addOnAttachStateChangeListener(this);
-                zmp.setCallback(mZenModePanelCallback);
+                mZenPanel.init(mController);
+                mZenPanel.addOnAttachStateChangeListener(this);
+                mZenPanel.setCallback(mZenModePanelCallback);
+                mZenPanel.setEmptyState(R.drawable.ic_qs_dnd_off, R.string.dnd_is_off);
             }
-            return zmp;
+            updatePanel();
+            return mZenPanel;
+        }
+
+        private void updatePanel() {
+            if (mZenPanel == null) return;
+            mAuto = false;
+            if (mController.getZen() == ZEN_MODE_OFF) {
+                mZenPanel.setState(ZenModePanel.STATE_OFF);
+            } else {
+                ZenModeConfig config = mController.getConfig();
+                String summary = "";
+                if (config.manualRule != null && config.manualRule.enabler != null) {
+                    summary = getOwnerCaption(config.manualRule.enabler);
+                }
+                for (ZenRule automaticRule : config.automaticRules.values()) {
+                    if (automaticRule.isAutomaticActive()) {
+                        if (summary.isEmpty()) {
+                            summary = mContext.getString(R.string.qs_dnd_prompt_auto_rule,
+                                    automaticRule.name);
+                        } else {
+                            summary = mContext.getString(R.string.qs_dnd_prompt_auto_rule_app);
+                        }
+                    }
+                }
+                if (summary.isEmpty()) {
+                    mZenPanel.setState(ZenModePanel.STATE_MODIFY);
+                } else {
+                    mAuto = true;
+                    mZenPanel.setState(ZenModePanel.STATE_AUTO_RULE);
+                    mZenPanel.setAutoText(summary);
+                }
+            }
+        }
+
+        private String getOwnerCaption(String owner) {
+            final PackageManager pm = mContext.getPackageManager();
+            try {
+                final ApplicationInfo info = pm.getApplicationInfo(owner, 0);
+                if (info != null) {
+                    final CharSequence seq = info.loadLabel(pm);
+                    if (seq != null) {
+                        final String str = seq.toString().trim();
+                        return mContext.getString(R.string.qs_dnd_prompt_app, str);
+                    }
+                }
+            } catch (Throwable e) {
+                Slog.w(TAG, "Error loading owner caption", e);
+            }
+            return "";
         }
 
         @Override
@@ -313,6 +383,7 @@
         @Override
         public void onViewDetachedFromWindow(View v) {
             mShowingDetail = false;
+            mZenPanel = null;
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java
index 7d2d210..5a23d3b 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java
@@ -32,6 +32,7 @@
 import com.android.settingslib.wifi.AccessPoint;
 import com.android.systemui.Dependency;
 import com.android.systemui.R;
+import com.android.systemui.R.string;
 import com.android.systemui.plugins.ActivityStarter;
 import com.android.systemui.plugins.qs.DetailAdapter;
 import com.android.systemui.qs.QSDetailItems;
@@ -98,7 +99,9 @@
     }
 
     @Override
-    protected DetailAdapter createDetailAdapter() { return new WifiDetailAdapter(); }
+    protected DetailAdapter createDetailAdapter() {
+        return new WifiDetailAdapter();
+    }
 
     @Override
     public QSIconView createTileView(Context context) {
@@ -125,10 +128,6 @@
             return;
         }
         showDetail(true);
-        if (!mState.value) {
-            mController.setWifiEnabled(true);
-            mState.value = true;
-        }
     }
 
     @Override
@@ -234,15 +233,15 @@
         @Override
         public String toString() {
             return new StringBuilder("CallbackInfo[")
-                .append("enabled=").append(enabled)
-                .append(",connected=").append(connected)
-                .append(",wifiSignalIconId=").append(wifiSignalIconId)
-                .append(",enabledDesc=").append(enabledDesc)
-                .append(",activityIn=").append(activityIn)
-                .append(",activityOut=").append(activityOut)
-                .append(",wifiSignalContentDescription=").append(wifiSignalContentDescription)
-                .append(",isTransient=").append(isTransient)
-                .append(']').toString();
+                    .append("enabled=").append(enabled)
+                    .append(",connected=").append(connected)
+                    .append(",wifiSignalIconId=").append(wifiSignalIconId)
+                    .append(",enabledDesc=").append(enabledDesc)
+                    .append(",activityIn=").append(activityIn)
+                    .append(",activityOut=").append(activityOut)
+                    .append(",wifiSignalContentDescription=").append(wifiSignalContentDescription)
+                    .append(",isTransient=").append(isTransient)
+                    .append(']').toString();
         }
     }
 
@@ -261,9 +260,14 @@
             mInfo.activityOut = activityOut;
             mInfo.wifiSignalContentDescription = qsIcon.contentDescription;
             mInfo.isTransient = isTransient;
+            if (isShowingDetail()) {
+                mDetailAdapter.updateItems();
+            }
             refreshState(mInfo);
         }
-    };
+    }
+
+    ;
 
     protected class WifiDetailAdapter implements DetailAdapter,
             NetworkController.AccessPointController.AccessPointCallback, QSDetailItems.Callback {
@@ -290,7 +294,6 @@
             if (DEBUG) Log.d(TAG, "setToggleState " + state);
             MetricsLogger.action(mContext, MetricsEvent.QS_WIFI_TOGGLE, state);
             mController.setWifiEnabled(state);
-            showDetail(false);
         }
 
         @Override
@@ -307,8 +310,6 @@
             mItems = QSDetailItems.convertOrInflate(context, convertView, parent);
             mItems.setTagSuffix("Wifi");
             mItems.setCallback(this);
-            mItems.setEmptyState(R.drawable.ic_qs_wifi_detail_empty,
-                    R.string.quick_settings_wifi_detail_empty_text);
             updateItems();
             setItemsVisible(mState.value);
             return mItems;
@@ -352,6 +353,13 @@
 
         private void updateItems() {
             if (mItems == null) return;
+            if (mSignalCallback.mInfo.enabled) {
+                mItems.setEmptyState(R.drawable.ic_qs_wifi_detail_empty,
+                        R.string.quick_settings_wifi_detail_empty_text);
+            } else {
+                mItems.setEmptyState(R.drawable.ic_qs_wifi_detail_empty,
+                        R.string.wifi_is_off);
+            }
             Item[] items = null;
             if (mAccessPoints != null) {
                 items = new Item[mAccessPoints.length];
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java b/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java
index 9176f57..677642e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java
@@ -328,6 +328,9 @@
         mIsColorized = mStatusBarNotification.getNotification().isColorized();
         mShowingPublicInitialized = false;
         updateNotificationColor();
+        if (mMenuRow != null) {
+            mMenuRow.onNotificationUpdated();
+        }
         if (mIsSummaryWithChildren) {
             mChildrenContainer.recreateNotificationHeader(mExpandClickListener);
             mChildrenContainer.onNotificationUpdated();
@@ -760,7 +763,10 @@
         }
         mMenuRow = plugin;
         if (mMenuRow.useDefaultMenuItems()) {
-            mMenuRow.setMenuItems(NotificationMenuRow.getDefaultMenuItems(mContext));
+            ArrayList<MenuItem> items = new ArrayList<>();
+            items.add(NotificationMenuRow.createInfoItem(mContext));
+            items.add(NotificationMenuRow.createSnoozeItem(mContext));
+            mMenuRow.setMenuItems(items);
         }
         if (existed) {
             createMenu();
@@ -787,7 +793,6 @@
         return mMenuRow;
     }
 
-
     public NotificationMenuRowPlugin getProvider() {
         return mMenuRow;
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMenuRow.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMenuRow.java
index bb82b60..2a1e063 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMenuRow.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMenuRow.java
@@ -30,11 +30,13 @@
 import android.animation.Animator;
 import android.animation.AnimatorListenerAdapter;
 import android.animation.ValueAnimator;
+import android.app.Notification;
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
 import android.os.Handler;
 import android.util.Log;
+import android.service.notification.StatusBarNotification;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
@@ -48,12 +50,12 @@
     private static final long SHOW_MENU_DELAY = 60;
     private static final long SWIPE_MENU_TIMING = 200;
 
-    private static final int NOTIFICATION_INFO_INDEX = 1;
-
     private ExpandableNotificationRow mParent;
 
     private Context mContext;
     private FrameLayout mMenuContainer;
+    private MenuItem mSnoozeItem;
+    private MenuItem mInfoItem;
     private ArrayList<MenuItem> mMenuItems;
     private OnMenuEventListener mMenuListener;
 
@@ -94,7 +96,11 @@
         mVertSpaceForIcons = res.getDimensionPixelSize(R.dimen.notification_min_height);
         mIconPadding = res.getDimensionPixelSize(R.dimen.notification_menu_icon_padding);
         mHandler = new Handler();
-        mMenuItems = getDefaultMenuItems(context);
+        mMenuItems = new ArrayList<>();
+        mSnoozeItem = createSnoozeItem(context);
+        mInfoItem = createInfoItem(context);
+        mMenuItems.add(mSnoozeItem);
+        mMenuItems.add(mInfoItem);
     }
 
     @Override
@@ -104,7 +110,7 @@
 
     @Override
     public MenuItem getLongpressMenuItem(Context context) {
-        return mMenuItems.get(NOTIFICATION_INFO_INDEX);
+        return mInfoItem;
     }
 
     @Override
@@ -120,14 +126,7 @@
     @Override
     public void createMenu(ViewGroup parent) {
         mParent = (ExpandableNotificationRow) parent;
-        if (mMenuContainer != null) {
-            mMenuContainer.removeAllViews();
-        }
-        mMenuContainer = new FrameLayout(mContext);
-        for (int i = 0; i < mMenuItems.size(); i++) {
-            addMenuView(mMenuItems.get(i), mMenuContainer);
-        }
-        resetState(false);
+        createMenuViews();
     }
 
     @Override
@@ -145,6 +144,40 @@
         resetState(true);
     }
 
+    @Override
+    public void onNotificationUpdated() {
+        if (mMenuContainer == null) {
+            // Menu hasn't been created yet, no need to do anything.
+            return;
+        }
+        createMenuViews();
+    }
+
+    private void createMenuViews() {
+        // Filter the menu items based on the notification
+        if (mParent != null && mParent.getStatusBarNotification() != null) {
+            int flags = mParent.getStatusBarNotification().getNotification().flags;
+            boolean isForeground = (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
+            if (isForeground) {
+                // Don't show snooze for foreground services
+                mMenuItems.remove(mSnoozeItem);
+            } else if (!mMenuItems.contains(mSnoozeItem)) {
+                // Was a foreground service but is no longer, add snooze back
+                mMenuItems.add(mSnoozeItem);
+            }
+        }
+        // Recreate the menu
+        if (mMenuContainer != null) {
+            mMenuContainer.removeAllViews();
+        } else {
+            mMenuContainer = new FrameLayout(mContext);
+        }
+        for (int i = 0; i < mMenuItems.size(); i++) {
+            addMenuView(mMenuItems.get(i), mMenuContainer);
+        }
+        resetState(false /* notify */);
+    }
+
     private void resetState(boolean notify) {
         setMenuAlpha(0f);
         mIconsPlaced = false;
@@ -495,24 +528,24 @@
         // TODO -- handle / allow custom menu items!
     }
 
-    public static ArrayList<MenuItem> getDefaultMenuItems(Context context) {
-        ArrayList<MenuItem> items = new ArrayList<MenuItem>();
+    public static MenuItem createSnoozeItem(Context context) {
         Resources res = context.getResources();
-
         NotificationSnooze content = (NotificationSnooze) LayoutInflater.from(context)
                 .inflate(R.layout.notification_snooze, null, false);
         String snoozeDescription = res.getString(R.string.notification_menu_snooze_description);
         MenuItem snooze = new NotificationMenuItem(context, snoozeDescription, content,
                 R.drawable.ic_snooze);
-        items.add(snooze);
+        return snooze;
+    }
 
-        String settingsDescription = res.getString(R.string.notification_menu_gear_description);
-        NotificationInfo settingsContent = (NotificationInfo) LayoutInflater.from(context).inflate(
+    public static MenuItem createInfoItem(Context context) {
+        Resources res = context.getResources();
+        String infoDescription = res.getString(R.string.notification_menu_gear_description);
+        NotificationInfo infoContent = (NotificationInfo) LayoutInflater.from(context).inflate(
                 R.layout.notification_info, null, false);
-        MenuItem settings = new NotificationMenuItem(context, settingsDescription, settingsContent,
+        MenuItem info = new NotificationMenuItem(context, infoDescription, infoContent,
                 R.drawable.ic_settings);
-        items.add(settings);
-        return items;
+        return info;
     }
 
     private void addMenuView(MenuItem item, ViewGroup parent) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
index f70a6a0..0b3cbd9 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
@@ -6377,17 +6377,10 @@
                                             .getIdentifier();
                                     if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)
                                             && mKeyguardManager.isDeviceLocked(userId)) {
-                                        boolean canBypass = false;
-                                        try {
-                                            canBypass = ActivityManager.getService()
-                                                    .canBypassWorkChallenge(intent);
-                                        } catch (RemoteException e) {
-                                        }
-                                        // For direct-boot aware activities, they can be shown when
-                                        // the device is still locked without triggering the work
-                                        // challenge.
-                                        if ((!canBypass) && startWorkChallengeIfNecessary(userId,
-                                                    intent.getIntentSender(), notificationKey)) {
+                                        // TODO(b/28935539): should allow certain activities to
+                                        // bypass work challenge
+                                        if (startWorkChallengeIfNecessary(userId,
+                                                intent.getIntentSender(), notificationKey)) {
                                             // Show work challenge, do not run PendingIntent and
                                             // remove notification
                                             return;
diff --git a/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java b/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java
index cfe16dd..c3a53de 100644
--- a/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java
+++ b/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java
@@ -387,7 +387,7 @@
                     .addAction(new Action(R.drawable.ic_eject_24dp,
                             mContext.getString(R.string.ext_media_unmount_action),
                             buildUnmountPendingIntent(vol)))
-                    .setContentIntent(buildUnmountPendingIntent(vol))
+                    .setContentIntent(browseIntent)
                     .setCategory(Notification.CATEGORY_SYSTEM);
             // Non-adoptable disks can't be snoozed.
             if (disk.isAdoptable()) {
diff --git a/packages/SystemUI/src/com/android/systemui/volume/ZenModePanel.java b/packages/SystemUI/src/com/android/systemui/volume/ZenModePanel.java
index 98e89e4..f7dfc0a 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/ZenModePanel.java
+++ b/packages/SystemUI/src/com/android/systemui/volume/ZenModePanel.java
@@ -36,6 +36,7 @@
 import android.service.notification.ZenModeConfig.ZenRule;
 import android.text.TextUtils;
 import android.text.format.DateFormat;
+import android.text.format.DateUtils;
 import android.util.ArraySet;
 import android.util.AttributeSet;
 import android.util.Log;
@@ -45,6 +46,7 @@
 import android.view.ViewGroup;
 import android.widget.CompoundButton;
 import android.widget.CompoundButton.OnCheckedChangeListener;
+import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.RadioButton;
@@ -55,6 +57,7 @@
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.systemui.Prefs;
 import com.android.systemui.R;
+import com.android.systemui.R.string;
 import com.android.systemui.statusbar.policy.ZenModeController;
 
 import java.io.FileDescriptor;
@@ -65,10 +68,14 @@
 import java.util.Locale;
 import java.util.Objects;
 
-public class ZenModePanel extends LinearLayout {
+public class ZenModePanel extends FrameLayout {
     private static final String TAG = "ZenModePanel";
     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
 
+    public static final int STATE_MODIFY = 0;
+    public static final int STATE_AUTO_RULE = 1;
+    public static final int STATE_OFF = 2;
+
     private static final int SECONDS_MS = 1000;
     private static final int MINUTES_MS = 60 * SECONDS_MS;
 
@@ -86,6 +93,8 @@
     public static final Intent ZEN_PRIORITY_SETTINGS
             = new Intent(Settings.ACTION_ZEN_MODE_PRIORITY_SETTINGS);
 
+    private static final long TRANSITION_DURATION = 300;
+
     private final Context mContext;
     protected final LayoutInflater mInflater;
     private final H mHandler = new H();
@@ -124,6 +133,13 @@
 
     protected int mZenModeConditionLayoutId;
     protected int mZenModeButtonLayoutId;
+    private View mEmpty;
+    private TextView mEmptyText;
+    private ImageView mEmptyIcon;
+    private View mAutoRule;
+    private TextView mAutoTitle;
+    private int mState = STATE_MODIFY;
+    private ViewGroup mEdit;
 
     public ZenModePanel(Context context, AttributeSet attrs) {
         super(context, attrs);
@@ -156,7 +172,7 @@
     }
 
     protected void createZenButtons() {
-        mZenButtons = (SegmentedButtons) findViewById(R.id.zen_buttons);
+        mZenButtons = findViewById(R.id.zen_buttons);
         mZenButtons.addButton(R.string.interruption_level_none_twoline,
                 R.string.interruption_level_none_with_warning,
                 Global.ZEN_MODE_NO_INTERRUPTIONS);
@@ -174,30 +190,75 @@
         super.onFinishInflate();
         createZenButtons();
         mZenIntroduction = findViewById(R.id.zen_introduction);
-        mZenIntroductionMessage = (TextView) findViewById(R.id.zen_introduction_message);
+        mZenIntroductionMessage = findViewById(R.id.zen_introduction_message);
         mZenIntroductionConfirm = findViewById(R.id.zen_introduction_confirm);
-        mZenIntroductionConfirm.setOnClickListener(new OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                confirmZenIntroduction();
-            }
-        });
-        mZenIntroductionCustomize = (TextView) findViewById(R.id.zen_introduction_customize);
-        mZenIntroductionCustomize.setOnClickListener(new OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                confirmZenIntroduction();
-                if (mCallback != null) {
-                    mCallback.onPrioritySettings();
-                }
+        mZenIntroductionConfirm.setOnClickListener(v -> confirmZenIntroduction());
+        mZenIntroductionCustomize = findViewById(R.id.zen_introduction_customize);
+        mZenIntroductionCustomize.setOnClickListener(v -> {
+            confirmZenIntroduction();
+            if (mCallback != null) {
+                mCallback.onPrioritySettings();
             }
         });
         mConfigurableTexts.add(mZenIntroductionCustomize, R.string.zen_priority_customize_button);
 
-        mZenConditions = (LinearLayout) findViewById(R.id.zen_conditions);
-        mZenAlarmWarning = (TextView) findViewById(R.id.zen_alarm_warning);
-        mZenRadioGroup = (RadioGroup) findViewById(R.id.zen_radio_buttons);
-        mZenRadioGroupContent = (LinearLayout) findViewById(R.id.zen_radio_buttons_content);
+        mZenConditions = findViewById(R.id.zen_conditions);
+        mZenAlarmWarning = findViewById(R.id.zen_alarm_warning);
+        mZenRadioGroup = findViewById(R.id.zen_radio_buttons);
+        mZenRadioGroupContent = findViewById(R.id.zen_radio_buttons_content);
+
+        mEdit = findViewById(R.id.edit_container);
+
+        mEmpty = findViewById(android.R.id.empty);
+        mEmpty.setVisibility(INVISIBLE);
+        mEmptyText = mEmpty.findViewById(android.R.id.title);
+        mEmptyIcon = mEmpty.findViewById(android.R.id.icon);
+
+        mAutoRule = findViewById(R.id.auto_rule);
+        mAutoTitle = mAutoRule.findViewById(android.R.id.title);
+        mAutoRule.setVisibility(INVISIBLE);
+    }
+
+    public void setEmptyState(int icon, int text) {
+        mEmptyIcon.post(() -> {
+            mEmptyIcon.setImageResource(icon);
+            mEmptyText.setText(text);
+        });
+    }
+
+    public void setAutoText(CharSequence text) {
+        mAutoTitle.post(() -> mAutoTitle.setText(text));
+    }
+
+    public void setState(int state) {
+        if (mState == state) return;
+        transitionFrom(getView(mState), getView(state));
+        mState = state;
+    }
+
+    private void transitionFrom(View from, View to) {
+        from.post(() -> {
+            // TODO: Better transitions
+            to.setAlpha(0);
+            to.setVisibility(VISIBLE);
+            to.bringToFront();
+            to.animate().cancel();
+            to.animate().alpha(1)
+                    .setDuration(TRANSITION_DURATION)
+                    .withEndAction(() -> from.setVisibility(INVISIBLE))
+                    .start();
+        });
+    }
+
+    private View getView(int state) {
+        switch (state) {
+            case STATE_AUTO_RULE:
+                return mAutoRule;
+            case STATE_OFF:
+                return mEmpty;
+            default:
+                return mEdit;
+        }
     }
 
     @Override
@@ -232,6 +293,7 @@
     protected void onAttachedToWindow() {
         super.onAttachedToWindow();
         if (DEBUG) Log.d(mTag, "onAttachedToWindow");
+        setExpanded(true);
         mAttached = true;
         mAttachedZen = getSelectedZen(-1);
         mSessionZen = mAttachedZen;
@@ -246,6 +308,7 @@
     protected void onDetachedFromWindow() {
         super.onDetachedFromWindow();
         if (DEBUG) Log.d(mTag, "onDetachedFromWindow");
+        setExpanded(false);
         checkForAttachedZenChange();
         mAttached = false;
         mAttachedZen = -1;
@@ -285,7 +348,7 @@
         if (expanded == mExpanded) return;
         if (DEBUG) Log.d(mTag, "setExpanded " + expanded);
         mExpanded = expanded;
-        if (mExpanded && isShown()) {
+        if (mExpanded) {
             ensureSelection();
         }
         updateWidgets();
@@ -317,10 +380,10 @@
 
     protected void addZenConditions(int count) {
         for (int i = 0; i < count; i++) {
-            final View rb = mInflater.inflate(mZenModeButtonLayoutId, this, false);
+            final View rb = mInflater.inflate(mZenModeButtonLayoutId, mEdit, false);
             rb.setId(i);
             mZenRadioGroup.addView(rb);
-            final View rbc = mInflater.inflate(mZenModeConditionLayoutId, this, false);
+            final View rbc = mInflater.inflate(mZenModeConditionLayoutId, mEdit, false);
             rbc.setId(i + count);
             mZenRadioGroupContent.addView(rbc);
         }
@@ -368,13 +431,26 @@
     private void handleUpdateManualRule(ZenRule rule) {
         final int zen = rule != null ? rule.zenMode : Global.ZEN_MODE_OFF;
         handleUpdateZen(zen);
-        final Condition c = rule != null ? rule.condition : null;
+        final Condition c = rule == null ? null
+                : rule.condition != null ? rule.condition
+                : createCondition(rule.conditionId);
         handleExitConditionChanged(c);
     }
 
+    private Condition createCondition(Uri conditionId) {
+        if (ZenModeConfig.isValidCountdownConditionId(conditionId)) {
+            long time = ZenModeConfig.tryParseCountdownConditionId(conditionId);
+            int mins = (int) ((time - System.currentTimeMillis() + DateUtils.MINUTE_IN_MILLIS / 2)
+                    / DateUtils.MINUTE_IN_MILLIS);
+            Condition c = ZenModeConfig.toTimeCondition(mContext, time, mins,
+                    ActivityManager.getCurrentUser(), false);
+            return c;
+        }
+        return null;
+    }
+
     private void handleUpdateZen(int zen) {
         if (mSessionZen != -1 && mSessionZen != zen) {
-            setExpanded(isShown());
             mSessionZen = zen;
         }
         mZenButtons.setSelectedValue(zen, false /* fromClick */);
@@ -391,15 +467,20 @@
     private void handleExitConditionChanged(Condition exitCondition) {
         setExitCondition(exitCondition);
         if (DEBUG) Log.d(mTag, "handleExitConditionChanged " + mExitCondition);
+        if (exitCondition == null) return;
         final int N = getVisibleConditions();
         for (int i = 0; i < N; i++) {
             final ConditionTag tag = getConditionTagAt(i);
-            if (tag != null) {
-                if (sameConditionId(tag.condition, mExitCondition)) {
-                    bind(exitCondition, mZenRadioGroupContent.getChildAt(i), i);
-                }
+            if (tag != null && sameConditionId(tag.condition, mExitCondition)) {
+                bind(exitCondition, mZenRadioGroupContent.getChildAt(i), i);
+                return;
             }
         }
+        if (mCountdownConditionSupported && ZenModeConfig.isValidCountdownConditionId(
+                exitCondition.id)) {
+            bind(exitCondition, mZenRadioGroupContent.getChildAt(COUNTDOWN_CONDITION_INDEX),
+                    COUNTDOWN_CONDITION_INDEX);
+        }
     }
 
     private Condition getSelectedCondition() {
@@ -519,7 +600,7 @@
             }
         }
         // ensure something is selected
-        if (mExpanded && isShown()) {
+        if (mExpanded) {
             ensureSelection();
         }
         mZenConditions.setVisibility(mSessionZen != Global.ZEN_MODE_OFF ? View.VISIBLE : View.GONE);
@@ -597,7 +678,9 @@
         if (foreverTag == null) return;
         if (DEBUG) Log.d(mTag, "Selecting a default");
         final int favoriteIndex = mPrefs.getMinuteIndex();
-        if (favoriteIndex == -1 || !mCountdownConditionSupported) {
+        if (mExitCondition != null && mExitCondition.equals(mTimeCondition)) {
+            getConditionTagAt(COUNTDOWN_CONDITION_INDEX).rb.setChecked(true);
+        } else if (favoriteIndex == -1 || !mCountdownConditionSupported) {
             foreverTag.rb.setChecked(true);
         } else {
             mTimeCondition = ZenModeConfig.toTimeCondition(mContext,
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationMenuRowTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationMenuRowTest.java
index efb9fea..9249df6 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationMenuRowTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationMenuRowTest.java
@@ -14,6 +14,8 @@
 
 package com.android.systemui.statusbar;
 
+import static junit.framework.Assert.assertTrue;
+
 import android.testing.AndroidTestingRunner;
 import android.testing.TestableLooper;
 import android.testing.TestableLooper.RunWithLooper;
@@ -49,6 +51,8 @@
     public void testRecreateMenu() {
         NotificationMenuRowPlugin row = new NotificationMenuRow(mContext);
         row.createMenu(null);
+        assertTrue(row.getMenuView() != null);
         row.createMenu(null);
+        assertTrue(row.getMenuView() != null);
     }
 }
diff --git a/services/backup/java/com/android/server/backup/BackupRestoreTask.java b/services/backup/java/com/android/server/backup/BackupRestoreTask.java
index eab1c76..acaab0c 100644
--- a/services/backup/java/com/android/server/backup/BackupRestoreTask.java
+++ b/services/backup/java/com/android/server/backup/BackupRestoreTask.java
@@ -19,7 +19,7 @@
 /**
  * Interface and methods used by the asynchronous-with-timeout backup/restore operations.
  */
-interface BackupRestoreTask {
+public interface BackupRestoreTask {
 
     // Execute one tick of whatever state machine the task implements
     void execute();
diff --git a/services/backup/java/com/android/server/backup/FileMetadata.java b/services/backup/java/com/android/server/backup/FileMetadata.java
index 0f71da9..4ece639 100644
--- a/services/backup/java/com/android/server/backup/FileMetadata.java
+++ b/services/backup/java/com/android/server/backup/FileMetadata.java
@@ -19,15 +19,15 @@
 /**
  * Description of a file in the restore datastream.
  */
-class FileMetadata {
-    String packageName;             // name of the owning app
-    String installerPackageName;    // name of the market-type app that installed the owner
-    int type;                       // e.g. BackupAgent.TYPE_DIRECTORY
-    String domain;                  // e.g. FullBackup.DATABASE_TREE_TOKEN
-    String path;                    // subpath within the semantic domain
-    long mode;                      // e.g. 0666 (actually int)
-    long mtime;                     // last mod time, UTC time_t (actually int)
-    long size;                      // bytes of content
+public class FileMetadata {
+    public String packageName;             // name of the owning app
+    public String installerPackageName;    // name of the market-type app that installed the owner
+    public int type;                       // e.g. BackupAgent.TYPE_DIRECTORY
+    public String domain;                  // e.g. FullBackup.DATABASE_TREE_TOKEN
+    public String path;                    // subpath within the semantic domain
+    public long mode;                      // e.g. 0666 (actually int)
+    public long mtime;                     // last mod time, UTC time_t (actually int)
+    public long size;                      // bytes of content
 
     @Override
     public String toString() {
diff --git a/services/backup/java/com/android/server/backup/KeyValueAdbBackupEngine.java b/services/backup/java/com/android/server/backup/KeyValueAdbBackupEngine.java
index d3577e0..279c828 100644
--- a/services/backup/java/com/android/server/backup/KeyValueAdbBackupEngine.java
+++ b/services/backup/java/com/android/server/backup/KeyValueAdbBackupEngine.java
@@ -35,7 +35,7 @@
  * TODO: We should create unified backup/restore engines that can be used for both transport and
  * adb backup/restore, and for fullbackup and key-value backup.
  */
-class KeyValueAdbBackupEngine {
+public class KeyValueAdbBackupEngine {
     private static final String TAG = "KeyValueAdbBackupEngine";
     private static final boolean DEBUG = false;
 
@@ -58,8 +58,8 @@
     private ParcelFileDescriptor mBackupData;
     private ParcelFileDescriptor mNewState;
 
-    KeyValueAdbBackupEngine(OutputStream output, PackageInfo packageInfo,
-        BackupManagerServiceInterface backupManagerService, PackageManager packageManager,
+    public KeyValueAdbBackupEngine(OutputStream output, PackageInfo packageInfo,
+            BackupManagerServiceInterface backupManagerService, PackageManager packageManager,
             File baseStateDir, File dataDir) {
         mOutput = output;
         mCurrentPackage = packageInfo;
@@ -81,7 +81,7 @@
         mManifestFile = new File(mDataDir, BackupManagerService.BACKUP_MANIFEST_FILENAME);
     }
 
-    void backupOnePackage() throws IOException {
+    public void backupOnePackage() throws IOException {
         ApplicationInfo targetApp = mCurrentPackage.applicationInfo;
 
         try {
diff --git a/services/backup/java/com/android/server/backup/KeyValueAdbRestoreEngine.java b/services/backup/java/com/android/server/backup/KeyValueAdbRestoreEngine.java
index 38791c1..b62bb5c 100644
--- a/services/backup/java/com/android/server/backup/KeyValueAdbRestoreEngine.java
+++ b/services/backup/java/com/android/server/backup/KeyValueAdbRestoreEngine.java
@@ -33,7 +33,7 @@
  * TODO: We should create unified backup/restore engines that can be used for both transport and
  * adb backup/restore, and for fullbackup and key-value backup.
  */
-class KeyValueAdbRestoreEngine implements Runnable {
+public class KeyValueAdbRestoreEngine implements Runnable {
     private static final String TAG = "KeyValueAdbRestoreEngine";
     private static final boolean DEBUG = false;
 
@@ -46,8 +46,9 @@
     IBackupAgent mAgent;
     int mToken;
 
-    KeyValueAdbRestoreEngine(BackupManagerServiceInterface backupManagerService, File dataDir,
-            FileMetadata info, ParcelFileDescriptor inFD, IBackupAgent agent, int token) {
+    public KeyValueAdbRestoreEngine(BackupManagerServiceInterface backupManagerService,
+            File dataDir, FileMetadata info, ParcelFileDescriptor inFD, IBackupAgent agent,
+            int token) {
         mBackupManagerService = backupManagerService;
         mDataDir = dataDir;
         mInfo = info;
diff --git a/services/backup/java/com/android/server/backup/PackageManagerBackupAgent.java b/services/backup/java/com/android/server/backup/PackageManagerBackupAgent.java
index 09f240f..92f00b8 100644
--- a/services/backup/java/com/android/server/backup/PackageManagerBackupAgent.java
+++ b/services/backup/java/com/android/server/backup/PackageManagerBackupAgent.java
@@ -108,11 +108,11 @@
 
     // We're constructed with the set of applications that are participating
     // in backup.  This set changes as apps are installed & removed.
-    PackageManagerBackupAgent(PackageManager packageMgr, List<PackageInfo> packages) {
+    public PackageManagerBackupAgent(PackageManager packageMgr, List<PackageInfo> packages) {
         init(packageMgr, packages);
     }
 
-    PackageManagerBackupAgent(PackageManager packageMgr) {
+    public PackageManagerBackupAgent(PackageManager packageMgr) {
         init(packageMgr, null);
 
         evaluateStorablePackages();
diff --git a/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java b/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java
index ba4238a..3e1f8ee 100644
--- a/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java
+++ b/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java
@@ -17,50 +17,24 @@
 package com.android.server.backup;
 
 import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_NAME;
-import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_VERSION;
-import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_MANIFEST_PACKAGE_NAME;
-import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_OLD_VERSION;
-import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_POLICY_ALLOW_APKS;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_APK_NOT_INSTALLED;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_CANNOT_RESTORE_WITHOUT_APK;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_EXPECTED_DIFFERENT_PACKAGE;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_ALLOW_BACKUP_FALSE;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_SIGNATURE_MISMATCH;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_MISSING_SIGNATURE;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_RESTORE_ANY_VERSION;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_SYSTEM_APP_NO_AGENT;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_VERSIONS_MATCH;
-import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_VERSION_OF_BACKUP_OLDER;
 import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_BACKUP_IN_FOREGROUND;
 
 import android.app.ActivityManager;
 import android.app.AlarmManager;
 import android.app.AppGlobals;
-import android.app.ApplicationThreadConstants;
 import android.app.IActivityManager;
 import android.app.IBackupAgent;
-import android.app.PackageInstallObserver;
 import android.app.PendingIntent;
-import android.app.backup.BackupAgent;
-import android.app.backup.BackupDataInput;
-import android.app.backup.BackupDataOutput;
 import android.app.backup.BackupManager;
 import android.app.backup.BackupManagerMonitor;
 import android.app.backup.BackupProgress;
-import android.app.backup.BackupTransport;
 import android.app.backup.FullBackup;
-import android.app.backup.FullBackupDataOutput;
 import android.app.backup.IBackupManager;
 import android.app.backup.IBackupManagerMonitor;
 import android.app.backup.IBackupObserver;
 import android.app.backup.IFullBackupRestoreObserver;
-import android.app.backup.IRestoreObserver;
 import android.app.backup.IRestoreSession;
 import android.app.backup.ISelectBackupTransportCallback;
-import android.app.backup.RestoreDescription;
-import android.app.backup.RestoreSet;
 import android.app.backup.SelectBackupTransportCallback;
 import android.content.ActivityNotFoundException;
 import android.content.BroadcastReceiver;
@@ -69,10 +43,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.ServiceConnection;
 import android.content.pm.ApplicationInfo;
-import android.content.pm.IPackageDataObserver;
-import android.content.pm.IPackageDeleteObserver;
 import android.content.pm.IPackageManager;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
@@ -84,11 +55,8 @@
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Environment;
-import android.os.Environment.UserEnvironment;
-import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.IBinder;
-import android.os.Looper;
 import android.os.Message;
 import android.os.ParcelFileDescriptor;
 import android.os.PowerManager;
@@ -99,33 +67,44 @@
 import android.os.ServiceManager;
 import android.os.SystemClock;
 import android.os.UserHandle;
-import android.os.WorkSource;
 import android.os.storage.IStorageManager;
 import android.os.storage.StorageManager;
 import android.provider.Settings;
-import android.system.ErrnoException;
-import android.system.Os;
 import android.text.TextUtils;
 import android.util.AtomicFile;
 import android.util.EventLog;
-import android.util.Log;
 import android.util.Pair;
 import android.util.Slog;
 import android.util.SparseArray;
 import android.util.StringBuilderPrinter;
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.backup.IBackupTransport;
-import com.android.internal.backup.IObbBackupService;
 import com.android.internal.util.DumpUtils;
 import com.android.server.AppWidgetBackupBridge;
 import com.android.server.EventLogTags;
 import com.android.server.SystemConfig;
 import com.android.server.SystemService;
-import com.android.server.backup.PackageManagerBackupAgent.Metadata;
+import com.android.server.backup.fullbackup.FullBackupEntry;
+import com.android.server.backup.fullbackup.PerformFullTransportBackupTask;
+import com.android.server.backup.internal.BackupHandler;
+import com.android.server.backup.internal.BackupRequest;
+import com.android.server.backup.internal.ClearDataObserver;
+import com.android.server.backup.internal.Operation;
+import com.android.server.backup.internal.ProvisionedObserver;
+import com.android.server.backup.internal.RunBackupReceiver;
+import com.android.server.backup.internal.RunInitializeReceiver;
+import com.android.server.backup.params.AdbBackupParams;
+import com.android.server.backup.params.AdbParams;
+import com.android.server.backup.params.AdbRestoreParams;
+import com.android.server.backup.params.BackupParams;
+import com.android.server.backup.params.ClearParams;
+import com.android.server.backup.params.ClearRetryParams;
+import com.android.server.backup.params.RestoreParams;
+import com.android.server.backup.restore.ActiveRestoreSession;
+import com.android.server.backup.restore.PerformUnifiedRestoreTask;
 import com.android.server.power.BatterySaverPolicy.ServiceType;
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
@@ -140,10 +119,7 @@
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.RandomAccessFile;
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
 import java.security.Key;
-import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 import java.security.spec.InvalidKeySpecException;
@@ -151,46 +127,26 @@
 import java.text.SimpleDateFormat;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
-import java.util.Map.Entry;
-import java.util.Objects;
 import java.util.Queue;
 import java.util.Random;
 import java.util.Set;
-import java.util.TreeMap;
 import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.zip.Deflater;
-import java.util.zip.DeflaterOutputStream;
-import java.util.zip.InflaterInputStream;
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.CipherInputStream;
-import javax.crypto.CipherOutputStream;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
 import javax.crypto.SecretKey;
 import javax.crypto.SecretKeyFactory;
-import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.PBEKeySpec;
-import javax.crypto.spec.SecretKeySpec;
 import libcore.io.IoUtils;
 
 public class RefactoredBackupManagerService implements BackupManagerServiceInterface {
 
-    private static final String TAG = "BackupManagerService";
-    private static final boolean DEBUG = true;
-    private static final boolean MORE_DEBUG = false;
-    private static final boolean DEBUG_SCHEDULING = MORE_DEBUG || true;
+    public static final String TAG = "BackupManagerService";
+    public static final boolean DEBUG = true;
+    public static final boolean MORE_DEBUG = false;
+    public static final boolean DEBUG_SCHEDULING = MORE_DEBUG || true;
 
     // File containing backup-enabled state.  Contains a single byte;
     // nonzero == enabled.  File missing or contains a zero byte == disabled.
@@ -199,19 +155,19 @@
     // System-private key used for backing up an app's widget state.  Must
     // begin with U+FFxx by convention (we reserve all keys starting
     // with U+FF00 or higher for system use).
-    private static final String KEY_WIDGET_STATE = "\uffed\uffedwidget";
+    public static final String KEY_WIDGET_STATE = "\uffed\uffedwidget";
 
     // Historical and current algorithm names
-    private static final String PBKDF_CURRENT = "PBKDF2WithHmacSHA1";
-    private static final String PBKDF_FALLBACK = "PBKDF2WithHmacSHA1And8bit";
+    public static final String PBKDF_CURRENT = "PBKDF2WithHmacSHA1";
+    public static final String PBKDF_FALLBACK = "PBKDF2WithHmacSHA1And8bit";
 
     // Name and current contents version of the full-backup manifest file
     //
     // Manifest version history:
     //
     // 1 : initial release
-    private static final String BACKUP_MANIFEST_FILENAME = "_manifest";
-    private static final int BACKUP_MANIFEST_VERSION = 1;
+    public static final String BACKUP_MANIFEST_FILENAME = "_manifest";
+    public static final int BACKUP_MANIFEST_VERSION = 1;
 
     // External archive format version history:
     //
@@ -220,68 +176,68 @@
     // 3 : introduced "_meta" metadata file; no other format change per se
     // 4 : added support for new device-encrypted storage locations
     // 5 : added support for key-value packages
-    private static final int BACKUP_FILE_VERSION = 5;
-    private static final String BACKUP_FILE_HEADER_MAGIC = "ANDROID BACKUP\n";
+    public static final int BACKUP_FILE_VERSION = 5;
+    public static final String BACKUP_FILE_HEADER_MAGIC = "ANDROID BACKUP\n";
     private static final int BACKUP_PW_FILE_VERSION = 2;
-    private static final String BACKUP_METADATA_FILENAME = "_meta";
-    private static final int BACKUP_METADATA_VERSION = 1;
-    private static final int BACKUP_WIDGET_METADATA_TOKEN = 0x01FFED01;
+    public static final String BACKUP_METADATA_FILENAME = "_meta";
+    public static final int BACKUP_METADATA_VERSION = 1;
+    public static final int BACKUP_WIDGET_METADATA_TOKEN = 0x01FFED01;
 
-    private static final int TAR_HEADER_LONG_RADIX = 8;
-    private static final int TAR_HEADER_OFFSET_FILESIZE = 124;
-    private static final int TAR_HEADER_LENGTH_FILESIZE = 12;
-    private static final int TAR_HEADER_OFFSET_MODTIME = 136;
-    private static final int TAR_HEADER_LENGTH_MODTIME = 12;
-    private static final int TAR_HEADER_OFFSET_MODE = 100;
-    private static final int TAR_HEADER_LENGTH_MODE = 8;
-    private static final int TAR_HEADER_OFFSET_PATH_PREFIX = 345;
-    private static final int TAR_HEADER_LENGTH_PATH_PREFIX = 155;
-    private static final int TAR_HEADER_OFFSET_PATH = 0;
-    private static final int TAR_HEADER_LENGTH_PATH = 100;
-    private static final int TAR_HEADER_OFFSET_TYPE_CHAR = 156;
+    public static final int TAR_HEADER_LONG_RADIX = 8;
+    public static final int TAR_HEADER_OFFSET_FILESIZE = 124;
+    public static final int TAR_HEADER_LENGTH_FILESIZE = 12;
+    public static final int TAR_HEADER_OFFSET_MODTIME = 136;
+    public static final int TAR_HEADER_LENGTH_MODTIME = 12;
+    public static final int TAR_HEADER_OFFSET_MODE = 100;
+    public static final int TAR_HEADER_LENGTH_MODE = 8;
+    public static final int TAR_HEADER_OFFSET_PATH_PREFIX = 345;
+    public static final int TAR_HEADER_LENGTH_PATH_PREFIX = 155;
+    public static final int TAR_HEADER_OFFSET_PATH = 0;
+    public static final int TAR_HEADER_LENGTH_PATH = 100;
+    public static final int TAR_HEADER_OFFSET_TYPE_CHAR = 156;
 
     private static final boolean COMPRESS_FULL_BACKUPS = true; // should be true in production
 
-    private static final String SETTINGS_PACKAGE = "com.android.providers.settings";
-    private static final String SHARED_BACKUP_AGENT_PACKAGE = "com.android.sharedstoragebackup";
+    public static final String SETTINGS_PACKAGE = "com.android.providers.settings";
+    public static final String SHARED_BACKUP_AGENT_PACKAGE = "com.android.sharedstoragebackup";
     private static final String SERVICE_ACTION_TRANSPORT_HOST = "android.backup.TRANSPORT_HOST";
 
     // Retry interval for clear/init when the transport is unavailable
     private static final long TRANSPORT_RETRY_INTERVAL = 1 * AlarmManager.INTERVAL_HOUR;
 
-    private static final String RUN_BACKUP_ACTION = "android.app.backup.intent.RUN";
-    private static final String RUN_INITIALIZE_ACTION = "android.app.backup.intent.INIT";
-    private static final int MSG_RUN_BACKUP = 1;
-    private static final int MSG_RUN_ADB_BACKUP = 2;
-    private static final int MSG_RUN_RESTORE = 3;
-    private static final int MSG_RUN_CLEAR = 4;
-    private static final int MSG_RUN_INITIALIZE = 5;
-    private static final int MSG_RUN_GET_RESTORE_SETS = 6;
-    private static final int MSG_RESTORE_SESSION_TIMEOUT = 8;
-    private static final int MSG_FULL_CONFIRMATION_TIMEOUT = 9;
-    private static final int MSG_RUN_ADB_RESTORE = 10;
-    private static final int MSG_RETRY_INIT = 11;
-    private static final int MSG_RETRY_CLEAR = 12;
-    private static final int MSG_WIDGET_BROADCAST = 13;
-    private static final int MSG_RUN_FULL_TRANSPORT_BACKUP = 14;
-    private static final int MSG_REQUEST_BACKUP = 15;
-    private static final int MSG_SCHEDULE_BACKUP_PACKAGE = 16;
-    private static final int MSG_BACKUP_OPERATION_TIMEOUT = 17;
-    private static final int MSG_RESTORE_OPERATION_TIMEOUT = 18;
+    public static final String RUN_BACKUP_ACTION = "android.app.backup.intent.RUN";
+    public static final String RUN_INITIALIZE_ACTION = "android.app.backup.intent.INIT";
+    public static final int MSG_RUN_BACKUP = 1;
+    public static final int MSG_RUN_ADB_BACKUP = 2;
+    public static final int MSG_RUN_RESTORE = 3;
+    public static final int MSG_RUN_CLEAR = 4;
+    public static final int MSG_RUN_INITIALIZE = 5;
+    public static final int MSG_RUN_GET_RESTORE_SETS = 6;
+    public static final int MSG_RESTORE_SESSION_TIMEOUT = 8;
+    public static final int MSG_FULL_CONFIRMATION_TIMEOUT = 9;
+    public static final int MSG_RUN_ADB_RESTORE = 10;
+    public static final int MSG_RETRY_INIT = 11;
+    public static final int MSG_RETRY_CLEAR = 12;
+    public static final int MSG_WIDGET_BROADCAST = 13;
+    public static final int MSG_RUN_FULL_TRANSPORT_BACKUP = 14;
+    public static final int MSG_REQUEST_BACKUP = 15;
+    public static final int MSG_SCHEDULE_BACKUP_PACKAGE = 16;
+    public static final int MSG_BACKUP_OPERATION_TIMEOUT = 17;
+    public static final int MSG_RESTORE_OPERATION_TIMEOUT = 18;
 
     // backup task state machine tick
-    private static final int MSG_BACKUP_RESTORE_STEP = 20;
-    private static final int MSG_OP_COMPLETE = 21;
+    public static final int MSG_BACKUP_RESTORE_STEP = 20;
+    public static final int MSG_OP_COMPLETE = 21;
 
     // Timeout interval for deciding that a bind or clear-data has taken too long
     private static final long TIMEOUT_INTERVAL = 10 * 1000;
 
     // Timeout intervals for agent backup & restore operations
-    private static final long TIMEOUT_BACKUP_INTERVAL = 30 * 1000;
-    private static final long TIMEOUT_FULL_BACKUP_INTERVAL = 5 * 60 * 1000;
+    public static final long TIMEOUT_BACKUP_INTERVAL = 30 * 1000;
+    public static final long TIMEOUT_FULL_BACKUP_INTERVAL = 5 * 60 * 1000;
     private static final long TIMEOUT_SHARED_BACKUP_INTERVAL = 30 * 60 * 1000;
-    private static final long TIMEOUT_RESTORE_INTERVAL = 60 * 1000;
-    private static final long TIMEOUT_RESTORE_FINISHED_INTERVAL = 30 * 1000;
+    public static final long TIMEOUT_RESTORE_INTERVAL = 60 * 1000;
+    public static final long TIMEOUT_RESTORE_FINISHED_INTERVAL = 30 * 1000;
 
     // User confirmation timeout for a full backup/restore operation.  It's this long in
     // order to give them time to enter the backup password.
@@ -295,52 +251,41 @@
     private static final long BUSY_BACKOFF_MIN_MILLIS = 1000 * 60 * 60;  // one hour
     private static final int BUSY_BACKOFF_FUZZ = 1000 * 60 * 60 * 2;  // two hours
 
-    private Context mContext;
-    private PackageManager mPackageManager;
-    private IPackageManager mPackageManagerBinder;
-    private IActivityManager mActivityManager;
+    public Context mContext;
+    public PackageManager mPackageManager;
+    public IPackageManager mPackageManagerBinder;
+    public IActivityManager mActivityManager;
     private PowerManager mPowerManager;
-    private AlarmManager mAlarmManager;
+    public AlarmManager mAlarmManager;
     private IStorageManager mStorageManager;
 
-    private IBackupManager mBackupManagerBinder;
+    public IBackupManager mBackupManagerBinder;
 
-    private final TransportManager mTransportManager;
+    public final TransportManager mTransportManager;
 
-    private boolean mEnabled;   // access to this is synchronized on 'this'
-    private boolean mProvisioned;
+    public boolean mEnabled;   // access to this is synchronized on 'this'
+    public boolean mProvisioned;
     private boolean mAutoRestore;
-    private PowerManager.WakeLock mWakelock;
+    public PowerManager.WakeLock mWakelock;
     private HandlerThread mHandlerThread;
-    private BackupHandler mBackupHandler;
+    public BackupHandler mBackupHandler;
     private PendingIntent mRunBackupIntent;
-    private PendingIntent mRunInitIntent;
+    public PendingIntent mRunInitIntent;
     private BroadcastReceiver mRunBackupReceiver;
     private BroadcastReceiver mRunInitReceiver;
     // map UIDs to the set of participating packages under that UID
     private final SparseArray<HashSet<String>> mBackupParticipants
             = new SparseArray<>();
-    // set of backup services that have pending changes
-    class BackupRequest {
-        public String packageName;
 
-        BackupRequest(String pkgName) {
-            packageName = pkgName;
-        }
-
-        public String toString() {
-            return "BackupRequest{pkg=" + packageName + "}";
-        }
-    }
     // Backups that we haven't started yet.  Keys are package names.
-    private HashMap<String, BackupRequest> mPendingBackups
+    public HashMap<String, BackupRequest> mPendingBackups
             = new HashMap<>();
 
     // Pseudoname that we use for the Package Manager metadata "package"
-    private static final String PACKAGE_MANAGER_SENTINEL = "@pm@";
+    public static final String PACKAGE_MANAGER_SENTINEL = "@pm@";
 
     // locking around the pending-backup management
-    private final Object mQueueLock = new Object();
+    public final Object mQueueLock = new Object();
 
     // The thread performing the sequence of queued backups binds to each app's agent
     // in succession.  Bind notifications are asynchronously delivered through the
@@ -348,24 +293,24 @@
     // completed.
     private final Object mAgentConnectLock = new Object();
     private IBackupAgent mConnectedAgent;
-    private volatile boolean mBackupRunning;
+    public volatile boolean mBackupRunning;
     private volatile boolean mConnecting;
-    private volatile long mLastBackupPass;
+    public volatile long mLastBackupPass;
 
     // For debugging, we maintain a progress trace of operations during backup
-    private static final boolean DEBUG_BACKUP_TRACE = true;
+    public static final boolean DEBUG_BACKUP_TRACE = true;
     private final List<String> mBackupTrace = new ArrayList<>();
 
     // A similar synchronization mechanism around clearing apps' data for restore
-    private final Object mClearDataLock = new Object();
-    private volatile boolean mClearingData;
+    public final Object mClearDataLock = new Object();
+    public volatile boolean mClearingData;
 
     @GuardedBy("mPendingRestores")
-    private boolean mIsRestoreInProgress;
+    public boolean mIsRestoreInProgress;
     @GuardedBy("mPendingRestores")
-    private final Queue<PerformUnifiedRestoreTask> mPendingRestores = new ArrayDeque<>();
+    public final Queue<PerformUnifiedRestoreTask> mPendingRestores = new ArrayDeque<>();
 
-    private ActiveRestoreSession mActiveRestoreSession;
+    public ActiveRestoreSession mActiveRestoreSession;
 
     // Watch the device provisioning operation during setup
     private ContentObserver mProvisionedObserver;
@@ -428,242 +373,20 @@
         }
     }
 
-    class ProvisionedObserver extends ContentObserver {
-        public ProvisionedObserver(Handler handler) {
-            super(handler);
-        }
-
-        public void onChange(boolean selfChange) {
-            final boolean wasProvisioned = mProvisioned;
-            final boolean isProvisioned = deviceIsProvisioned();
-            // latch: never unprovision
-            mProvisioned = wasProvisioned || isProvisioned;
-            if (MORE_DEBUG) {
-                Slog.d(TAG, "Provisioning change: was=" + wasProvisioned
-                        + " is=" + isProvisioned + " now=" + mProvisioned);
-            }
-
-            synchronized (mQueueLock) {
-                if (mProvisioned && !wasProvisioned && mEnabled) {
-                    // we're now good to go, so start the backup alarms
-                    if (MORE_DEBUG) Slog.d(TAG, "Now provisioned, so starting backups");
-                    KeyValueBackupJob.schedule(mContext);
-                    scheduleNextFullBackupJob(0);
-                }
-            }
-        }
-    }
-
-    class RestoreGetSetsParams {
-        public IBackupTransport transport;
-        public ActiveRestoreSession session;
-        public IRestoreObserver observer;
-        public IBackupManagerMonitor monitor;
-
-        RestoreGetSetsParams(IBackupTransport _transport, ActiveRestoreSession _session,
-                IRestoreObserver _observer, IBackupManagerMonitor _monitor) {
-            transport = _transport;
-            session = _session;
-            observer = _observer;
-            monitor = _monitor;
-        }
-    }
-
-    class RestoreParams {
-        public IBackupTransport transport;
-        public String dirName;
-        public IRestoreObserver observer;
-        public IBackupManagerMonitor monitor;
-        public long token;
-        public PackageInfo pkgInfo;
-        public int pmToken; // in post-install restore, the PM's token for this transaction
-        public boolean isSystemRestore;
-        public String[] filterSet;
-
-        /**
-         * Restore a single package; no kill after restore
-         */
-        RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
-                IBackupManagerMonitor _monitor, long _token, PackageInfo _pkg) {
-            transport = _transport;
-            dirName = _dirName;
-            observer = _obs;
-            monitor = _monitor;
-            token = _token;
-            pkgInfo = _pkg;
-            pmToken = 0;
-            isSystemRestore = false;
-            filterSet = null;
-        }
-
-        /**
-         * Restore at install: PM token needed, kill after restore
-         */
-        RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
-                IBackupManagerMonitor _monitor, long _token, String _pkgName, int _pmToken) {
-            transport = _transport;
-            dirName = _dirName;
-            observer = _obs;
-            monitor = _monitor;
-            token = _token;
-            pkgInfo = null;
-            pmToken = _pmToken;
-            isSystemRestore = false;
-            filterSet = new String[] { _pkgName };
-        }
-
-        /**
-         * Restore everything possible.  This is the form that Setup Wizard or similar
-         * restore UXes use.
-         */
-        RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
-                IBackupManagerMonitor _monitor, long _token) {
-            transport = _transport;
-            dirName = _dirName;
-            observer = _obs;
-            monitor = _monitor;
-            token = _token;
-            pkgInfo = null;
-            pmToken = 0;
-            isSystemRestore = true;
-            filterSet = null;
-        }
-
-        /**
-         * Restore some set of packages.  Leave this one up to the caller to specify
-         * whether it's to be considered a system-level restore.
-         */
-        RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
-                IBackupManagerMonitor _monitor, long _token,
-                String[] _filterSet, boolean _isSystemRestore) {
-            transport = _transport;
-            dirName = _dirName;
-            observer = _obs;
-            monitor = _monitor;
-            token = _token;
-            pkgInfo = null;
-            pmToken = 0;
-            isSystemRestore = _isSystemRestore;
-            filterSet = _filterSet;
-        }
-    }
-
-    class ClearParams {
-        public IBackupTransport transport;
-        public PackageInfo packageInfo;
-
-        ClearParams(IBackupTransport _transport, PackageInfo _info) {
-            transport = _transport;
-            packageInfo = _info;
-        }
-    }
-
-    class ClearRetryParams {
-        public String transportName;
-        public String packageName;
-
-        ClearRetryParams(String transport, String pkg) {
-            transportName = transport;
-            packageName = pkg;
-        }
-    }
-
-    // Parameters used by adbBackup() and adbRestore()
-    class AdbParams {
-        public ParcelFileDescriptor fd;
-        public final AtomicBoolean latch;
-        public IFullBackupRestoreObserver observer;
-        public String curPassword;     // filled in by the confirmation step
-        public String encryptPassword;
-
-        AdbParams() {
-            latch = new AtomicBoolean(false);
-        }
-    }
-
-    class AdbBackupParams extends AdbParams {
-        public boolean includeApks;
-        public boolean includeObbs;
-        public boolean includeShared;
-        public boolean doWidgets;
-        public boolean allApps;
-        public boolean includeSystem;
-        public boolean doCompress;
-        public boolean includeKeyValue;
-        public String[] packages;
-
-        AdbBackupParams(ParcelFileDescriptor output, boolean saveApks, boolean saveObbs,
-                boolean saveShared, boolean alsoWidgets, boolean doAllApps, boolean doSystem,
-                boolean compress, boolean doKeyValue, String[] pkgList) {
-            fd = output;
-            includeApks = saveApks;
-            includeObbs = saveObbs;
-            includeShared = saveShared;
-            doWidgets = alsoWidgets;
-            allApps = doAllApps;
-            includeSystem = doSystem;
-            doCompress = compress;
-            includeKeyValue = doKeyValue;
-            packages = pkgList;
-        }
-    }
-
-    class AdbRestoreParams extends AdbParams {
-        AdbRestoreParams(ParcelFileDescriptor input) {
-            fd = input;
-        }
-    }
-
-    class BackupParams {
-        public IBackupTransport transport;
-        public String dirName;
-        public ArrayList<String> kvPackages;
-        public ArrayList<String> fullPackages;
-        public IBackupObserver observer;
-        public IBackupManagerMonitor monitor;
-        public boolean userInitiated;
-        public boolean nonIncrementalBackup;
-
-        BackupParams(IBackupTransport transport, String dirName, ArrayList<String> kvPackages,
-                ArrayList<String> fullPackages, IBackupObserver observer,
-                IBackupManagerMonitor monitor,boolean userInitiated, boolean nonIncrementalBackup) {
-            this.transport = transport;
-            this.dirName = dirName;
-            this.kvPackages = kvPackages;
-            this.fullPackages = fullPackages;
-            this.observer = observer;
-            this.monitor = monitor;
-            this.userInitiated = userInitiated;
-            this.nonIncrementalBackup = nonIncrementalBackup;
-        }
-    }
-
     // Bookkeeping of in-flight operations for timeout etc. purposes.  The operation
     // token is the index of the entry in the pending-operations list.
-    private static final int OP_PENDING = 0;
+    public static final int OP_PENDING = 0;
     private static final int OP_ACKNOWLEDGED = 1;
     private static final int OP_TIMEOUT = -1;
 
     // Waiting for backup agent to respond during backup operation.
-    private static final int OP_TYPE_BACKUP_WAIT = 0;
+    public static final int OP_TYPE_BACKUP_WAIT = 0;
 
     // Waiting for backup agent to respond during restore operation.
-    private static final int OP_TYPE_RESTORE_WAIT = 1;
+    public static final int OP_TYPE_RESTORE_WAIT = 1;
 
     // An entire backup operation spanning multiple packages.
-    private static final int OP_TYPE_BACKUP = 2;
-
-    class Operation {
-        int state;
-        final BackupRestoreTask callback;
-        final int type;
-
-        Operation(int initialState, BackupRestoreTask callbackObj, int type) {
-            state = initialState;
-            callback = callbackObj;
-            this.type = type;
-        }
-    }
+    public static final int OP_TYPE_BACKUP = 2;
 
     /**
      * mCurrentOperations contains the list of currently active operations.
@@ -686,17 +409,17 @@
      * cancel backup tasks.
      */
     @GuardedBy("mCurrentOpLock")
-    private final SparseArray<Operation> mCurrentOperations = new SparseArray<>();
-    private final Object mCurrentOpLock = new Object();
+    public final SparseArray<Operation> mCurrentOperations = new SparseArray<>();
+    public final Object mCurrentOpLock = new Object();
     private final Random mTokenGenerator = new Random();
 
-    private final SparseArray<AdbParams> mAdbBackupRestoreConfirmations = new SparseArray<>();
+    public final SparseArray<AdbParams> mAdbBackupRestoreConfirmations = new SparseArray<>();
 
     // Where we keep our journal files and other bookkeeping
-    private File mBaseStateDir;
-    private File mDataDir;
+    public File mBaseStateDir;
+    public File mDataDir;
     private File mJournalDir;
-    private File mJournal;
+    public File mJournal;
 
     // Backup password, if any, and the file where it's saved.  What is stored is not the
     // password text itself; it's the result of a PBKDF2 hash with a randomly chosen (but
@@ -704,7 +427,7 @@
     // same PBKDF2 cycle with the persisted salt; if the resulting derived key string matches
     // the saved hash string, then the challenge text matches the originally supplied
     // password text.
-    private final SecureRandom mRng = new SecureRandom();
+    public final SecureRandom mRng = new SecureRandom();
     private String mPasswordHash;
     private File mPasswordHashFile;
     private int mPasswordVersion;
@@ -712,10 +435,10 @@
     private byte[] mPasswordSalt;
 
     // Configuration of PBKDF2 that we use for generating pw hashes and intermediate keys
-    private static final int PBKDF2_HASH_ROUNDS = 10000;
+    public static final int PBKDF2_HASH_ROUNDS = 10000;
     private static final int PBKDF2_KEY_SIZE = 256;     // bits
-    private static final int PBKDF2_SALT_SIZE = 512;    // bits
-    private static final String ENCRYPTION_ALGORITHM_NAME = "AES-256";
+    public static final int PBKDF2_SALT_SIZE = 512;    // bits
+    public static final String ENCRYPTION_ALGORITHM_NAME = "AES-256";
 
     // Keep a log of all the apps we've ever backed up, and what the
     // dataset tokens are for both the current backup dataset and
@@ -725,38 +448,22 @@
 
     private static final int CURRENT_ANCESTRAL_RECORD_VERSION = 1;  // increment when the schema changes
     private File mTokenFile;
-    private Set<String> mAncestralPackages = null;
-    private long mAncestralToken = 0;
-    private long mCurrentToken = 0;
+    public Set<String> mAncestralPackages = null;
+    public long mAncestralToken = 0;
+    public long mCurrentToken = 0;
 
     // Persistently track the need to do a full init
     private static final String INIT_SENTINEL_FILE_NAME = "_need_init_";
-    private HashSet<String> mPendingInits = new HashSet<>();  // transport names
+    public HashSet<String> mPendingInits = new HashSet<>();  // transport names
 
     // Round-robin queue for scheduling full backup passes
     private static final int SCHEDULE_FILE_VERSION = 1; // current version of the schedule file
-    class FullBackupEntry implements Comparable<FullBackupEntry> {
-        String packageName;
-        long lastBackup;
-
-        FullBackupEntry(String pkg, long when) {
-            packageName = pkg;
-            lastBackup = when;
-        }
-
-        @Override
-        public int compareTo(FullBackupEntry other) {
-            if (lastBackup < other.lastBackup) return -1;
-            else if (lastBackup > other.lastBackup) return 1;
-            else return 0;
-        }
-    }
 
     private File mFullBackupScheduleFile;
     // If we're running a schedule-driven full backup, this is the task instance doing it
 
     @GuardedBy("mQueueLock")
-    private PerformFullTransportBackupTask mRunningFullBackupTask;
+    public PerformFullTransportBackupTask mRunningFullBackupTask;
 
     @GuardedBy("mQueueLock")
     private ArrayList<FullBackupEntry> mFullBackupQueue;
@@ -794,12 +501,12 @@
     }
 
     // Checks if the app is in a stopped state, that means it won't receive broadcasts.
-    private static boolean appIsStopped(ApplicationInfo app) {
+    public static boolean appIsStopped(ApplicationInfo app) {
         return ((app.flags & ApplicationInfo.FLAG_STOPPED) != 0);
     }
 
     /* does *not* check overall backup eligibility policy! */
-    private static boolean appGetsFullBackup(PackageInfo pkg) {
+    public static boolean appGetsFullBackup(PackageInfo pkg) {
         if (pkg.applicationInfo.backupAgentName != null) {
             // If it has an agent, it gets full backups only if it says so
             return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_FULL_BACKUP_ONLY) != 0;
@@ -812,346 +519,12 @@
     /* adb backup: is this app only capable of doing key/value?  We say otherwise if
      * the app has a backup agent and does not say fullBackupOnly,
      */
-    private static boolean appIsKeyValueOnly(PackageInfo pkg) {
+    public static boolean appIsKeyValueOnly(PackageInfo pkg) {
         return !appGetsFullBackup(pkg);
     }
 
-    // ----- Asynchronous backup/restore handler thread -----
-
-    private class BackupHandler extends Handler {
-        public BackupHandler(Looper looper) {
-            super(looper);
-        }
-
-        public void handleMessage(Message msg) {
-
-            switch (msg.what) {
-            case MSG_RUN_BACKUP:
-            {
-                mLastBackupPass = System.currentTimeMillis();
-
-                IBackupTransport transport = mTransportManager.getCurrentTransportBinder();
-                if (transport == null) {
-                    Slog.v(TAG, "Backup requested but no transport available");
-                    synchronized (mQueueLock) {
-                        mBackupRunning = false;
-                    }
-                    mWakelock.release();
-                    break;
-                }
-
-                // snapshot the pending-backup set and work on that
-                ArrayList<BackupRequest> queue = new ArrayList<>();
-                File oldJournal = mJournal;
-                synchronized (mQueueLock) {
-                    // Do we have any work to do?  Construct the work queue
-                    // then release the synchronization lock to actually run
-                    // the backup.
-                    if (mPendingBackups.size() > 0) {
-                        for (BackupRequest b: mPendingBackups.values()) {
-                            queue.add(b);
-                        }
-                        if (DEBUG) Slog.v(TAG, "clearing pending backups");
-                        mPendingBackups.clear();
-
-                        // Start a new backup-queue journal file too
-                        mJournal = null;
-
-                    }
-                }
-
-                // At this point, we have started a new journal file, and the old
-                // file identity is being passed to the backup processing task.
-                // When it completes successfully, that old journal file will be
-                // deleted.  If we crash prior to that, the old journal is parsed
-                // at next boot and the journaled requests fulfilled.
-                boolean staged = true;
-                if (queue.size() > 0) {
-                    // Spin up a backup state sequence and set it running
-                    try {
-                        String dirName = transport.transportDirName();
-                        PerformBackupTask pbt = new PerformBackupTask(transport, dirName, queue,
-                                oldJournal, null, null, Collections.<String>emptyList(), false,
-                                false /* nonIncremental */);
-                        Message pbtMessage = obtainMessage(MSG_BACKUP_RESTORE_STEP, pbt);
-                        sendMessage(pbtMessage);
-                    } catch (Exception e) {
-                        // unable to ask the transport its dir name -- transient failure, since
-                        // the above check succeeded.  Try again next time.
-                        Slog.e(TAG, "Transport became unavailable attempting backup"
-                                + " or error initializing backup task", e);
-                        staged = false;
-                    }
-                } else {
-                    Slog.v(TAG, "Backup requested but nothing pending");
-                    staged = false;
-                }
-
-                if (!staged) {
-                    // if we didn't actually hand off the wakelock, rewind until next time
-                    synchronized (mQueueLock) {
-                        mBackupRunning = false;
-                    }
-                    mWakelock.release();
-                }
-                break;
-            }
-
-            case MSG_BACKUP_RESTORE_STEP:
-            {
-                try {
-                    BackupRestoreTask task = (BackupRestoreTask) msg.obj;
-                    if (MORE_DEBUG) Slog.v(TAG, "Got next step for " + task + ", executing");
-                    task.execute();
-                } catch (ClassCastException e) {
-                    Slog.e(TAG, "Invalid backup task in flight, obj=" + msg.obj);
-                }
-                break;
-            }
-
-            case MSG_OP_COMPLETE:
-            {
-                try {
-                    Pair<BackupRestoreTask, Long> taskWithResult =
-                            (Pair<BackupRestoreTask, Long>) msg.obj;
-                    taskWithResult.first.operationComplete(taskWithResult.second);
-                } catch (ClassCastException e) {
-                    Slog.e(TAG, "Invalid completion in flight, obj=" + msg.obj);
-                }
-                break;
-            }
-
-            case MSG_RUN_ADB_BACKUP:
-            {
-                // TODO: refactor full backup to be a looper-based state machine
-                // similar to normal backup/restore.
-                AdbBackupParams params = (AdbBackupParams)msg.obj;
-                PerformAdbBackupTask task = new PerformAdbBackupTask(params.fd,
-                        params.observer, params.includeApks, params.includeObbs,
-                        params.includeShared, params.doWidgets, params.curPassword,
-                        params.encryptPassword, params.allApps, params.includeSystem,
-                        params.doCompress, params.includeKeyValue, params.packages, params.latch);
-                (new Thread(task, "adb-backup")).start();
-                break;
-            }
-
-            case MSG_RUN_FULL_TRANSPORT_BACKUP:
-            {
-                PerformFullTransportBackupTask task = (PerformFullTransportBackupTask) msg.obj;
-                (new Thread(task, "transport-backup")).start();
-                break;
-            }
-
-            case MSG_RUN_RESTORE:
-            {
-                RestoreParams params = (RestoreParams)msg.obj;
-                Slog.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer);
-
-                PerformUnifiedRestoreTask task = new PerformUnifiedRestoreTask(params.transport,
-                        params.observer, params.monitor, params.token, params.pkgInfo,
-                        params.pmToken, params.isSystemRestore, params.filterSet);
-
-                synchronized (mPendingRestores) {
-                    if (mIsRestoreInProgress) {
-                        if (DEBUG) {
-                            Slog.d(TAG, "Restore in progress, queueing.");
-                        }
-                        mPendingRestores.add(task);
-                        // This task will be picked up and executed when the the currently running
-                        // restore task finishes.
-                    } else {
-                        if (DEBUG) {
-                            Slog.d(TAG, "Starting restore.");
-                        }
-                        mIsRestoreInProgress = true;
-                        Message restoreMsg = obtainMessage(MSG_BACKUP_RESTORE_STEP, task);
-                        sendMessage(restoreMsg);
-                    }
-                }
-                break;
-            }
-
-            case MSG_RUN_ADB_RESTORE:
-            {
-                // TODO: refactor full restore to be a looper-based state machine
-                // similar to normal backup/restore.
-                AdbRestoreParams params = (AdbRestoreParams)msg.obj;
-                PerformAdbRestoreTask task = new PerformAdbRestoreTask(params.fd,
-                        params.curPassword, params.encryptPassword,
-                        params.observer, params.latch);
-                (new Thread(task, "adb-restore")).start();
-                break;
-            }
-
-            case MSG_RUN_CLEAR:
-            {
-                ClearParams params = (ClearParams)msg.obj;
-                (new PerformClearTask(params.transport, params.packageInfo)).run();
-                break;
-            }
-
-            case MSG_RETRY_CLEAR:
-            {
-                // reenqueues if the transport remains unavailable
-                ClearRetryParams params = (ClearRetryParams)msg.obj;
-                clearBackupData(params.transportName, params.packageName);
-                break;
-            }
-
-            case MSG_RUN_INITIALIZE:
-            {
-                HashSet<String> queue;
-
-                // Snapshot the pending-init queue and work on that
-                synchronized (mQueueLock) {
-                    queue = new HashSet<>(mPendingInits);
-                    mPendingInits.clear();
-                }
-
-                (new PerformInitializeTask(queue)).run();
-                break;
-            }
-
-            case MSG_RETRY_INIT:
-            {
-                synchronized (mQueueLock) {
-                    recordInitPendingLocked(msg.arg1 != 0, (String)msg.obj);
-                    mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
-                            mRunInitIntent);
-                }
-                break;
-            }
-
-            case MSG_RUN_GET_RESTORE_SETS:
-            {
-                // Like other async operations, this is entered with the wakelock held
-                RestoreSet[] sets = null;
-                RestoreGetSetsParams params = (RestoreGetSetsParams)msg.obj;
-                try {
-                    sets = params.transport.getAvailableRestoreSets();
-                    // cache the result in the active session
-                    synchronized (params.session) {
-                        params.session.mRestoreSets = sets;
-                    }
-                    if (sets == null) EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
-                } catch (Exception e) {
-                    Slog.e(TAG, "Error from transport getting set list: " + e.getMessage());
-                } finally {
-                    if (params.observer != null) {
-                        try {
-                            params.observer.restoreSetsAvailable(sets);
-                        } catch (RemoteException re) {
-                            Slog.e(TAG, "Unable to report listing to observer");
-                        } catch (Exception e) {
-                            Slog.e(TAG, "Restore observer threw: " + e.getMessage());
-                        }
-                    }
-
-                    // Done: reset the session timeout clock
-                    removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
-                    sendEmptyMessageDelayed(MSG_RESTORE_SESSION_TIMEOUT, TIMEOUT_RESTORE_INTERVAL);
-
-                    mWakelock.release();
-                }
-                break;
-            }
-
-            case MSG_BACKUP_OPERATION_TIMEOUT:
-            case MSG_RESTORE_OPERATION_TIMEOUT:
-            {
-                Slog.d(TAG, "Timeout message received for token=" + Integer.toHexString(msg.arg1));
-                handleCancel(msg.arg1, false);
-                break;
-            }
-
-            case MSG_RESTORE_SESSION_TIMEOUT:
-            {
-                synchronized (RefactoredBackupManagerService.this) {
-                    if (mActiveRestoreSession != null) {
-                        // Client app left the restore session dangling.  We know that it
-                        // can't be in the middle of an actual restore operation because
-                        // the timeout is suspended while a restore is in progress.  Clean
-                        // up now.
-                        Slog.w(TAG, "Restore session timed out; aborting");
-                        mActiveRestoreSession.markTimedOut();
-                        post(mActiveRestoreSession.new EndRestoreRunnable(
-                                RefactoredBackupManagerService.this, mActiveRestoreSession));
-                    }
-                }
-                break;
-            }
-
-            case MSG_FULL_CONFIRMATION_TIMEOUT:
-            {
-                synchronized (mAdbBackupRestoreConfirmations) {
-                    AdbParams params = mAdbBackupRestoreConfirmations.get(msg.arg1);
-                    if (params != null) {
-                        Slog.i(TAG, "Full backup/restore timed out waiting for user confirmation");
-
-                        // Release the waiter; timeout == completion
-                        signalAdbBackupRestoreCompletion(params);
-
-                        // Remove the token from the set
-                        mAdbBackupRestoreConfirmations.delete(msg.arg1);
-
-                        // Report a timeout to the observer, if any
-                        if (params.observer != null) {
-                            try {
-                                params.observer.onTimeout();
-                            } catch (RemoteException e) {
-                                /* don't care if the app has gone away */
-                            }
-                        }
-                    } else {
-                        Slog.d(TAG, "couldn't find params for token " + msg.arg1);
-                    }
-                }
-                break;
-            }
-
-            case MSG_WIDGET_BROADCAST:
-            {
-                final Intent intent = (Intent) msg.obj;
-                mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM);
-                break;
-            }
-
-            case MSG_REQUEST_BACKUP:
-            {
-                BackupParams params = (BackupParams)msg.obj;
-                if (MORE_DEBUG) {
-                    Slog.d(TAG, "MSG_REQUEST_BACKUP observer=" + params.observer);
-                }
-                ArrayList<BackupRequest> kvQueue = new ArrayList<>();
-                for (String packageName : params.kvPackages) {
-                    kvQueue.add(new BackupRequest(packageName));
-                }
-                mBackupRunning = true;
-                mWakelock.acquire();
-
-                PerformBackupTask pbt = new PerformBackupTask(params.transport, params.dirName,
-                        kvQueue, null, params.observer, params.monitor, params.fullPackages, true,
-                        params.nonIncrementalBackup);
-                Message pbtMessage = obtainMessage(MSG_BACKUP_RESTORE_STEP, pbt);
-                sendMessage(pbtMessage);
-                break;
-            }
-
-            case MSG_SCHEDULE_BACKUP_PACKAGE:
-            {
-                String pkgName = (String)msg.obj;
-                if (MORE_DEBUG) {
-                    Slog.d(TAG, "MSG_SCHEDULE_BACKUP_PACKAGE " + pkgName);
-                }
-                dataChangedImpl(pkgName);
-                break;
-            }
-            }
-        }
-    }
-
     // ----- Debug-only backup operation trace -----
-    private void addBackupTrace(String s) {
+    public void addBackupTrace(String s) {
         if (DEBUG_BACKUP_TRACE) {
             synchronized (mBackupTrace) {
                 mBackupTrace.add(s);
@@ -1159,7 +532,7 @@
         }
     }
 
-    private void clearBackupTrace() {
+    public void clearBackupTrace() {
         if (DEBUG_BACKUP_TRACE) {
             synchronized (mBackupTrace) {
                 mBackupTrace.clear();
@@ -1184,7 +557,7 @@
         // spin up the backup/restore handler thread
         mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND);
         mHandlerThread.start();
-        mBackupHandler = new BackupHandler(mHandlerThread.getLooper());
+        mBackupHandler = new BackupHandler(this, mHandlerThread.getLooper());
 
         // Set up our bookkeeping
         final ContentResolver resolver = context.getContentResolver();
@@ -1193,7 +566,7 @@
         mAutoRestore = Settings.Secure.getInt(resolver,
                 Settings.Secure.BACKUP_AUTO_RESTORE, 1) != 0;
 
-        mProvisionedObserver = new ProvisionedObserver(mBackupHandler);
+        mProvisionedObserver = new ProvisionedObserver(this, mBackupHandler);
         resolver.registerContentObserver(
                 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
                 false, mProvisionedObserver);
@@ -1257,13 +630,13 @@
         }
 
         // Alarm receivers for scheduled backups & initialization operations
-        mRunBackupReceiver = new RunBackupReceiver();
+        mRunBackupReceiver = new RunBackupReceiver(this);
         IntentFilter filter = new IntentFilter();
         filter.addAction(RUN_BACKUP_ACTION);
         context.registerReceiver(mRunBackupReceiver, filter,
                 android.Manifest.permission.BACKUP, null);
 
-        mRunInitReceiver = new RunInitializeReceiver();
+        mRunInitReceiver = new RunInitializeReceiver(this);
         filter = new IntentFilter();
         filter.addAction(RUN_INITIALIZE_ACTION);
         context.registerReceiver(mRunInitReceiver, filter,
@@ -1318,64 +691,6 @@
         mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*backup*");
     }
 
-    private class RunBackupReceiver extends BroadcastReceiver {
-        public void onReceive(Context context, Intent intent) {
-            if (RUN_BACKUP_ACTION.equals(intent.getAction())) {
-                synchronized (mQueueLock) {
-                    if (mPendingInits.size() > 0) {
-                        // If there are pending init operations, we process those
-                        // and then settle into the usual periodic backup schedule.
-                        if (MORE_DEBUG) Slog.v(TAG, "Init pending at scheduled backup");
-                        try {
-                            mAlarmManager.cancel(mRunInitIntent);
-                            mRunInitIntent.send();
-                        } catch (PendingIntent.CanceledException ce) {
-                            Slog.e(TAG, "Run init intent cancelled");
-                            // can't really do more than bail here
-                        }
-                    } else {
-                        // Don't run backups now if we're disabled or not yet
-                        // fully set up.
-                        if (mEnabled && mProvisioned) {
-                            if (!mBackupRunning) {
-                                if (DEBUG) Slog.v(TAG, "Running a backup pass");
-
-                                // Acquire the wakelock and pass it to the backup thread.  it will
-                                // be released once backup concludes.
-                                mBackupRunning = true;
-                                mWakelock.acquire();
-
-                                Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP);
-                                mBackupHandler.sendMessage(msg);
-                            } else {
-                                Slog.i(TAG, "Backup time but one already running");
-                            }
-                        } else {
-                            Slog.w(TAG, "Backup pass but e=" + mEnabled + " p=" + mProvisioned);
-                        }
-                    }
-                }
-            }
-        }
-    }
-
-    private class RunInitializeReceiver extends BroadcastReceiver {
-        public void onReceive(Context context, Intent intent) {
-            if (RUN_INITIALIZE_ACTION.equals(intent.getAction())) {
-                synchronized (mQueueLock) {
-                    if (DEBUG) Slog.v(TAG, "Running a device init");
-
-                    // Acquire the wakelock and pass it to the init thread.  it will
-                    // be released once init concludes.
-                    mWakelock.acquire();
-
-                    Message msg = mBackupHandler.obtainMessage(MSG_RUN_INITIALIZE);
-                    mBackupHandler.sendMessage(msg);
-                }
-            }
-        }
-    }
-
     private void initPackageTracking() {
         if (MORE_DEBUG) Slog.v(TAG, "` tracking");
 
@@ -1646,7 +961,7 @@
         }
     }
 
-    private SecretKey buildPasswordKey(String algorithm, String pw, byte[] salt, int rounds) {
+    public SecretKey buildPasswordKey(String algorithm, String pw, byte[] salt, int rounds) {
         return buildCharArrayKey(algorithm, pw.toCharArray(), salt, rounds);
     }
 
@@ -1671,7 +986,7 @@
         return null;
     }
 
-    private String byteArrayToHex(byte[] data) {
+    public String byteArrayToHex(byte[] data) {
         StringBuilder buf = new StringBuilder(data.length * 2);
         for (int i = 0; i < data.length; i++) {
             buf.append(Byte.toHexString(data[i], true));
@@ -1679,7 +994,7 @@
         return buf.toString();
     }
 
-    private byte[] hexToByteArray(String digits) {
+    public byte[] hexToByteArray(String digits) {
         final int bytes = digits.length() / 2;
         if (2*bytes != digits.length()) {
             throw new IllegalArgumentException("Hex string must have an even number of digits");
@@ -1692,7 +1007,7 @@
         return result;
     }
 
-    private byte[] makeKeyChecksum(String algorithm, byte[] pwBytes, byte[] salt, int rounds) {
+    public byte[] makeKeyChecksum(String algorithm, byte[] pwBytes, byte[] salt, int rounds) {
         char[] mkAsChar = new char[pwBytes.length];
         for (int i = 0; i < pwBytes.length; i++) {
             mkAsChar[i] = (char) pwBytes[i];
@@ -1703,7 +1018,7 @@
     }
 
     // Used for generating random salts or passwords
-    private byte[] randomBytes(int bits) {
+    public byte[] randomBytes(int bits) {
         byte[] array = new byte[bits / 8];
         mRng.nextBytes(array);
         return array;
@@ -1817,7 +1132,7 @@
         return mPasswordHash != null && mPasswordHash.length() > 0;
     }
 
-    private boolean backupPasswordMatches(String currentPw) {
+    public boolean backupPasswordMatches(String currentPw) {
         if (hasBackupPassword()) {
             final boolean pbkdf2Fallback = (mPasswordVersion < BACKUP_PW_FILE_VERSION);
             if (!passwordMatchesSaved(PBKDF_CURRENT, currentPw, PBKDF2_HASH_ROUNDS)
@@ -1832,7 +1147,7 @@
 
     // Maintain persistent state around whether need to do an initialize operation.
     // Must be called with the queue lock held.
-    private void recordInitPendingLocked(boolean isPending, String transportName) {
+    public void recordInitPendingLocked(boolean isPending, String transportName) {
         if (MORE_DEBUG) Slog.i(TAG, "recordInitPendingLocked: " + isPending
                 + " on transport " + transportName);
         mBackupHandler.removeMessages(MSG_RETRY_INIT);
@@ -1884,7 +1199,7 @@
     // Reset all of our bookkeeping, in response to having been told that
     // the backend data has been wiped [due to idle expiry, for example],
     // so we must re-upload all saved settings.
-    private void resetBackupState(File stateFileDir) {
+    public void resetBackupState(File stateFileDir) {
         synchronized (mQueueLock) {
             // Wipe the "what we've ever backed up" tracking
             mEverStoredApps.clear();
@@ -2177,7 +1492,7 @@
     // Called from the backup tasks: record that the given app has been successfully
     // backed up at least once.  This includes both key/value and full-data backups
     // through the transport.
-    private void logBackupComplete(String packageName) {
+    public void logBackupComplete(String packageName) {
         if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) return;
 
         synchronized (mEverStoredApps) {
@@ -2237,7 +1552,7 @@
     // Persistently record the current and ancestral backup tokens as well
     // as the set of packages with data [supposedly] available in the
     // ancestral dataset.
-    private void writeRestoreTokens() {
+    public void writeRestoreTokens() {
         try {
             RandomAccessFile af = new RandomAccessFile(mTokenFile, "rwd");
 
@@ -2323,7 +1638,7 @@
     }
 
     // clear an application's data, blocking until the operation completes or times out
-    private void clearApplicationDataSynchronous(String packageName) {
+    public void clearApplicationDataSynchronous(String packageName) {
         // Don't wipe packages marked allowClearUserData=false
         try {
             PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
@@ -2337,7 +1652,7 @@
             return;
         }
 
-        ClearDataObserver observer = new ClearDataObserver();
+        ClearDataObserver observer = new ClearDataObserver(this);
 
         synchronized(mClearDataLock) {
             mClearingData = true;
@@ -2360,15 +1675,6 @@
         }
     }
 
-    class ClearDataObserver extends IPackageDataObserver.Stub {
-        public void onRemoveCompleted(String packageName, boolean succeeded) {
-            synchronized(mClearDataLock) {
-                mClearingData = false;
-                mClearDataLock.notifyAll();
-            }
-        }
-    }
-
     // Get the restore-set token for the best-available restore set for this package:
     // the active set if possible, else the ancestral one.  Returns zero if none available.
     @Override
@@ -2528,7 +1834,7 @@
         }
     }
 
-    private void removeOperation(int token) {
+    public void removeOperation(int token) {
         if (MORE_DEBUG) {
             Slog.d(TAG, "Removing operation token=" + Integer.toHexString(token));
         }
@@ -2583,7 +1889,7 @@
         return finalState == OP_ACKNOWLEDGED;
     }
 
-    private void handleCancel(int token, boolean cancelAll) {
+    public void handleCancel(int token, boolean cancelAll) {
         // Notify any synchronous waiters
         Operation op = null;
         synchronized (mCurrentOpLock) {
@@ -2624,994 +1930,13 @@
 
     // ----- Back up a set of applications via a worker thread -----
 
-    private enum BackupState {
+    public enum BackupState {
         INITIAL,
         RUNNING_QUEUE,
         FINAL
     }
 
-    /**
-     * This class handles the process of backing up a given list of key/value backup packages.
-     * Also takes in a list of pending dolly backups and kicks them off when key/value backups
-     * are done.
-     *
-     * Flow:
-     * If required, backup @pm@.
-     * For each pending key/value backup package:
-     *     - Bind to agent.
-     *     - Call agent.doBackup()
-     *     - Wait either for cancel/timeout or operationComplete() callback from the agent.
-     * Start task to perform dolly backups.
-     *
-     * There are three entry points into this class:
-     *     - execute() [Called from the handler thread]
-     *     - operationComplete(long result) [Called from the handler thread]
-     *     - handleCancel(boolean cancelAll) [Can be called from any thread]
-     * These methods synchronize on mCancelLock.
-     *
-     * Interaction with mCurrentOperations:
-     *     - An entry for this task is put into mCurrentOperations for the entire lifetime of the
-     *       task. This is useful to cancel the task if required.
-     *     - An ephemeral entry is put into mCurrentOperations each time we are waiting on for
-     *       response from a backup agent. This is used to plumb timeouts and completion callbacks.
-     */
-    class PerformBackupTask implements BackupRestoreTask {
-        private static final String TAG = "PerformBackupTask";
-
-        private final Object mCancelLock = new Object();
-
-        IBackupTransport mTransport;
-        ArrayList<BackupRequest> mQueue;
-        ArrayList<BackupRequest> mOriginalQueue;
-        File mStateDir;
-        File mJournal;
-        BackupState mCurrentState;
-        List<String> mPendingFullBackups;
-        IBackupObserver mObserver;
-        IBackupManagerMonitor mMonitor;
-
-        private final PerformFullTransportBackupTask mFullBackupTask;
-        private final int mCurrentOpToken;
-        private volatile int mEphemeralOpToken;
-
-        // carried information about the current in-flight operation
-        IBackupAgent mAgentBinder;
-        PackageInfo mCurrentPackage;
-        File mSavedStateName;
-        File mBackupDataName;
-        File mNewStateName;
-        ParcelFileDescriptor mSavedState;
-        ParcelFileDescriptor mBackupData;
-        ParcelFileDescriptor mNewState;
-        int mStatus;
-        boolean mFinished;
-        final boolean mUserInitiated;
-        final boolean mNonIncremental;
-
-        private volatile boolean mCancelAll;
-
-        public PerformBackupTask(IBackupTransport transport, String dirName,
-                ArrayList<BackupRequest> queue, File journal, IBackupObserver observer,
-                IBackupManagerMonitor monitor, List<String> pendingFullBackups,
-                boolean userInitiated, boolean nonIncremental) {
-            mTransport = transport;
-            mOriginalQueue = queue;
-            mQueue = new ArrayList<>();
-            mJournal = journal;
-            mObserver = observer;
-            mMonitor = monitor;
-            mPendingFullBackups = pendingFullBackups;
-            mUserInitiated = userInitiated;
-            mNonIncremental = nonIncremental;
-
-            mStateDir = new File(mBaseStateDir, dirName);
-            mCurrentOpToken = generateRandomIntegerToken();
-
-            mFinished = false;
-
-            synchronized (mCurrentOpLock) {
-                if (isBackupOperationInProgress()) {
-                    if (DEBUG) {
-                        Slog.d(TAG, "Skipping backup since one is already in progress.");
-                    }
-                    mCancelAll = true;
-                    mFullBackupTask = null;
-                    mCurrentState = BackupState.FINAL;
-                    addBackupTrace("Skipped. Backup already in progress.");
-                } else {
-                    mCurrentState = BackupState.INITIAL;
-                    CountDownLatch latch = new CountDownLatch(1);
-                    String[] fullBackups =
-                            mPendingFullBackups.toArray(new String[mPendingFullBackups.size()]);
-                    mFullBackupTask =
-                            new PerformFullTransportBackupTask(/*fullBackupRestoreObserver*/ null,
-                                    fullBackups, /*updateSchedule*/ false, /*runningJob*/ null,
-                                    latch,
-                                    mObserver, mMonitor, mUserInitiated);
-
-                    registerTask();
-                    addBackupTrace("STATE => INITIAL");
-                }
-            }
-        }
-
-        /**
-         * Put this task in the repository of running tasks.
-         */
-        private void registerTask() {
-            synchronized (mCurrentOpLock) {
-                mCurrentOperations.put(mCurrentOpToken, new Operation(OP_PENDING, this,
-                        OP_TYPE_BACKUP));
-            }
-        }
-
-        /**
-         * Remove this task from repository of running tasks.
-         */
-        private void unregisterTask() {
-            removeOperation(mCurrentOpToken);
-        }
-
-        // Main entry point: perform one chunk of work, updating the state as appropriate
-        // and reposting the next chunk to the primary backup handler thread.
-        @Override
-        @GuardedBy("mCancelLock")
-        public void execute() {
-            synchronized (mCancelLock) {
-                switch (mCurrentState) {
-                    case INITIAL:
-                        beginBackup();
-                        break;
-
-                    case RUNNING_QUEUE:
-                        invokeNextAgent();
-                        break;
-
-                    case FINAL:
-                        if (!mFinished) finalizeBackup();
-                        else {
-                            Slog.e(TAG, "Duplicate finish");
-                        }
-                        mFinished = true;
-                        break;
-                }
-            }
-        }
-
-        // We're starting a backup pass.  Initialize the transport and send
-        // the PM metadata blob if we haven't already.
-        void beginBackup() {
-            if (DEBUG_BACKUP_TRACE) {
-                clearBackupTrace();
-                StringBuilder b = new StringBuilder(256);
-                b.append("beginBackup: [");
-                for (BackupRequest req : mOriginalQueue) {
-                    b.append(' ');
-                    b.append(req.packageName);
-                }
-                b.append(" ]");
-                addBackupTrace(b.toString());
-            }
-
-            mAgentBinder = null;
-            mStatus = BackupTransport.TRANSPORT_OK;
-
-            // Sanity check: if the queue is empty we have no work to do.
-            if (mOriginalQueue.isEmpty() && mPendingFullBackups.isEmpty()) {
-                Slog.w(TAG, "Backup begun with an empty queue - nothing to do.");
-                addBackupTrace("queue empty at begin");
-                sendBackupFinished(mObserver, BackupManager.SUCCESS);
-                executeNextState(BackupState.FINAL);
-                return;
-            }
-
-            // We need to retain the original queue contents in case of transport
-            // failure, but we want a working copy that we can manipulate along
-            // the way.
-            mQueue = (ArrayList<BackupRequest>) mOriginalQueue.clone();
-
-            // When the transport is forcing non-incremental key/value payloads, we send the
-            // metadata only if it explicitly asks for it.
-            boolean skipPm = mNonIncremental;
-
-            // The app metadata pseudopackage might also be represented in the
-            // backup queue if apps have been added/removed since the last time
-            // we performed a backup.  Drop it from the working queue now that
-            // we're committed to evaluating it for backup regardless.
-            for (int i = 0; i < mQueue.size(); i++) {
-                if (PACKAGE_MANAGER_SENTINEL.equals(mQueue.get(i).packageName)) {
-                    if (MORE_DEBUG) {
-                        Slog.i(TAG, "Metadata in queue; eliding");
-                    }
-                    mQueue.remove(i);
-                    skipPm = false;
-                    break;
-                }
-            }
-
-            if (DEBUG) Slog.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
-
-            File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL);
-            try {
-                final String transportName = mTransport.transportDirName();
-                EventLog.writeEvent(EventLogTags.BACKUP_START, transportName);
-
-                // If we haven't stored package manager metadata yet, we must init the transport.
-                if (mStatus == BackupTransport.TRANSPORT_OK && pmState.length() <= 0) {
-                    Slog.i(TAG, "Initializing (wiping) backup state and transport storage");
-                    addBackupTrace("initializing transport " + transportName);
-                    resetBackupState(mStateDir);  // Just to make sure.
-                    mStatus = mTransport.initializeDevice();
-
-                    addBackupTrace("transport.initializeDevice() == " + mStatus);
-                    if (mStatus == BackupTransport.TRANSPORT_OK) {
-                        EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE);
-                    } else {
-                        EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)");
-                        Slog.e(TAG, "Transport error in initializeDevice()");
-                    }
-                }
-
-                if (skipPm) {
-                    Slog.d(TAG, "Skipping backup of package metadata.");
-                    executeNextState(BackupState.RUNNING_QUEUE);
-                } else {
-                    // The package manager doesn't have a proper <application> etc, but since
-                    // it's running here in the system process we can just set up its agent
-                    // directly and use a synthetic BackupRequest.  We always run this pass
-                    // because it's cheap and this way we guarantee that we don't get out of
-                    // step even if we're selecting among various transports at run time.
-                    if (mStatus == BackupTransport.TRANSPORT_OK) {
-                        PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
-                                mPackageManager);
-                        mStatus = invokeAgentForBackup(PACKAGE_MANAGER_SENTINEL,
-                                IBackupAgent.Stub.asInterface(pmAgent.onBind()), mTransport);
-                        addBackupTrace("PMBA invoke: " + mStatus);
-
-                        // Because the PMBA is a local instance, it has already executed its
-                        // backup callback and returned.  Blow away the lingering (spurious)
-                        // pending timeout message for it.
-                        mBackupHandler.removeMessages(MSG_BACKUP_OPERATION_TIMEOUT);
-                    }
-                }
-
-                if (mStatus == BackupTransport.TRANSPORT_NOT_INITIALIZED) {
-                    // The backend reports that our dataset has been wiped.  Note this in
-                    // the event log; the no-success code below will reset the backup
-                    // state as well.
-                    EventLog.writeEvent(EventLogTags.BACKUP_RESET, mTransport.transportDirName());
-                }
-            } catch (Exception e) {
-                Slog.e(TAG, "Error in backup thread", e);
-                addBackupTrace("Exception in backup thread: " + e);
-                mStatus = BackupTransport.TRANSPORT_ERROR;
-            } finally {
-                // If we've succeeded so far, invokeAgentForBackup() will have run the PM
-                // metadata and its completion/timeout callback will continue the state
-                // machine chain.  If it failed that won't happen; we handle that now.
-                addBackupTrace("exiting prelim: " + mStatus);
-                if (mStatus != BackupTransport.TRANSPORT_OK) {
-                    // if things went wrong at this point, we need to
-                    // restage everything and try again later.
-                    resetBackupState(mStateDir);  // Just to make sure.
-                    // In case of any other error, it's backup transport error.
-                    sendBackupFinished(mObserver, BackupManager.ERROR_TRANSPORT_ABORTED);
-                    executeNextState(BackupState.FINAL);
-                }
-            }
-        }
-
-        // Transport has been initialized and the PM metadata submitted successfully
-        // if that was warranted.  Now we process the single next thing in the queue.
-        void invokeNextAgent() {
-            mStatus = BackupTransport.TRANSPORT_OK;
-            addBackupTrace("invoke q=" + mQueue.size());
-
-            // Sanity check that we have work to do.  If not, skip to the end where
-            // we reestablish the wakelock invariants etc.
-            if (mQueue.isEmpty()) {
-                if (MORE_DEBUG) Slog.i(TAG, "queue now empty");
-                executeNextState(BackupState.FINAL);
-                return;
-            }
-
-            // pop the entry we're going to process on this step
-            BackupRequest request = mQueue.get(0);
-            mQueue.remove(0);
-
-            Slog.d(TAG, "starting key/value backup of " + request);
-            addBackupTrace("launch agent for " + request.packageName);
-
-            // Verify that the requested app exists; it might be something that
-            // requested a backup but was then uninstalled.  The request was
-            // journalled and rather than tamper with the journal it's safer
-            // to sanity-check here.  This also gives us the classname of the
-            // package's backup agent.
-            try {
-                mCurrentPackage = mPackageManager.getPackageInfo(request.packageName,
-                        PackageManager.GET_SIGNATURES);
-                if (!appIsEligibleForBackup(mCurrentPackage.applicationInfo)) {
-                    // The manifest has changed but we had a stale backup request pending.
-                    // This won't happen again because the app won't be requesting further
-                    // backups.
-                    Slog.i(TAG, "Package " + request.packageName
-                            + " no longer supports backup; skipping");
-                    addBackupTrace("skipping - not eligible, completion is noop");
-                    // Shouldn't happen in case of requested backup, as pre-check was done in
-                    // #requestBackup(), except to app update done concurrently
-                    sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
-                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
-                    executeNextState(BackupState.RUNNING_QUEUE);
-                    return;
-                }
-
-                if (appGetsFullBackup(mCurrentPackage)) {
-                    // It's possible that this app *formerly* was enqueued for key/value backup,
-                    // but has since been updated and now only supports the full-data path.
-                    // Don't proceed with a key/value backup for it in this case.
-                    Slog.i(TAG, "Package " + request.packageName
-                            + " requests full-data rather than key/value; skipping");
-                    addBackupTrace("skipping - fullBackupOnly, completion is noop");
-                    // Shouldn't happen in case of requested backup, as pre-check was done in
-                    // #requestBackup()
-                    sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
-                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
-                    executeNextState(BackupState.RUNNING_QUEUE);
-                    return;
-                }
-
-                if (appIsStopped(mCurrentPackage.applicationInfo)) {
-                    // The app has been force-stopped or cleared or just installed,
-                    // and not yet launched out of that state, so just as it won't
-                    // receive broadcasts, we won't run it for backup.
-                    addBackupTrace("skipping - stopped");
-                    sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
-                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
-                    executeNextState(BackupState.RUNNING_QUEUE);
-                    return;
-                }
-
-                IBackupAgent agent = null;
-                try {
-                    mWakelock.setWorkSource(new WorkSource(mCurrentPackage.applicationInfo.uid));
-                    agent = bindToAgentSynchronous(mCurrentPackage.applicationInfo,
-                            ApplicationThreadConstants.BACKUP_MODE_INCREMENTAL);
-                    addBackupTrace("agent bound; a? = " + (agent != null));
-                    if (agent != null) {
-                        mAgentBinder = agent;
-                        mStatus = invokeAgentForBackup(request.packageName, agent, mTransport);
-                        // at this point we'll either get a completion callback from the
-                        // agent, or a timeout message on the main handler.  either way, we're
-                        // done here as long as we're successful so far.
-                    } else {
-                        // Timeout waiting for the agent
-                        mStatus = BackupTransport.AGENT_ERROR;
-                    }
-                } catch (SecurityException ex) {
-                    // Try for the next one.
-                    Slog.d(TAG, "error in bind/backup", ex);
-                    mStatus = BackupTransport.AGENT_ERROR;
-                            addBackupTrace("agent SE");
-                }
-            } catch (NameNotFoundException e) {
-                Slog.d(TAG, "Package does not exist; skipping");
-                addBackupTrace("no such package");
-                mStatus = BackupTransport.AGENT_UNKNOWN;
-            } finally {
-                mWakelock.setWorkSource(null);
-
-                // If there was an agent error, no timeout/completion handling will occur.
-                // That means we need to direct to the next state ourselves.
-                if (mStatus != BackupTransport.TRANSPORT_OK) {
-                    BackupState nextState = BackupState.RUNNING_QUEUE;
-                    mAgentBinder = null;
-
-                    // An agent-level failure means we reenqueue this one agent for
-                    // a later retry, but otherwise proceed normally.
-                    if (mStatus == BackupTransport.AGENT_ERROR) {
-                        if (MORE_DEBUG) Slog.i(TAG, "Agent failure for " + request.packageName
-                                + " - restaging");
-                        dataChangedImpl(request.packageName);
-                        mStatus = BackupTransport.TRANSPORT_OK;
-                        if (mQueue.isEmpty()) nextState = BackupState.FINAL;
-                        sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
-                                BackupManager.ERROR_AGENT_FAILURE);
-                    } else if (mStatus == BackupTransport.AGENT_UNKNOWN) {
-                        // Failed lookup of the app, so we couldn't bring up an agent, but
-                        // we're otherwise fine.  Just drop it and go on to the next as usual.
-                        mStatus = BackupTransport.TRANSPORT_OK;
-                        sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
-                                BackupManager.ERROR_PACKAGE_NOT_FOUND);
-                    } else {
-                        // Transport-level failure means we reenqueue everything
-                        revertAndEndBackup();
-                        nextState = BackupState.FINAL;
-                    }
-
-                    executeNextState(nextState);
-                } else {
-                    // success case
-                    addBackupTrace("expecting completion/timeout callback");
-                }
-            }
-        }
-
-        void finalizeBackup() {
-            addBackupTrace("finishing");
-
-            // Mark packages that we didn't backup (because backup was cancelled, etc.) as needing
-            // backup.
-            for (BackupRequest req : mQueue) {
-                dataChangedImpl(req.packageName);
-            }
-
-            // Either backup was successful, in which case we of course do not need
-            // this pass's journal any more; or it failed, in which case we just
-            // re-enqueued all of these packages in the current active journal.
-            // Either way, we no longer need this pass's journal.
-            if (mJournal != null && !mJournal.delete()) {
-                Slog.e(TAG, "Unable to remove backup journal file " + mJournal);
-            }
-
-            // If everything actually went through and this is the first time we've
-            // done a backup, we can now record what the current backup dataset token
-            // is.
-            if ((mCurrentToken == 0) && (mStatus == BackupTransport.TRANSPORT_OK)) {
-                addBackupTrace("success; recording token");
-                try {
-                    mCurrentToken = mTransport.getCurrentRestoreSet();
-                    writeRestoreTokens();
-                } catch (Exception e) {
-                    // nothing for it at this point, unfortunately, but this will be
-                    // recorded the next time we fully succeed.
-                    Slog.e(TAG, "Transport threw reporting restore set: " + e.getMessage());
-                    addBackupTrace("transport threw returning token");
-                }
-            }
-
-            // Set up the next backup pass - at this point we can set mBackupRunning
-            // to false to allow another pass to fire, because we're done with the
-            // state machine sequence and the wakelock is refcounted.
-            synchronized (mQueueLock) {
-                mBackupRunning = false;
-                if (mStatus == BackupTransport.TRANSPORT_NOT_INITIALIZED) {
-                    // Make sure we back up everything and perform the one-time init
-                    if (MORE_DEBUG) Slog.d(TAG, "Server requires init; rerunning");
-                    addBackupTrace("init required; rerunning");
-                    try {
-                        final String name = mTransportManager.getTransportName(mTransport);
-                        if (name != null) {
-                            mPendingInits.add(name);
-                        } else {
-                            if (DEBUG) {
-                                Slog.w(TAG, "Couldn't find name of transport " + mTransport
-                                        + " for init");
-                            }
-                        }
-                    } catch (Exception e) {
-                        Slog.w(TAG, "Failed to query transport name for init: " + e.getMessage());
-                        // swallow it and proceed; we don't rely on this
-                    }
-                    clearMetadata();
-                    backupNow();
-                }
-            }
-
-            clearBackupTrace();
-
-            unregisterTask();
-
-            if (!mCancelAll && mStatus == BackupTransport.TRANSPORT_OK &&
-                    mPendingFullBackups != null && !mPendingFullBackups.isEmpty()) {
-                Slog.d(TAG, "Starting full backups for: " + mPendingFullBackups);
-                // Acquiring wakelock for PerformFullTransportBackupTask before its start.
-                mWakelock.acquire();
-                (new Thread(mFullBackupTask, "full-transport-requested")).start();
-            } else if (mCancelAll) {
-                if (mFullBackupTask != null) {
-                    mFullBackupTask.unregisterTask();
-                }
-                sendBackupFinished(mObserver, BackupManager.ERROR_BACKUP_CANCELLED);
-            } else {
-                mFullBackupTask.unregisterTask();
-                switch (mStatus) {
-                    case BackupTransport.TRANSPORT_OK:
-                        sendBackupFinished(mObserver, BackupManager.SUCCESS);
-                        break;
-                    case BackupTransport.TRANSPORT_NOT_INITIALIZED:
-                        sendBackupFinished(mObserver, BackupManager.ERROR_TRANSPORT_ABORTED);
-                        break;
-                    case BackupTransport.TRANSPORT_ERROR:
-                    default:
-                        sendBackupFinished(mObserver, BackupManager.ERROR_TRANSPORT_ABORTED);
-                        break;
-                }
-            }
-            Slog.i(RefactoredBackupManagerService.TAG, "K/V backup pass finished.");
-            // Only once we're entirely finished do we release the wakelock for k/v backup.
-            mWakelock.release();
-        }
-
-        // Remove the PM metadata state. This will generate an init on the next pass.
-        void clearMetadata() {
-            final File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL);
-            if (pmState.exists()) pmState.delete();
-        }
-
-        // Invoke an agent's doBackup() and start a timeout message spinning on the main
-        // handler in case it doesn't get back to us.
-        int invokeAgentForBackup(String packageName, IBackupAgent agent,
-                IBackupTransport transport) {
-            if (DEBUG) Slog.d(TAG, "invokeAgentForBackup on " + packageName);
-            addBackupTrace("invoking " + packageName);
-
-            File blankStateName = new File(mStateDir, "blank_state");
-            mSavedStateName = new File(mStateDir, packageName);
-            mBackupDataName = new File(mDataDir, packageName + ".data");
-            mNewStateName = new File(mStateDir, packageName + ".new");
-            if (MORE_DEBUG) Slog.d(TAG, "data file: " + mBackupDataName);
-
-            mSavedState = null;
-            mBackupData = null;
-            mNewState = null;
-
-            boolean callingAgent = false;
-            mEphemeralOpToken = generateRandomIntegerToken();
-            try {
-                // Look up the package info & signatures.  This is first so that if it
-                // throws an exception, there's no file setup yet that would need to
-                // be unraveled.
-                if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
-                    // The metadata 'package' is synthetic; construct one and make
-                    // sure our global state is pointed at it
-                    mCurrentPackage = new PackageInfo();
-                    mCurrentPackage.packageName = packageName;
-                }
-
-                // In a full backup, we pass a null ParcelFileDescriptor as
-                // the saved-state "file". For key/value backups we pass the old state if
-                // an incremental backup is required, and a blank state otherwise.
-                mSavedState = ParcelFileDescriptor.open(
-                        mNonIncremental ? blankStateName : mSavedStateName,
-                        ParcelFileDescriptor.MODE_READ_ONLY |
-                        ParcelFileDescriptor.MODE_CREATE);  // Make an empty file if necessary
-
-                mBackupData = ParcelFileDescriptor.open(mBackupDataName,
-                        ParcelFileDescriptor.MODE_READ_WRITE |
-                        ParcelFileDescriptor.MODE_CREATE |
-                        ParcelFileDescriptor.MODE_TRUNCATE);
-
-                if (!SELinux.restorecon(mBackupDataName)) {
-                    Slog.e(TAG, "SELinux restorecon failed on " + mBackupDataName);
-                }
-
-                mNewState = ParcelFileDescriptor.open(mNewStateName,
-                        ParcelFileDescriptor.MODE_READ_WRITE |
-                        ParcelFileDescriptor.MODE_CREATE |
-                        ParcelFileDescriptor.MODE_TRUNCATE);
-
-                final long quota = mTransport.getBackupQuota(packageName, false /* isFullBackup */);
-                callingAgent = true;
-
-                // Initiate the target's backup pass
-                addBackupTrace("setting timeout");
-                prepareOperationTimeout(mEphemeralOpToken, TIMEOUT_BACKUP_INTERVAL, this,
-                        OP_TYPE_BACKUP_WAIT);
-                addBackupTrace("calling agent doBackup()");
-
-                agent.doBackup(mSavedState, mBackupData, mNewState, quota, mEphemeralOpToken,
-                        mBackupManagerBinder);
-            } catch (Exception e) {
-                Slog.e(TAG, "Error invoking for backup on " + packageName + ". " + e);
-                addBackupTrace("exception: " + e);
-                EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName,
-                        e.toString());
-                errorCleanup();
-                return callingAgent ? BackupTransport.AGENT_ERROR
-                        : BackupTransport.TRANSPORT_ERROR;
-            } finally {
-                if (mNonIncremental) {
-                    blankStateName.delete();
-                }
-            }
-
-            // At this point the agent is off and running.  The next thing to happen will
-            // either be a callback from the agent, at which point we'll process its data
-            // for transport, or a timeout.  Either way the next phase will happen in
-            // response to the TimeoutHandler interface callbacks.
-            addBackupTrace("invoke success");
-            return BackupTransport.TRANSPORT_OK;
-        }
-
-        public void failAgent(IBackupAgent agent, String message) {
-            try {
-                agent.fail(message);
-            } catch (Exception e) {
-                Slog.w(TAG, "Error conveying failure to " + mCurrentPackage.packageName);
-            }
-        }
-
-        // SHA-1 a byte array and return the result in hex
-        private String SHA1Checksum(byte[] input) {
-            final byte[] checksum;
-            try {
-                MessageDigest md = MessageDigest.getInstance("SHA-1");
-                checksum = md.digest(input);
-            } catch (NoSuchAlgorithmException e) {
-                Slog.e(TAG, "Unable to use SHA-1!");
-                return "00";
-            }
-
-            StringBuffer sb = new StringBuffer(checksum.length * 2);
-            for (int i = 0; i < checksum.length; i++) {
-                sb.append(Integer.toHexString(checksum[i]));
-            }
-            return sb.toString();
-        }
-
-        private void writeWidgetPayloadIfAppropriate(FileDescriptor fd, String pkgName)
-                throws IOException {
-            // TODO: http://b/22388012
-            byte[] widgetState = AppWidgetBackupBridge.getWidgetState(pkgName,
-                    UserHandle.USER_SYSTEM);
-            // has the widget state changed since last time?
-            final File widgetFile = new File(mStateDir, pkgName + "_widget");
-            final boolean priorStateExists = widgetFile.exists();
-
-            if (MORE_DEBUG) {
-                if (priorStateExists || widgetState != null) {
-                    Slog.i(TAG, "Checking widget update: state=" + (widgetState != null)
-                            + " prior=" + priorStateExists);
-                }
-            }
-
-            if (!priorStateExists && widgetState == null) {
-                // no prior state, no new state => nothing to do
-                return;
-            }
-
-            // if the new state is not null, we might need to compare checksums to
-            // determine whether to update the widget blob in the archive.  If the
-            // widget state *is* null, we know a priori at this point that we simply
-            // need to commit a deletion for it.
-            String newChecksum = null;
-            if (widgetState != null) {
-                newChecksum = SHA1Checksum(widgetState);
-                if (priorStateExists) {
-                    final String priorChecksum;
-                    try (
-                        FileInputStream fin = new FileInputStream(widgetFile);
-                        DataInputStream in = new DataInputStream(fin)
-                    ) {
-                        priorChecksum = in.readUTF();
-                    }
-                    if (Objects.equals(newChecksum, priorChecksum)) {
-                        // Same checksum => no state change => don't rewrite the widget data
-                        return;
-                    }
-                }
-            } // else widget state *became* empty, so we need to commit a deletion
-
-            BackupDataOutput out = new BackupDataOutput(fd);
-            if (widgetState != null) {
-                try (
-                    FileOutputStream fout = new FileOutputStream(widgetFile);
-                    DataOutputStream stateOut = new DataOutputStream(fout)
-                ) {
-                    stateOut.writeUTF(newChecksum);
-                }
-
-                out.writeEntityHeader(KEY_WIDGET_STATE, widgetState.length);
-                out.writeEntityData(widgetState, widgetState.length);
-            } else {
-                // Widget state for this app has been removed; commit a deletion
-                out.writeEntityHeader(KEY_WIDGET_STATE, -1);
-                widgetFile.delete();
-            }
-        }
-
-        @Override
-        @GuardedBy("mCancelLock")
-        public void operationComplete(long unusedResult) {
-            removeOperation(mEphemeralOpToken);
-            synchronized (mCancelLock) {
-                // The agent reported back to us!
-                if (mFinished) {
-                    Slog.d(TAG, "operationComplete received after task finished.");
-                    return;
-                }
-
-                if (mBackupData == null) {
-                    // This callback was racing with our timeout, so we've cleaned up the
-                    // agent state already and are on to the next thing.  We have nothing
-                    // further to do here: agent state having been cleared means that we've
-                    // initiated the appropriate next operation.
-                    final String pkg = (mCurrentPackage != null)
-                            ? mCurrentPackage.packageName : "[none]";
-                    if (MORE_DEBUG) {
-                        Slog.i(TAG, "Callback after agent teardown: " + pkg);
-                    }
-                    addBackupTrace("late opComplete; curPkg = " + pkg);
-                    return;
-                }
-
-                final String pkgName = mCurrentPackage.packageName;
-                final long filepos = mBackupDataName.length();
-                FileDescriptor fd = mBackupData.getFileDescriptor();
-                try {
-                    // If it's a 3rd party app, see whether they wrote any protected keys
-                    // and complain mightily if they are attempting shenanigans.
-                    if (mCurrentPackage.applicationInfo != null &&
-                            (mCurrentPackage.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
-                                    == 0) {
-                        ParcelFileDescriptor readFd = ParcelFileDescriptor.open(mBackupDataName,
-                                ParcelFileDescriptor.MODE_READ_ONLY);
-                        BackupDataInput in = new BackupDataInput(readFd.getFileDescriptor());
-                        try {
-                            while (in.readNextHeader()) {
-                                final String key = in.getKey();
-                                if (key != null && key.charAt(0) >= 0xff00) {
-                                    // Not okay: crash them and bail.
-                                    failAgent(mAgentBinder, "Illegal backup key: " + key);
-                                    addBackupTrace("illegal key " + key + " from " + pkgName);
-                                    EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, pkgName,
-                                            "bad key");
-                                    mMonitor = monitorEvent(mMonitor,
-                                            BackupManagerMonitor.LOG_EVENT_ID_ILLEGAL_KEY,
-                                            mCurrentPackage,
-                                            BackupManagerMonitor
-                                                    .LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                            putMonitoringExtra(null,
-                                                    BackupManagerMonitor.EXTRA_LOG_ILLEGAL_KEY,
-                                                    key));
-                                    mBackupHandler.removeMessages(MSG_BACKUP_OPERATION_TIMEOUT);
-                                    sendBackupOnPackageResult(mObserver, pkgName,
-                                            BackupManager.ERROR_AGENT_FAILURE);
-                                    errorCleanup();
-                                    // agentErrorCleanup() implicitly executes next state properly
-                                    return;
-                                }
-                                in.skipEntityData();
-                            }
-                        } finally {
-                            if (readFd != null) {
-                                readFd.close();
-                            }
-                        }
-                    }
-
-                    // Piggyback the widget state payload, if any
-                    writeWidgetPayloadIfAppropriate(fd, pkgName);
-                } catch (IOException e) {
-                    // Hard disk error; recovery/failure policy TBD.  For now roll back,
-                    // but we may want to consider this a transport-level failure (i.e.
-                    // we're in such a bad state that we can't contemplate doing backup
-                    // operations any more during this pass).
-                    Slog.w(TAG, "Unable to save widget state for " + pkgName);
-                    try {
-                        Os.ftruncate(fd, filepos);
-                    } catch (ErrnoException ee) {
-                        Slog.w(TAG, "Unable to roll back!");
-                    }
-                }
-
-                // Spin the data off to the transport and proceed with the next stage.
-                if (MORE_DEBUG) Slog.v(TAG, "operationComplete(): sending data to transport for "
-                        + pkgName);
-                mBackupHandler.removeMessages(MSG_BACKUP_OPERATION_TIMEOUT);
-                clearAgentState();
-                addBackupTrace("operation complete");
-
-                ParcelFileDescriptor backupData = null;
-                mStatus = BackupTransport.TRANSPORT_OK;
-                long size = 0;
-                try {
-                    size = mBackupDataName.length();
-                    if (size > 0) {
-                        if (mStatus == BackupTransport.TRANSPORT_OK) {
-                            backupData = ParcelFileDescriptor.open(mBackupDataName,
-                                    ParcelFileDescriptor.MODE_READ_ONLY);
-                            addBackupTrace("sending data to transport");
-                            int flags = mUserInitiated ? BackupTransport.FLAG_USER_INITIATED : 0;
-                            mStatus = mTransport.performBackup(mCurrentPackage, backupData, flags);
-                        }
-
-                        // TODO - We call finishBackup() for each application backed up, because
-                        // we need to know now whether it succeeded or failed.  Instead, we should
-                        // hold off on finishBackup() until the end, which implies holding off on
-                        // renaming *all* the output state files (see below) until that happens.
-
-                        addBackupTrace("data delivered: " + mStatus);
-                        if (mStatus == BackupTransport.TRANSPORT_OK) {
-                            addBackupTrace("finishing op on transport");
-                            mStatus = mTransport.finishBackup();
-                            addBackupTrace("finished: " + mStatus);
-                        } else if (mStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
-                            addBackupTrace("transport rejected package");
-                        }
-                    } else {
-                        if (MORE_DEBUG) Slog.i(TAG,
-                                "no backup data written; not calling transport");
-                        addBackupTrace("no data to send");
-                        mMonitor = monitorEvent(mMonitor,
-                                BackupManagerMonitor.LOG_EVENT_ID_NO_DATA_TO_SEND,
-                                mCurrentPackage,
-                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                null);
-                    }
-
-                    if (mStatus == BackupTransport.TRANSPORT_OK) {
-                        // After successful transport, delete the now-stale data
-                        // and juggle the files so that next time we supply the agent
-                        // with the new state file it just created.
-                        mBackupDataName.delete();
-                        mNewStateName.renameTo(mSavedStateName);
-                        sendBackupOnPackageResult(mObserver, pkgName, BackupManager.SUCCESS);
-                        EventLog.writeEvent(EventLogTags.BACKUP_PACKAGE, pkgName, size);
-                        logBackupComplete(pkgName);
-                    } else if (mStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
-                        // The transport has rejected backup of this specific package.  Roll it
-                        // back but proceed with running the rest of the queue.
-                        mBackupDataName.delete();
-                        mNewStateName.delete();
-                        sendBackupOnPackageResult(mObserver, pkgName,
-                                BackupManager.ERROR_TRANSPORT_PACKAGE_REJECTED);
-                        EventLogTags.writeBackupAgentFailure(pkgName, "Transport rejected");
-                    } else if (mStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
-                        sendBackupOnPackageResult(mObserver, pkgName,
-                                BackupManager.ERROR_TRANSPORT_QUOTA_EXCEEDED);
-                        EventLog.writeEvent(EventLogTags.BACKUP_QUOTA_EXCEEDED, pkgName);
-                    } else {
-                        // Actual transport-level failure to communicate the data to the backend
-                        sendBackupOnPackageResult(mObserver, pkgName,
-                                BackupManager.ERROR_TRANSPORT_ABORTED);
-                        EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, pkgName);
-                    }
-                } catch (Exception e) {
-                    sendBackupOnPackageResult(mObserver, pkgName,
-                            BackupManager.ERROR_TRANSPORT_ABORTED);
-                    Slog.e(TAG, "Transport error backing up " + pkgName, e);
-                    EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, pkgName);
-                    mStatus = BackupTransport.TRANSPORT_ERROR;
-                } finally {
-                    try {
-                        if (backupData != null) backupData.close();
-                    } catch (IOException e) {
-                    }
-                }
-
-                final BackupState nextState;
-                if (mStatus == BackupTransport.TRANSPORT_OK
-                        || mStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
-                    // Success or single-package rejection.  Proceed with the next app if any,
-                    // otherwise we're done.
-                    nextState = (mQueue.isEmpty()) ? BackupState.FINAL : BackupState.RUNNING_QUEUE;
-                } else if (mStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
-                    if (MORE_DEBUG) {
-                        Slog.d(TAG, "Package " + mCurrentPackage.packageName +
-                                " hit quota limit on k/v backup");
-                    }
-                    if (mAgentBinder != null) {
-                        try {
-                            long quota = mTransport.getBackupQuota(mCurrentPackage.packageName,
-                                    false);
-                            mAgentBinder.doQuotaExceeded(size, quota);
-                        } catch (Exception e) {
-                            Slog.e(TAG, "Unable to notify about quota exceeded: " + e.getMessage());
-                        }
-                    }
-                    nextState = (mQueue.isEmpty()) ? BackupState.FINAL : BackupState.RUNNING_QUEUE;
-                } else {
-                    // Any other error here indicates a transport-level failure.  That means
-                    // we need to halt everything and reschedule everything for next time.
-                    revertAndEndBackup();
-                    nextState = BackupState.FINAL;
-                }
-
-                executeNextState(nextState);
-            }
-        }
-
-
-        @Override
-        @GuardedBy("mCancelLock")
-        public void handleCancel(boolean cancelAll) {
-            removeOperation(mEphemeralOpToken);
-            synchronized (mCancelLock) {
-                if (mFinished) {
-                    // We have already cancelled this operation.
-                    if (MORE_DEBUG) {
-                        Slog.d(TAG, "Ignoring stale cancel. cancelAll=" + cancelAll);
-                    }
-                    return;
-                }
-                mCancelAll = cancelAll;
-                final String logPackageName = (mCurrentPackage != null)
-                        ? mCurrentPackage.packageName
-                        : "no_package_yet";
-                Slog.i(TAG, "Cancel backing up " + logPackageName);
-                EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, logPackageName);
-                addBackupTrace("cancel of " + logPackageName + ", cancelAll=" + cancelAll);
-                mMonitor = monitorEvent(mMonitor,
-                        BackupManagerMonitor.LOG_EVENT_ID_KEY_VALUE_BACKUP_CANCEL,
-                        mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT,
-                        putMonitoringExtra(null, BackupManagerMonitor.EXTRA_LOG_CANCEL_ALL,
-                                mCancelAll));
-                errorCleanup();
-                if (!cancelAll) {
-                    // The current agent either timed out or was cancelled running doBackup().
-                    // Restage it for the next time we run a backup pass.
-                    // !!! TODO: keep track of failure counts per agent, and blacklist those which
-                    // fail repeatedly (i.e. have proved themselves to be buggy).
-                    executeNextState(
-                            mQueue.isEmpty() ? BackupState.FINAL : BackupState.RUNNING_QUEUE);
-                    dataChangedImpl(mCurrentPackage.packageName);
-                } else {
-                    finalizeBackup();
-                }
-            }
-        }
-
-        void revertAndEndBackup() {
-            if (MORE_DEBUG) Slog.i(TAG, "Reverting backup queue - restaging everything");
-            addBackupTrace("transport error; reverting");
-
-            // We want to reset the backup schedule based on whatever the transport suggests
-            // by way of retry/backoff time.
-            long delay;
-            try {
-                delay = mTransport.requestBackupTime();
-            } catch (Exception e) {
-                Slog.w(TAG, "Unable to contact transport for recommended backoff: " + e.getMessage());
-                delay = 0;  // use the scheduler's default
-            }
-            KeyValueBackupJob.schedule(mContext, delay);
-
-            for (BackupRequest request : mOriginalQueue) {
-                dataChangedImpl(request.packageName);
-            }
-
-        }
-
-        void errorCleanup() {
-            mBackupDataName.delete();
-            mNewStateName.delete();
-            clearAgentState();
-        }
-
-        // Cleanup common to both success and failure cases
-        void clearAgentState() {
-            try { if (mSavedState != null) mSavedState.close(); } catch (IOException e) {}
-            try { if (mBackupData != null) mBackupData.close(); } catch (IOException e) {}
-            try { if (mNewState != null) mNewState.close(); } catch (IOException e) {}
-            synchronized (mCurrentOpLock) {
-                // Current-operation callback handling requires the validity of these various
-                // bits of internal state as an invariant of the operation still being live.
-                // This means we make sure to clear all of the state in unison inside the lock.
-                mCurrentOperations.remove(mEphemeralOpToken);
-                mSavedState = mBackupData = mNewState = null;
-            }
-
-            // If this was a pseudopackage there's no associated Activity Manager state
-            if (mCurrentPackage.applicationInfo != null) {
-                addBackupTrace("unbinding " + mCurrentPackage.packageName);
-                try {  // unbind even on timeout, just in case
-                    mActivityManager.unbindBackupAgent(mCurrentPackage.applicationInfo);
-                } catch (RemoteException e) { /* can't happen; activity manager is local */ }
-            }
-        }
-
-        void executeNextState(BackupState nextState) {
-            if (MORE_DEBUG) Slog.i(TAG, " => executing next step on "
-                    + this + " nextState=" + nextState);
-            addBackupTrace("executeNextState => " + nextState);
-            mCurrentState = nextState;
-            Message msg = mBackupHandler.obtainMessage(MSG_BACKUP_RESTORE_STEP, this);
-            mBackupHandler.sendMessage(msg);
-        }
-    }
-
-    private boolean isBackupOperationInProgress() {
+    public boolean isBackupOperationInProgress() {
         synchronized (mCurrentOpLock) {
             for (int i = 0; i < mCurrentOperations.size(); i++) {
                 Operation op = mCurrentOperations.valueAt(i);
@@ -3626,102 +1951,7 @@
 
     // ----- Full backup/restore to a file/socket -----
 
-    class FullBackupObbConnection implements ServiceConnection {
-        volatile IObbBackupService mService;
-
-        FullBackupObbConnection() {
-            mService = null;
-        }
-
-        public void establish() {
-            if (MORE_DEBUG) Slog.i(TAG, "Initiating bind of OBB service on " + this);
-            Intent obbIntent = new Intent().setComponent(new ComponentName(
-                    "com.android.sharedstoragebackup",
-                    "com.android.sharedstoragebackup.ObbBackupService"));
-            RefactoredBackupManagerService.this.mContext.bindServiceAsUser(
-                    obbIntent, this, Context.BIND_AUTO_CREATE, UserHandle.SYSTEM);
-        }
-
-        public void tearDown() {
-            RefactoredBackupManagerService.this.mContext.unbindService(this);
-        }
-
-        public boolean backupObbs(PackageInfo pkg, OutputStream out) {
-            boolean success = false;
-            waitForConnection();
-
-            ParcelFileDescriptor[] pipes = null;
-            try {
-                pipes = ParcelFileDescriptor.createPipe();
-                int token = generateRandomIntegerToken();
-                prepareOperationTimeout(token, TIMEOUT_FULL_BACKUP_INTERVAL,
-                        null, OP_TYPE_BACKUP_WAIT);
-                mService.backupObbs(pkg.packageName, pipes[1], token, mBackupManagerBinder);
-                routeSocketDataToOutput(pipes[0], out);
-                success = waitUntilOperationComplete(token);
-            } catch (Exception e) {
-                Slog.w(TAG, "Unable to back up OBBs for " + pkg, e);
-            } finally {
-                try {
-                    out.flush();
-                    if (pipes != null) {
-                        if (pipes[0] != null) pipes[0].close();
-                        if (pipes[1] != null) pipes[1].close();
-                    }
-                } catch (IOException e) {
-                    Slog.w(TAG, "I/O error closing down OBB backup", e);
-                }
-            }
-            return success;
-        }
-
-        public void restoreObbFile(String pkgName, ParcelFileDescriptor data,
-                long fileSize, int type, String path, long mode, long mtime,
-                int token, IBackupManager callbackBinder) {
-            waitForConnection();
-
-            try {
-                mService.restoreObbFile(pkgName, data, fileSize, type, path, mode, mtime,
-                        token, callbackBinder);
-            } catch (Exception e) {
-                Slog.w(TAG, "Unable to restore OBBs for " + pkgName, e);
-            }
-        }
-
-        private void waitForConnection() {
-            synchronized (this) {
-                while (mService == null) {
-                    if (MORE_DEBUG) Slog.i(TAG, "...waiting for OBB service binding...");
-                    try {
-                        this.wait();
-                    } catch (InterruptedException e) { /* never interrupted */ }
-                }
-                if (MORE_DEBUG) Slog.i(TAG, "Connected to OBB service; continuing");
-            }
-        }
-
-        @Override
-        public void onServiceConnected(ComponentName name, IBinder service) {
-            synchronized (this) {
-                mService = IObbBackupService.Stub.asInterface(service);
-                if (MORE_DEBUG) Slog.i(TAG, "OBB service connection " + mService
-                        + " connected on " + this);
-                this.notifyAll();
-            }
-        }
-
-        @Override
-        public void onServiceDisconnected(ComponentName name) {
-            synchronized (this) {
-                mService = null;
-                if (MORE_DEBUG) Slog.i(TAG, "OBB service connection disconnected on " + this);
-                this.notifyAll();
-            }
-        }
-
-    }
-
-    private static void routeSocketDataToOutput(ParcelFileDescriptor inPipe, OutputStream out)
+    public static void routeSocketDataToOutput(ParcelFileDescriptor inPipe, OutputStream out)
             throws IOException {
         // We do not take close() responsibility for the pipe FD
         FileInputStream raw = new FileInputStream(inPipe.getFileDescriptor());
@@ -3768,297 +1998,8 @@
     // Core logic for performing one package's full backup, gathering the tarball from the
     // application and emitting it to the designated OutputStream.
 
-    // Callout from the engine to an interested participant that might need to communicate
-    // with the agent prior to asking it to move data
-    interface FullBackupPreflight {
-        /**
-         * Perform the preflight operation necessary for the given package.
-         * @param pkg The name of the package being proposed for full-data backup
-         * @param agent Live BackupAgent binding to the target app's agent
-         * @return BackupTransport.TRANSPORT_OK to proceed with the backup operation,
-         *         or one of the other BackupTransport.* error codes as appropriate
-         */
-        int preflightFullBackup(PackageInfo pkg, IBackupAgent agent);
-
-        long getExpectedSizeOrErrorCode();
-    }
-
-    class FullBackupEngine {
-        OutputStream mOutput;
-        FullBackupPreflight mPreflightHook;
-        BackupRestoreTask mTimeoutMonitor;
-        IBackupAgent mAgent;
-        File mFilesDir;
-        File mManifestFile;
-        File mMetadataFile;
-        boolean mIncludeApks;
-        PackageInfo mPkg;
-        private final long mQuota;
-        private final int mOpToken;
-
-        class FullBackupRunner implements Runnable {
-            PackageInfo mPackage;
-            byte[] mWidgetData;
-            IBackupAgent mAgent;
-            ParcelFileDescriptor mPipe;
-            int mToken;
-            boolean mSendApk;
-            boolean mWriteManifest;
-
-            FullBackupRunner(PackageInfo pack, IBackupAgent agent, ParcelFileDescriptor pipe,
-                             int token, boolean sendApk, boolean writeManifest, byte[] widgetData)
-                    throws IOException {
-                mPackage = pack;
-                mWidgetData = widgetData;
-                mAgent = agent;
-                mPipe = ParcelFileDescriptor.dup(pipe.getFileDescriptor());
-                mToken = token;
-                mSendApk = sendApk;
-                mWriteManifest = writeManifest;
-            }
-
-            @Override
-            public void run() {
-                try {
-                    FullBackupDataOutput output = new FullBackupDataOutput(mPipe);
-
-                    if (mWriteManifest) {
-                        final boolean writeWidgetData = mWidgetData != null;
-                        if (MORE_DEBUG) Slog.d(TAG, "Writing manifest for " + mPackage.packageName);
-                        writeAppManifest(mPackage, mPackageManager, mManifestFile, mSendApk, writeWidgetData);
-                        FullBackup.backupToTar(mPackage.packageName, null, null,
-                                mFilesDir.getAbsolutePath(),
-                                mManifestFile.getAbsolutePath(),
-                                output);
-                        mManifestFile.delete();
-
-                        // We only need to write a metadata file if we have widget data to stash
-                        if (writeWidgetData) {
-                            writeMetadata(mPackage, mMetadataFile, mWidgetData);
-                            FullBackup.backupToTar(mPackage.packageName, null, null,
-                                    mFilesDir.getAbsolutePath(),
-                                    mMetadataFile.getAbsolutePath(),
-                                    output);
-                            mMetadataFile.delete();
-                        }
-                    }
-
-                    if (mSendApk) {
-                        writeApkToBackup(mPackage, output);
-                    }
-
-                    if (DEBUG) Slog.d(TAG, "Calling doFullBackup() on " + mPackage.packageName);
-                    prepareOperationTimeout(mToken, TIMEOUT_FULL_BACKUP_INTERVAL,
-                            mTimeoutMonitor /* in parent class */, OP_TYPE_BACKUP_WAIT);
-                    mAgent.doFullBackup(mPipe, mQuota, mToken, mBackupManagerBinder);
-                } catch (IOException e) {
-                    Slog.e(TAG, "Error running full backup for " + mPackage.packageName);
-                } catch (RemoteException e) {
-                    Slog.e(TAG, "Remote agent vanished during full backup of "
-                            + mPackage.packageName);
-                } finally {
-                    try {
-                        mPipe.close();
-                    } catch (IOException e) {}
-                }
-            }
-        }
-
-        FullBackupEngine(OutputStream output, FullBackupPreflight preflightHook, PackageInfo pkg,
-                         boolean alsoApks, BackupRestoreTask timeoutMonitor, long quota, int opToken) {
-            mOutput = output;
-            mPreflightHook = preflightHook;
-            mPkg = pkg;
-            mIncludeApks = alsoApks;
-            mTimeoutMonitor = timeoutMonitor;
-            mFilesDir = new File("/data/system");
-            mManifestFile = new File(mFilesDir, BACKUP_MANIFEST_FILENAME);
-            mMetadataFile = new File(mFilesDir, BACKUP_METADATA_FILENAME);
-            mQuota = quota;
-            mOpToken = opToken;
-        }
-
-        public int preflightCheck() throws RemoteException {
-            if (mPreflightHook == null) {
-                if (MORE_DEBUG) {
-                    Slog.v(TAG, "No preflight check");
-                }
-                return BackupTransport.TRANSPORT_OK;
-            }
-            if (initializeAgent()) {
-                int result = mPreflightHook.preflightFullBackup(mPkg, mAgent);
-                if (MORE_DEBUG) {
-                    Slog.v(TAG, "preflight returned " + result);
-                }
-                return result;
-            } else {
-                Slog.w(TAG, "Unable to bind to full agent for " + mPkg.packageName);
-                return BackupTransport.AGENT_ERROR;
-            }
-        }
-
-        public int backupOnePackage() throws RemoteException {
-            int result = BackupTransport.AGENT_ERROR;
-
-            if (initializeAgent()) {
-                ParcelFileDescriptor[] pipes = null;
-                try {
-                    pipes = ParcelFileDescriptor.createPipe();
-
-                    ApplicationInfo app = mPkg.applicationInfo;
-                    final boolean isSharedStorage =
-                            mPkg.packageName.equals(SHARED_BACKUP_AGENT_PACKAGE);
-                    final boolean sendApk = mIncludeApks
-                            && !isSharedStorage
-                            && ((app.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) == 0)
-                            && ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0 ||
-                            (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0);
-
-                    // TODO: http://b/22388012
-                    byte[] widgetBlob = AppWidgetBackupBridge.getWidgetState(mPkg.packageName,
-                            UserHandle.USER_SYSTEM);
-
-                    FullBackupRunner runner = new FullBackupRunner(mPkg, mAgent, pipes[1],
-                            mOpToken, sendApk, !isSharedStorage, widgetBlob);
-                    pipes[1].close();   // the runner has dup'd it
-                    pipes[1] = null;
-                    Thread t = new Thread(runner, "app-data-runner");
-                    t.start();
-
-                    // Now pull data from the app and stuff it into the output
-                    routeSocketDataToOutput(pipes[0], mOutput);
-
-                    if (!waitUntilOperationComplete(mOpToken)) {
-                        Slog.e(TAG, "Full backup failed on package " + mPkg.packageName);
-                    } else {
-                        if (MORE_DEBUG) {
-                            Slog.d(TAG, "Full package backup success: " + mPkg.packageName);
-                        }
-                        result = BackupTransport.TRANSPORT_OK;
-                    }
-                } catch (IOException e) {
-                    Slog.e(TAG, "Error backing up " + mPkg.packageName + ": " + e.getMessage());
-                    result = BackupTransport.AGENT_ERROR;
-                } finally {
-                    try {
-                        // flush after every package
-                        mOutput.flush();
-                        if (pipes != null) {
-                            if (pipes[0] != null) pipes[0].close();
-                            if (pipes[1] != null) pipes[1].close();
-                        }
-                    } catch (IOException e) {
-                        Slog.w(TAG, "Error bringing down backup stack");
-                        result = BackupTransport.TRANSPORT_ERROR;
-                    }
-                }
-            } else {
-                Slog.w(TAG, "Unable to bind to full agent for " + mPkg.packageName);
-            }
-            tearDown();
-            return result;
-        }
-
-        public void sendQuotaExceeded(final long backupDataBytes, final long quotaBytes) {
-            if (initializeAgent()) {
-                try {
-                    mAgent.doQuotaExceeded(backupDataBytes, quotaBytes);
-                } catch (RemoteException e) {
-                    Slog.e(TAG, "Remote exception while telling agent about quota exceeded");
-                }
-            }
-        }
-
-        private boolean initializeAgent() {
-            if (mAgent == null) {
-                if (MORE_DEBUG) {
-                    Slog.d(TAG, "Binding to full backup agent : " + mPkg.packageName);
-                }
-                mAgent = bindToAgentSynchronous(mPkg.applicationInfo,
-                        ApplicationThreadConstants.BACKUP_MODE_FULL);
-            }
-            return mAgent != null;
-        }
-
-        private void writeApkToBackup(PackageInfo pkg, FullBackupDataOutput output) {
-            // Forward-locked apps, system-bundled .apks, etc are filtered out before we get here
-            // TODO: handle backing up split APKs
-            final String appSourceDir = pkg.applicationInfo.getBaseCodePath();
-            final String apkDir = new File(appSourceDir).getParent();
-            FullBackup.backupToTar(pkg.packageName, FullBackup.APK_TREE_TOKEN, null,
-                    apkDir, appSourceDir, output);
-
-            // TODO: migrate this to SharedStorageBackup, since AID_SYSTEM
-            // doesn't have access to external storage.
-
-            // Save associated .obb content if it exists and we did save the apk
-            // check for .obb and save those too
-            // TODO: http://b/22388012
-            final UserEnvironment userEnv = new UserEnvironment(UserHandle.USER_SYSTEM);
-            final File obbDir = userEnv.buildExternalStorageAppObbDirs(pkg.packageName)[0];
-            if (obbDir != null) {
-                if (MORE_DEBUG) Log.i(TAG, "obb dir: " + obbDir.getAbsolutePath());
-                File[] obbFiles = obbDir.listFiles();
-                if (obbFiles != null) {
-                    final String obbDirName = obbDir.getAbsolutePath();
-                    for (File obb : obbFiles) {
-                        FullBackup.backupToTar(pkg.packageName, FullBackup.OBB_TREE_TOKEN, null,
-                                obbDirName, obb.getAbsolutePath(), output);
-                    }
-                }
-            }
-        }
-
-        // Widget metadata format. All header entries are strings ending in LF:
-        //
-        // Version 1 header:
-        //     BACKUP_METADATA_VERSION, currently "1"
-        //     package name
-        //
-        // File data (all integers are binary in network byte order)
-        // *N: 4 : integer token identifying which metadata blob
-        //     4 : integer size of this blob = N
-        //     N : raw bytes of this metadata blob
-        //
-        // Currently understood blobs (always in network byte order):
-        //
-        //     widgets : metadata token = 0x01FFED01 (BACKUP_WIDGET_METADATA_TOKEN)
-        //
-        // Unrecognized blobs are *ignored*, not errors.
-        private void writeMetadata(PackageInfo pkg, File destination, byte[] widgetData)
-                throws IOException {
-            StringBuilder b = new StringBuilder(512);
-            StringBuilderPrinter printer = new StringBuilderPrinter(b);
-            printer.println(Integer.toString(BACKUP_METADATA_VERSION));
-            printer.println(pkg.packageName);
-
-            FileOutputStream fout = new FileOutputStream(destination);
-            BufferedOutputStream bout = new BufferedOutputStream(fout);
-            DataOutputStream out = new DataOutputStream(bout);
-            bout.write(b.toString().getBytes());    // bypassing DataOutputStream
-
-            if (widgetData != null && widgetData.length > 0) {
-                out.writeInt(BACKUP_WIDGET_METADATA_TOKEN);
-                out.writeInt(widgetData.length);
-                out.write(widgetData);
-            }
-            bout.flush();
-            out.close();
-
-            // As with the manifest file, guarantee idempotence of the archive metadata
-            // for the widget block by using a fixed mtime on the transient file.
-            destination.setLastModified(0);
-        }
-
-        private void tearDown() {
-            if (mPkg != null) {
-                tearDownAgentAndKill(mPkg.applicationInfo);
-            }
-        }
-    }
-
-    private static void writeAppManifest(PackageInfo pkg, PackageManager packageManager,
-            File manifestFile, boolean withApk, boolean withWidgets) throws IOException {
+    public static void writeAppManifest(PackageInfo pkg, PackageManager packageManager,
+        File manifestFile, boolean withApk, boolean withWidgets) throws IOException {
         // Manifest format. All data are strings ending in LF:
         //     BACKUP_MANIFEST_VERSION, currently 1
         //
@@ -4103,51 +2044,7 @@
         manifestFile.setLastModified(0);
     }
 
-    // Generic driver skeleton for full backup operations
-    abstract class FullBackupTask implements Runnable {
-        IFullBackupRestoreObserver mObserver;
-
-        FullBackupTask(IFullBackupRestoreObserver observer) {
-            mObserver = observer;
-        }
-
-        // wrappers for observer use
-        final void sendStartBackup() {
-            if (mObserver != null) {
-                try {
-                    mObserver.onStartBackup();
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "full backup observer went away: startBackup");
-                    mObserver = null;
-                }
-            }
-        }
-
-        final void sendOnBackupPackage(String name) {
-            if (mObserver != null) {
-                try {
-                    // TODO: use a more user-friendly name string
-                    mObserver.onBackupPackage(name);
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "full backup observer went away: backupPackage");
-                    mObserver = null;
-                }
-            }
-        }
-
-        final void sendEndBackup() {
-            if (mObserver != null) {
-                try {
-                    mObserver.onEndBackup();
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "full backup observer went away: endBackup");
-                    mObserver = null;
-                }
-            }
-        }
-    }
-
-    private boolean deviceIsEncrypted() {
+    public boolean deviceIsEncrypted() {
         try {
             return mStorageManager.getEncryptionState()
                      != StorageManager.ENCRYPTION_STATE_NONE
@@ -4161,1201 +2058,12 @@
         }
     }
 
-    // Full backup task variant used for adb backup
-    class PerformAdbBackupTask extends FullBackupTask implements BackupRestoreTask {
-        FullBackupEngine mBackupEngine;
-        final AtomicBoolean mLatch;
-
-        ParcelFileDescriptor mOutputFile;
-        DeflaterOutputStream mDeflater;
-        boolean mIncludeApks;
-        boolean mIncludeObbs;
-        boolean mIncludeShared;
-        boolean mDoWidgets;
-        boolean mAllApps;
-        boolean mIncludeSystem;
-        boolean mCompress;
-        boolean mKeyValue;
-        ArrayList<String> mPackages;
-        PackageInfo mCurrentTarget;
-        String mCurrentPassword;
-        String mEncryptPassword;
-        private final int mCurrentOpToken;
-
-        PerformAdbBackupTask(ParcelFileDescriptor fd, IFullBackupRestoreObserver observer,
-                boolean includeApks, boolean includeObbs, boolean includeShared, boolean doWidgets,
-                String curPassword, String encryptPassword, boolean doAllApps, boolean doSystem,
-                boolean doCompress, boolean doKeyValue, String[] packages, AtomicBoolean latch) {
-            super(observer);
-            mCurrentOpToken = generateRandomIntegerToken();
-            mLatch = latch;
-
-            mOutputFile = fd;
-            mIncludeApks = includeApks;
-            mIncludeObbs = includeObbs;
-            mIncludeShared = includeShared;
-            mDoWidgets = doWidgets;
-            mAllApps = doAllApps;
-            mIncludeSystem = doSystem;
-            mPackages = (packages == null)
-                    ? new ArrayList<String>()
-                    : new ArrayList<>(Arrays.asList(packages));
-            mCurrentPassword = curPassword;
-            // when backing up, if there is a current backup password, we require that
-            // the user use a nonempty encryption password as well.  if one is supplied
-            // in the UI we use that, but if the UI was left empty we fall back to the
-            // current backup password (which was supplied by the user as well).
-            if (encryptPassword == null || "".equals(encryptPassword)) {
-                mEncryptPassword = curPassword;
-            } else {
-                mEncryptPassword = encryptPassword;
-            }
-            if (MORE_DEBUG) {
-                Slog.w(TAG, "Encrypting backup with passphrase=" + mEncryptPassword);
-            }
-            mCompress = doCompress;
-            mKeyValue = doKeyValue;
-        }
-
-        void addPackagesToSet(TreeMap<String, PackageInfo> set, List<String> pkgNames) {
-            for (String pkgName : pkgNames) {
-                if (!set.containsKey(pkgName)) {
-                    try {
-                        PackageInfo info = mPackageManager.getPackageInfo(pkgName,
-                                PackageManager.GET_SIGNATURES);
-                        set.put(pkgName, info);
-                    } catch (NameNotFoundException e) {
-                        Slog.w(TAG, "Unknown package " + pkgName + ", skipping");
-                    }
-                }
-            }
-        }
-
-        private OutputStream emitAesBackupHeader(StringBuilder headerbuf,
-                OutputStream ofstream) throws Exception {
-            // User key will be used to encrypt the master key.
-            byte[] newUserSalt = randomBytes(PBKDF2_SALT_SIZE);
-            SecretKey userKey = buildPasswordKey(PBKDF_CURRENT, mEncryptPassword, newUserSalt,
-                    PBKDF2_HASH_ROUNDS);
-
-            // the master key is random for each backup
-            byte[] masterPw = new byte[256 / 8];
-            mRng.nextBytes(masterPw);
-            byte[] checksumSalt = randomBytes(PBKDF2_SALT_SIZE);
-
-            // primary encryption of the datastream with the random key
-            Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
-            SecretKeySpec masterKeySpec = new SecretKeySpec(masterPw, "AES");
-            c.init(Cipher.ENCRYPT_MODE, masterKeySpec);
-            OutputStream finalOutput = new CipherOutputStream(ofstream, c);
-
-            // line 4: name of encryption algorithm
-            headerbuf.append(ENCRYPTION_ALGORITHM_NAME);
-            headerbuf.append('\n');
-            // line 5: user password salt [hex]
-            headerbuf.append(byteArrayToHex(newUserSalt));
-            headerbuf.append('\n');
-            // line 6: master key checksum salt [hex]
-            headerbuf.append(byteArrayToHex(checksumSalt));
-            headerbuf.append('\n');
-            // line 7: number of PBKDF2 rounds used [decimal]
-            headerbuf.append(PBKDF2_HASH_ROUNDS);
-            headerbuf.append('\n');
-
-            // line 8: IV of the user key [hex]
-            Cipher mkC = Cipher.getInstance("AES/CBC/PKCS5Padding");
-            mkC.init(Cipher.ENCRYPT_MODE, userKey);
-
-            byte[] IV = mkC.getIV();
-            headerbuf.append(byteArrayToHex(IV));
-            headerbuf.append('\n');
-
-            // line 9: master IV + key blob, encrypted by the user key [hex].  Blob format:
-            //    [byte] IV length = Niv
-            //    [array of Niv bytes] IV itself
-            //    [byte] master key length = Nmk
-            //    [array of Nmk bytes] master key itself
-            //    [byte] MK checksum hash length = Nck
-            //    [array of Nck bytes] master key checksum hash
-            //
-            // The checksum is the (master key + checksum salt), run through the
-            // stated number of PBKDF2 rounds
-            IV = c.getIV();
-            byte[] mk = masterKeySpec.getEncoded();
-            byte[] checksum = makeKeyChecksum(PBKDF_CURRENT, masterKeySpec.getEncoded(),
-                    checksumSalt, PBKDF2_HASH_ROUNDS);
-
-            ByteArrayOutputStream blob = new ByteArrayOutputStream(IV.length + mk.length
-                    + checksum.length + 3);
-            DataOutputStream mkOut = new DataOutputStream(blob);
-            mkOut.writeByte(IV.length);
-            mkOut.write(IV);
-            mkOut.writeByte(mk.length);
-            mkOut.write(mk);
-            mkOut.writeByte(checksum.length);
-            mkOut.write(checksum);
-            mkOut.flush();
-            byte[] encryptedMk = mkC.doFinal(blob.toByteArray());
-            headerbuf.append(byteArrayToHex(encryptedMk));
-            headerbuf.append('\n');
-
-            return finalOutput;
-        }
-
-        private void finalizeBackup(OutputStream out) {
-            try {
-                // A standard 'tar' EOF sequence: two 512-byte blocks of all zeroes.
-                byte[] eof = new byte[512 * 2]; // newly allocated == zero filled
-                out.write(eof);
-            } catch (IOException e) {
-                Slog.w(TAG, "Error attempting to finalize backup stream");
-            }
-        }
-
-        @Override
-        public void run() {
-            String includeKeyValue = mKeyValue ? ", including key-value backups" : "";
-            Slog.i(TAG, "--- Performing adb backup" + includeKeyValue + " ---");
-
-            TreeMap<String, PackageInfo> packagesToBackup = new TreeMap<>();
-            FullBackupObbConnection obbConnection = new FullBackupObbConnection();
-            obbConnection.establish();  // we'll want this later
-
-            sendStartBackup();
-
-            // doAllApps supersedes the package set if any
-            if (mAllApps) {
-                List<PackageInfo> allPackages = mPackageManager.getInstalledPackages(
-                        PackageManager.GET_SIGNATURES);
-                for (int i = 0; i < allPackages.size(); i++) {
-                    PackageInfo pkg = allPackages.get(i);
-                    // Exclude system apps if we've been asked to do so
-                    if (mIncludeSystem == true
-                            || ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)) {
-                        packagesToBackup.put(pkg.packageName, pkg);
-                    }
-                }
-            }
-
-            // If we're doing widget state as well, ensure that we have all the involved
-            // host & provider packages in the set
-            if (mDoWidgets) {
-                // TODO: http://b/22388012
-                List<String> pkgs =
-                        AppWidgetBackupBridge.getWidgetParticipants(UserHandle.USER_SYSTEM);
-                if (pkgs != null) {
-                    if (MORE_DEBUG) {
-                        Slog.i(TAG, "Adding widget participants to backup set:");
-                        StringBuilder sb = new StringBuilder(128);
-                        sb.append("   ");
-                        for (String s : pkgs) {
-                            sb.append(' ');
-                            sb.append(s);
-                        }
-                        Slog.i(TAG, sb.toString());
-                    }
-                    addPackagesToSet(packagesToBackup, pkgs);
-                }
-            }
-
-            // Now process the command line argument packages, if any. Note that explicitly-
-            // named system-partition packages will be included even if includeSystem was
-            // set to false.
-            if (mPackages != null) {
-                addPackagesToSet(packagesToBackup, mPackages);
-            }
-
-            // Now we cull any inapplicable / inappropriate packages from the set.  This
-            // includes the special shared-storage agent package; we handle that one
-            // explicitly at the end of the backup pass. Packages supporting key-value backup are
-            // added to their own queue, and handled after packages supporting fullbackup.
-            ArrayList<PackageInfo> keyValueBackupQueue = new ArrayList<>();
-            Iterator<Entry<String, PackageInfo>> iter = packagesToBackup.entrySet().iterator();
-            while (iter.hasNext()) {
-                PackageInfo pkg = iter.next().getValue();
-                if (!appIsEligibleForBackup(pkg.applicationInfo)
-                        || appIsStopped(pkg.applicationInfo)) {
-                    iter.remove();
-                    if (DEBUG) {
-                        Slog.i(TAG, "Package " + pkg.packageName
-                                + " is not eligible for backup, removing.");
-                    }
-                } else if (appIsKeyValueOnly(pkg)) {
-                    iter.remove();
-                    if (DEBUG) {
-                        Slog.i(TAG, "Package " + pkg.packageName
-                                + " is key-value.");
-                    }
-                    keyValueBackupQueue.add(pkg);
-                }
-            }
-
-            // flatten the set of packages now so we can explicitly control the ordering
-            ArrayList<PackageInfo> backupQueue =
-                new ArrayList<>(packagesToBackup.values());
-            FileOutputStream ofstream = new FileOutputStream(mOutputFile.getFileDescriptor());
-            OutputStream out = null;
-
-            PackageInfo pkg = null;
-            try {
-                boolean encrypting = (mEncryptPassword != null && mEncryptPassword.length() > 0);
-
-                // Only allow encrypted backups of encrypted devices
-                if (deviceIsEncrypted() && !encrypting) {
-                    Slog.e(TAG, "Unencrypted backup of encrypted device; aborting");
-                    return;
-                }
-
-                OutputStream finalOutput = ofstream;
-
-                // Verify that the given password matches the currently-active
-                // backup password, if any
-                if (!backupPasswordMatches(mCurrentPassword)) {
-                    if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting");
-                    return;
-                }
-
-                // Write the global file header.  All strings are UTF-8 encoded; lines end
-                // with a '\n' byte.  Actual backup data begins immediately following the
-                // final '\n'.
-                //
-                // line 1: "ANDROID BACKUP"
-                // line 2: backup file format version, currently "5"
-                // line 3: compressed?  "0" if not compressed, "1" if compressed.
-                // line 4: name of encryption algorithm [currently only "none" or "AES-256"]
-                //
-                // When line 4 is not "none", then additional header data follows:
-                //
-                // line 5: user password salt [hex]
-                // line 6: master key checksum salt [hex]
-                // line 7: number of PBKDF2 rounds to use (same for user & master) [decimal]
-                // line 8: IV of the user key [hex]
-                // line 9: master key blob [hex]
-                //     IV of the master key, master key itself, master key checksum hash
-                //
-                // The master key checksum is the master key plus its checksum salt, run through
-                // 10k rounds of PBKDF2.  This is used to verify that the user has supplied the
-                // correct password for decrypting the archive:  the master key decrypted from
-                // the archive using the user-supplied password is also run through PBKDF2 in
-                // this way, and if the result does not match the checksum as stored in the
-                // archive, then we know that the user-supplied password does not match the
-                // archive's.
-                StringBuilder headerbuf = new StringBuilder(1024);
-
-                headerbuf.append(BACKUP_FILE_HEADER_MAGIC);
-                headerbuf.append(BACKUP_FILE_VERSION); // integer, no trailing \n
-                headerbuf.append(mCompress ? "\n1\n" : "\n0\n");
-
-                try {
-                    // Set up the encryption stage if appropriate, and emit the correct header
-                    if (encrypting) {
-                        finalOutput = emitAesBackupHeader(headerbuf, finalOutput);
-                    } else {
-                        headerbuf.append("none\n");
-                    }
-
-                    byte[] header = headerbuf.toString().getBytes("UTF-8");
-                    ofstream.write(header);
-
-                    // Set up the compression stage feeding into the encryption stage (if any)
-                    if (mCompress) {
-                        Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
-                        finalOutput = new DeflaterOutputStream(finalOutput, deflater, true);
-                    }
-
-                    out = finalOutput;
-                } catch (Exception e) {
-                    // Should never happen!
-                    Slog.e(TAG, "Unable to emit archive header", e);
-                    return;
-                }
-
-                // Shared storage if requested
-                if (mIncludeShared) {
-                    try {
-                        pkg = mPackageManager.getPackageInfo(SHARED_BACKUP_AGENT_PACKAGE, 0);
-                        backupQueue.add(pkg);
-                    } catch (NameNotFoundException e) {
-                        Slog.e(TAG, "Unable to find shared-storage backup handler");
-                    }
-                }
-
-                // Now actually run the constructed backup sequence for full backup
-                int N = backupQueue.size();
-                for (int i = 0; i < N; i++) {
-                    pkg = backupQueue.get(i);
-                    if (DEBUG) {
-                        Slog.i(TAG,"--- Performing full backup for package " + pkg.packageName
-                                + " ---");
-                    }
-                    final boolean isSharedStorage =
-                            pkg.packageName.equals(SHARED_BACKUP_AGENT_PACKAGE);
-
-                    mBackupEngine = new FullBackupEngine(out, null, pkg, mIncludeApks, this, Long.MAX_VALUE, mCurrentOpToken);
-                    sendOnBackupPackage(isSharedStorage ? "Shared storage" : pkg.packageName);
-
-                    // Don't need to check preflight result as there is no preflight hook.
-                    mCurrentTarget = pkg;
-                    mBackupEngine.backupOnePackage();
-
-                    // after the app's agent runs to handle its private filesystem
-                    // contents, back up any OBB content it has on its behalf.
-                    if (mIncludeObbs) {
-                        boolean obbOkay = obbConnection.backupObbs(pkg, out);
-                        if (!obbOkay) {
-                            throw new RuntimeException("Failure writing OBB stack for " + pkg);
-                        }
-                    }
-                }
-                // And for key-value backup if enabled
-                if (mKeyValue) {
-                    for (PackageInfo keyValuePackage : keyValueBackupQueue) {
-                        if (DEBUG) {
-                            Slog.i(TAG, "--- Performing key-value backup for package "
-                                    + keyValuePackage.packageName + " ---");
-                        }
-                        KeyValueAdbBackupEngine kvBackupEngine =
-                                new KeyValueAdbBackupEngine(out, keyValuePackage,
-                                        RefactoredBackupManagerService.this,
-                                        mPackageManager, mBaseStateDir, mDataDir);
-                        sendOnBackupPackage(keyValuePackage.packageName);
-                        kvBackupEngine.backupOnePackage();
-                    }
-                }
-
-                // Done!
-                finalizeBackup(out);
-            } catch (RemoteException e) {
-                Slog.e(TAG, "App died during full backup");
-            } catch (Exception e) {
-                Slog.e(TAG, "Internal exception during full backup", e);
-            } finally {
-                try {
-                    if (out != null) {
-                        out.flush();
-                        out.close();
-                    }
-                    mOutputFile.close();
-                } catch (IOException e) {
-                    /* nothing we can do about this */
-                }
-                synchronized (mLatch) {
-                    mLatch.set(true);
-                    mLatch.notifyAll();
-                }
-                sendEndBackup();
-                obbConnection.tearDown();
-                if (DEBUG) Slog.d(TAG, "Full backup pass complete.");
-                mWakelock.release();
-            }
-        }
-
-        // BackupRestoreTask methods, used for timeout handling
-        @Override
-        public void execute() {
-            // Unused
-        }
-
-        @Override
-        public void operationComplete(long result) {
-            // Unused
-        }
-
-        @Override
-        public void handleCancel(boolean cancelAll) {
-            final PackageInfo target = mCurrentTarget;
-            if (DEBUG) {
-                Slog.w(TAG, "adb backup cancel of " + target);
-            }
-            if (target != null) {
-                tearDownAgentAndKill(mCurrentTarget.applicationInfo);
-            }
-            removeOperation(mCurrentOpToken);
-        }
-    }
-
-    /**
-     * Full backup task extension used for transport-oriented operation.
-     *
-     * Flow:
-     * For each requested package:
-     *     - Spin off a new SinglePackageBackupRunner (mBackupRunner) for the current package.
-     *     - Wait until preflight is complete. (mBackupRunner.getPreflightResultBlocking())
-     *     - If preflight data size is within limit, start reading data from agent pipe and writing
-     *       to transport pipe. While there is data to send, call transport.sendBackupData(int) to
-     *       tell the transport how many bytes to expect on its pipe.
-     *     - After sending all data, call transport.finishBackup() if things went well. And
-     *       transport.cancelFullBackup() otherwise.
-     *
-     * Interactions with mCurrentOperations:
-     *     - An entry for this object is added to mCurrentOperations for the entire lifetime of this
-     *       object. Used to cancel the operation.
-     *     - SinglePackageBackupRunner and SinglePackageBackupPreflight will put ephemeral entries
-     *       to get timeouts or operation complete callbacks.
-     *
-     * Handling cancels:
-     *     - The contract we provide is that the task won't interact with the transport after
-     *       handleCancel() is done executing.
-     *     - This task blocks at 3 points: 1. Preflight result check 2. Reading on agent side pipe
-     *       and 3. Get backup result from mBackupRunner.
-     *     - Bubbling up handleCancel to mBackupRunner handles all 3: 1. Calls handleCancel on the
-     *       preflight operation which counts down on the preflight latch. 2. Tears down the agent,
-     *       so read() returns -1. 3. Notifies mCurrentOpLock which unblocks
-     *       mBackupRunner.getBackupResultBlocking().
-     */
-    class PerformFullTransportBackupTask extends FullBackupTask implements BackupRestoreTask {
-        private static final String TAG = "PFTBT";
-
-        private final Object mCancelLock = new Object();
-
-        ArrayList<PackageInfo> mPackages;
-        PackageInfo mCurrentPackage;
-        boolean mUpdateSchedule;
-        CountDownLatch mLatch;
-        FullBackupJob mJob;             // if a scheduled job needs to be finished afterwards
-        IBackupObserver mBackupObserver;
-        IBackupManagerMonitor mMonitor;
-        boolean mUserInitiated;
-        private volatile IBackupTransport mTransport;
-        SinglePackageBackupRunner mBackupRunner;
-        private final int mBackupRunnerOpToken;
-
-        // This is true when a backup operation for some package is in progress.
-        private volatile boolean mIsDoingBackup;
-        private volatile boolean mCancelAll;
-        private final int mCurrentOpToken;
-
-        PerformFullTransportBackupTask(IFullBackupRestoreObserver observer,
-                String[] whichPackages, boolean updateSchedule,
-                FullBackupJob runningJob, CountDownLatch latch, IBackupObserver backupObserver,
-                IBackupManagerMonitor monitor, boolean userInitiated) {
-            super(observer);
-            mUpdateSchedule = updateSchedule;
-            mLatch = latch;
-            mJob = runningJob;
-            mPackages = new ArrayList<>(whichPackages.length);
-            mBackupObserver = backupObserver;
-            mMonitor = monitor;
-            mUserInitiated = userInitiated;
-            mCurrentOpToken = generateRandomIntegerToken();
-            mBackupRunnerOpToken = generateRandomIntegerToken();
-
-            if (isBackupOperationInProgress()) {
-                if (DEBUG) {
-                    Slog.d(TAG, "Skipping full backup. A backup is already in progress.");
-                }
-                mCancelAll = true;
-                return;
-            }
-
-            registerTask();
-
-            for (String pkg : whichPackages) {
-                try {
-                    PackageInfo info = mPackageManager.getPackageInfo(pkg,
-                            PackageManager.GET_SIGNATURES);
-                    mCurrentPackage = info;
-                    if (!appIsEligibleForBackup(info.applicationInfo)) {
-                        // Cull any packages that have indicated that backups are not permitted,
-                        // that run as system-domain uids but do not define their own backup agents,
-                        // as well as any explicit mention of the 'special' shared-storage agent
-                        // package (we handle that one at the end).
-                        if (MORE_DEBUG) {
-                            Slog.d(TAG, "Ignoring ineligible package " + pkg);
-                        }
-                        mMonitor = monitorEvent(mMonitor,
-                                BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_INELIGIBLE,
-                                mCurrentPackage,
-                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                null);
-                        sendBackupOnPackageResult(mBackupObserver, pkg,
-                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
-                        continue;
-                    } else if (!appGetsFullBackup(info)) {
-                        // Cull any packages that are found in the queue but now aren't supposed
-                        // to get full-data backup operations.
-                        if (MORE_DEBUG) {
-                            Slog.d(TAG, "Ignoring full-data backup of key/value participant "
-                                    + pkg);
-                        }
-                        mMonitor = monitorEvent(mMonitor,
-                                BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_KEY_VALUE_PARTICIPANT,
-                                mCurrentPackage,
-                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                null);
-                        sendBackupOnPackageResult(mBackupObserver, pkg,
-                                BackupManager.ERROR_BACKUP_NOT_ALLOWED);
-                        continue;
-                    } else if (appIsStopped(info.applicationInfo)) {
-                        // Cull any packages in the 'stopped' state: they've either just been
-                        // installed or have explicitly been force-stopped by the user.  In both
-                        // cases we do not want to launch them for backup.
-                        if (MORE_DEBUG) {
-                            Slog.d(TAG, "Ignoring stopped package " + pkg);
-                        }
-                        mMonitor = monitorEvent(mMonitor,
-                                BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_STOPPED,
-                                mCurrentPackage,
-                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                null);
-                        sendBackupOnPackageResult(mBackupObserver, pkg,
-                                BackupManager.ERROR_BACKUP_NOT_ALLOWED);
-                        continue;
-                    }
-                    mPackages.add(info);
-                } catch (NameNotFoundException e) {
-                    Slog.i(TAG, "Requested package " + pkg + " not found; ignoring");
-                    mMonitor = monitorEvent(mMonitor,
-                            BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_NOT_FOUND,
-                            mCurrentPackage,
-                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                            null);
-                }
-            }
-        }
-
-        private void registerTask() {
-            synchronized (mCurrentOpLock) {
-                Slog.d(TAG, "backupmanager pftbt token=" + Integer.toHexString(mCurrentOpToken));
-                mCurrentOperations.put(mCurrentOpToken, new Operation(OP_PENDING, this,
-                        OP_TYPE_BACKUP));
-            }
-        }
-
-        private void unregisterTask() {
-            removeOperation(mCurrentOpToken);
-        }
-
-        @Override
-        public void execute() {
-            // Nothing to do.
-        }
-
-        @Override
-        public void handleCancel(boolean cancelAll) {
-            synchronized (mCancelLock) {
-                // We only support 'cancelAll = true' case for this task. Cancelling of a single package
-
-                // due to timeout is handled by SinglePackageBackupRunner and SinglePackageBackupPreflight.
-
-                if (!cancelAll) {
-                    Slog.wtf(TAG, "Expected cancelAll to be true.");
-                }
-
-                if (mCancelAll) {
-                    Slog.d(TAG, "Ignoring duplicate cancel call.");
-                    return;
-                }
-
-                mCancelAll = true;
-                if (mIsDoingBackup) {
-                    RefactoredBackupManagerService.this.handleCancel(mBackupRunnerOpToken, cancelAll);
-                    try {
-                        mTransport.cancelFullBackup();
-                    } catch (RemoteException e) {
-                        Slog.w(TAG, "Error calling cancelFullBackup() on transport: " + e);
-                        // Can't do much.
-                    }
-                }
-            }
-        }
-
-        @Override
-        public void operationComplete(long result) {
-            // Nothing to do.
-        }
-
-        @Override
-        public void run() {
-
-            // data from the app, passed to us for bridging to the transport
-            ParcelFileDescriptor[] enginePipes = null;
-
-            // Pipe through which we write data to the transport
-            ParcelFileDescriptor[] transportPipes = null;
-
-            long backoff = 0;
-            int backupRunStatus = BackupManager.SUCCESS;
-
-            try {
-                if (!mEnabled || !mProvisioned) {
-                    // Backups are globally disabled, so don't proceed.
-                    if (DEBUG) {
-                        Slog.i(TAG, "full backup requested but enabled=" + mEnabled
-                                + " provisioned=" + mProvisioned + "; ignoring");
-                    }
-                    int monitoringEvent;
-                    if (!mEnabled) {
-                        monitoringEvent = BackupManagerMonitor.LOG_EVENT_ID_BACKUP_DISABLED;
-                    } else {
-                        monitoringEvent = BackupManagerMonitor.LOG_EVENT_ID_DEVICE_NOT_PROVISIONED;
-                    }
-                    mMonitor = monitorEvent(mMonitor, monitoringEvent, null,
-                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
-                    mUpdateSchedule = false;
-                    backupRunStatus = BackupManager.ERROR_BACKUP_NOT_ALLOWED;
-                    return;
-                }
-
-                mTransport = mTransportManager.getCurrentTransportBinder();
-                if (mTransport == null) {
-                    Slog.w(TAG, "Transport not present; full data backup not performed");
-                    backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
-                    mMonitor = monitorEvent(mMonitor,
-                            BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_TRANSPORT_NOT_PRESENT,
-                            mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT,
-                            null);
-                    return;
-                }
-
-                // Set up to send data to the transport
-                final int N = mPackages.size();
-                final byte[] buffer = new byte[8192];
-                for (int i = 0; i < N; i++) {
-                    PackageInfo currentPackage = mPackages.get(i);
-                    String packageName = currentPackage.packageName;
-                    if (DEBUG) {
-                        Slog.i(TAG, "Initiating full-data transport backup of " + packageName
-                                + " token: " + mCurrentOpToken);
-                    }
-                    EventLog.writeEvent(EventLogTags.FULL_BACKUP_PACKAGE, packageName);
-
-                    transportPipes = ParcelFileDescriptor.createPipe();
-
-                    // Tell the transport the data's coming
-                    int flags = mUserInitiated ? BackupTransport.FLAG_USER_INITIATED : 0;
-                    int backupPackageStatus;
-                    long quota = Long.MAX_VALUE;
-                    synchronized (mCancelLock) {
-                        if (mCancelAll) {
-                            break;
-                        }
-                        backupPackageStatus = mTransport.performFullBackup(currentPackage,
-                                transportPipes[0], flags);
-
-                        if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
-                            quota = mTransport.getBackupQuota(currentPackage.packageName,
-                                    true /* isFullBackup */);
-                            // Now set up the backup engine / data source end of things
-                            enginePipes = ParcelFileDescriptor.createPipe();
-                            mBackupRunner =
-                                    new SinglePackageBackupRunner(enginePipes[1], currentPackage,
-                                            mTransport, quota, mBackupRunnerOpToken);
-                            // The runner dup'd the pipe half, so we close it here
-                            enginePipes[1].close();
-                            enginePipes[1] = null;
-
-                            mIsDoingBackup = true;
-                        }
-                    }
-                    if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
-
-                        // The transport has its own copy of the read end of the pipe,
-                        // so close ours now
-                        transportPipes[0].close();
-                        transportPipes[0] = null;
-
-                        // Spin off the runner to fetch the app's data and pipe it
-                        // into the engine pipes
-                        (new Thread(mBackupRunner, "package-backup-bridge")).start();
-
-                        // Read data off the engine pipe and pass it to the transport
-                        // pipe until we hit EOD on the input stream.  We do not take
-                        // close() responsibility for these FDs into these stream wrappers.
-                        FileInputStream in = new FileInputStream(
-                                enginePipes[0].getFileDescriptor());
-                        FileOutputStream out = new FileOutputStream(
-                                transportPipes[1].getFileDescriptor());
-                        long totalRead = 0;
-                        final long preflightResult = mBackupRunner.getPreflightResultBlocking();
-                        // Preflight result is negative if some error happened on preflight.
-                        if (preflightResult < 0) {
-                            if (MORE_DEBUG) {
-                                Slog.d(TAG, "Backup error after preflight of package "
-                                        + packageName + ": " + preflightResult
-                                        + ", not running backup.");
-                            }
-                            mMonitor = monitorEvent(mMonitor,
-                                    BackupManagerMonitor.LOG_EVENT_ID_ERROR_PREFLIGHT,
-                                    mCurrentPackage,
-                                    BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                    putMonitoringExtra(null,
-                                            BackupManagerMonitor.EXTRA_LOG_PREFLIGHT_ERROR,
-                                            preflightResult));
-                            backupPackageStatus = (int) preflightResult;
-                        } else {
-                            int nRead = 0;
-                            do {
-                                nRead = in.read(buffer);
-                                if (MORE_DEBUG) {
-                                    Slog.v(TAG, "in.read(buffer) from app: " + nRead);
-                                }
-                                if (nRead > 0) {
-                                    out.write(buffer, 0, nRead);
-                                    synchronized (mCancelLock) {
-                                        if (!mCancelAll) {
-                                            backupPackageStatus = mTransport.sendBackupData(nRead);
-                                        }
-                                    }
-                                    totalRead += nRead;
-                                    if (mBackupObserver != null && preflightResult > 0) {
-                                        sendBackupOnUpdate(mBackupObserver, packageName,
-                                                new BackupProgress(preflightResult, totalRead));
-                                    }
-                                }
-                            } while (nRead > 0
-                                    && backupPackageStatus == BackupTransport.TRANSPORT_OK);
-                            // Despite preflight succeeded, package still can hit quota on flight.
-                            if (backupPackageStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
-                                Slog.w(TAG, "Package hit quota limit in-flight " + packageName
-                                        + ": " + totalRead + " of " + quota);
-                                mMonitor = monitorEvent(mMonitor,
-                                        BackupManagerMonitor.LOG_EVENT_ID_QUOTA_HIT_PREFLIGHT,
-                                        mCurrentPackage,
-                                        BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT,
-                                        null);
-                                mBackupRunner.sendQuotaExceeded(totalRead, quota);
-                            }
-                        }
-
-                        final int backupRunnerResult = mBackupRunner.getBackupResultBlocking();
-
-                        synchronized (mCancelLock) {
-                            mIsDoingBackup = false;
-                            // If mCancelCurrent is true, we have already called cancelFullBackup().
-                            if (!mCancelAll) {
-                                if (backupRunnerResult == BackupTransport.TRANSPORT_OK) {
-                                    // If we were otherwise in a good state, now interpret the final
-                                    // result based on what finishBackup() returns.  If we're in a
-                                    // failure case already, preserve that result and ignore whatever
-                                    // finishBackup() reports.
-                                    final int finishResult = mTransport.finishBackup();
-                                    if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
-                                        backupPackageStatus = finishResult;
-                                    }
-                                } else {
-                                    mTransport.cancelFullBackup();
-                                }
-                            }
-                        }
-
-                        // A transport-originated error here means that we've hit an error that the
-                        // runner doesn't know about, so it's still moving data but we're pulling the
-                        // rug out from under it.  Don't ask for its result:  we already know better
-                        // and we'll hang if we block waiting for it, since it relies on us to
-                        // read back the data it's writing into the engine.  Just proceed with
-                        // a graceful failure.  The runner/engine mechanism will tear itself
-                        // down cleanly when we close the pipes from this end.  Transport-level
-                        // errors take precedence over agent/app-specific errors for purposes of
-                        // determining our course of action.
-                        if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
-                            // We still could fail in backup runner thread.
-                            if (backupRunnerResult != BackupTransport.TRANSPORT_OK) {
-                                // If there was an error in runner thread and
-                                // not TRANSPORT_ERROR here, overwrite it.
-                                backupPackageStatus = backupRunnerResult;
-                            }
-                        } else {
-                            if (MORE_DEBUG) {
-                                Slog.i(TAG, "Transport-level failure; cancelling agent work");
-                            }
-                        }
-
-                        if (MORE_DEBUG) {
-                            Slog.i(TAG, "Done delivering backup data: result="
-                                    + backupPackageStatus);
-                        }
-
-                        if (backupPackageStatus != BackupTransport.TRANSPORT_OK) {
-                            Slog.e(TAG, "Error " + backupPackageStatus + " backing up "
-                                    + packageName);
-                        }
-
-                        // Also ask the transport how long it wants us to wait before
-                        // moving on to the next package, if any.
-                        backoff = mTransport.requestFullBackupTime();
-                        if (DEBUG_SCHEDULING) {
-                            Slog.i(TAG, "Transport suggested backoff=" + backoff);
-                        }
-
-                    }
-
-                    // Roll this package to the end of the backup queue if we're
-                    // in a queue-driven mode (regardless of success/failure)
-                    if (mUpdateSchedule) {
-                        enqueueFullBackup(packageName, System.currentTimeMillis());
-                    }
-
-                    if (backupPackageStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
-                        sendBackupOnPackageResult(mBackupObserver, packageName,
-                                BackupManager.ERROR_TRANSPORT_PACKAGE_REJECTED);
-                        if (DEBUG) {
-                            Slog.i(TAG, "Transport rejected backup of " + packageName
-                                    + ", skipping");
-                        }
-                        EventLog.writeEvent(EventLogTags.FULL_BACKUP_AGENT_FAILURE, packageName,
-                                "transport rejected");
-                        // Do nothing, clean up, and continue looping.
-                    } else if (backupPackageStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
-                        sendBackupOnPackageResult(mBackupObserver, packageName,
-                                BackupManager.ERROR_TRANSPORT_QUOTA_EXCEEDED);
-                        if (DEBUG) {
-                            Slog.i(TAG, "Transport quota exceeded for package: " + packageName);
-                            EventLog.writeEvent(EventLogTags.FULL_BACKUP_QUOTA_EXCEEDED,
-                                    packageName);
-                        }
-                        // Do nothing, clean up, and continue looping.
-                    } else if (backupPackageStatus == BackupTransport.AGENT_ERROR) {
-                        sendBackupOnPackageResult(mBackupObserver, packageName,
-                                BackupManager.ERROR_AGENT_FAILURE);
-                        Slog.w(TAG, "Application failure for package: " + packageName);
-                        EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName);
-                        tearDownAgentAndKill(currentPackage.applicationInfo);
-                        // Do nothing, clean up, and continue looping.
-                    } else if (backupPackageStatus == BackupManager.ERROR_BACKUP_CANCELLED) {
-                        sendBackupOnPackageResult(mBackupObserver, packageName,
-                                BackupManager.ERROR_BACKUP_CANCELLED);
-                        Slog.w(TAG, "Backup cancelled. package=" + packageName +
-                                ", cancelAll=" + mCancelAll);
-                        EventLog.writeEvent(EventLogTags.FULL_BACKUP_CANCELLED, packageName);
-                        tearDownAgentAndKill(currentPackage.applicationInfo);
-                        // Do nothing, clean up, and continue looping.
-                    } else if (backupPackageStatus != BackupTransport.TRANSPORT_OK) {
-                        sendBackupOnPackageResult(mBackupObserver, packageName,
-                            BackupManager.ERROR_TRANSPORT_ABORTED);
-                        Slog.w(TAG, "Transport failed; aborting backup: " + backupPackageStatus);
-                        EventLog.writeEvent(EventLogTags.FULL_BACKUP_TRANSPORT_FAILURE);
-                        // Abort entire backup pass.
-                        backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
-                        return;
-                    } else {
-                        // Success!
-                        sendBackupOnPackageResult(mBackupObserver, packageName,
-                                BackupManager.SUCCESS);
-                        EventLog.writeEvent(EventLogTags.FULL_BACKUP_SUCCESS, packageName);
-                        logBackupComplete(packageName);
-                    }
-                    cleanUpPipes(transportPipes);
-                    cleanUpPipes(enginePipes);
-                    if (currentPackage.applicationInfo != null) {
-                        Slog.i(TAG, "Unbinding agent in " + packageName);
-                        addBackupTrace("unbinding " + packageName);
-                        try {
-                            mActivityManager.unbindBackupAgent(currentPackage.applicationInfo);
-                        } catch (RemoteException e) { /* can't happen; activity manager is local */ }
-                    }
-                }
-            } catch (Exception e) {
-                backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
-                Slog.w(TAG, "Exception trying full transport backup", e);
-                mMonitor = monitorEvent(mMonitor,
-                        BackupManagerMonitor.LOG_EVENT_ID_EXCEPTION_FULL_BACKUP,
-                        mCurrentPackage,
-                        BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                        putMonitoringExtra(null,
-                                BackupManagerMonitor.EXTRA_LOG_EXCEPTION_FULL_BACKUP,
-                                Log.getStackTraceString(e)));
-
-            } finally {
-
-                if (mCancelAll) {
-                    backupRunStatus = BackupManager.ERROR_BACKUP_CANCELLED;
-                }
-
-                if (DEBUG) {
-                    Slog.i(TAG, "Full backup completed with status: " + backupRunStatus);
-                }
-                sendBackupFinished(mBackupObserver, backupRunStatus);
-
-                cleanUpPipes(transportPipes);
-                cleanUpPipes(enginePipes);
-
-                unregisterTask();
-
-                if (mJob != null) {
-                    mJob.finishBackupPass();
-                }
-
-                synchronized (mQueueLock) {
-                    mRunningFullBackupTask = null;
-                }
-
-                mLatch.countDown();
-
-                // Now that we're actually done with schedule-driven work, reschedule
-                // the next pass based on the new queue state.
-                if (mUpdateSchedule) {
-                    scheduleNextFullBackupJob(backoff);
-                }
-
-                Slog.i(RefactoredBackupManagerService.TAG, "Full data backup pass finished.");
-                mWakelock.release();
-            }
-        }
-
-        void cleanUpPipes(ParcelFileDescriptor[] pipes) {
-            if (pipes != null) {
-                if (pipes[0] != null) {
-                    ParcelFileDescriptor fd = pipes[0];
-                    pipes[0] = null;
-                    try {
-                        fd.close();
-                    } catch (IOException e) {
-                        Slog.w(TAG, "Unable to close pipe!");
-                    }
-                }
-                if (pipes[1] != null) {
-                    ParcelFileDescriptor fd = pipes[1];
-                    pipes[1] = null;
-                    try {
-                        fd.close();
-                    } catch (IOException e) {
-                        Slog.w(TAG, "Unable to close pipe!");
-                    }
-                }
-            }
-        }
-
-        // Run the backup and pipe it back to the given socket -- expects to run on
-        // a standalone thread.  The  runner owns this half of the pipe, and closes
-        // it to indicate EOD to the other end.
-        class SinglePackageBackupPreflight implements BackupRestoreTask, FullBackupPreflight {
-            final AtomicLong mResult = new AtomicLong(BackupTransport.AGENT_ERROR);
-            final CountDownLatch mLatch = new CountDownLatch(1);
-            final IBackupTransport mTransport;
-            final long mQuota;
-            private final int mCurrentOpToken;
-
-            SinglePackageBackupPreflight(IBackupTransport transport, long quota, int currentOpToken) {
-                mTransport = transport;
-                mQuota = quota;
-                mCurrentOpToken = currentOpToken;
-            }
-
-            @Override
-            public int preflightFullBackup(PackageInfo pkg, IBackupAgent agent) {
-                int result;
-                try {
-                    prepareOperationTimeout(mCurrentOpToken, TIMEOUT_FULL_BACKUP_INTERVAL,
-                            this, OP_TYPE_BACKUP_WAIT);
-                    addBackupTrace("preflighting");
-                    if (MORE_DEBUG) {
-                        Slog.d(TAG, "Preflighting full payload of " + pkg.packageName);
-                    }
-                    agent.doMeasureFullBackup(mQuota, mCurrentOpToken, mBackupManagerBinder);
-
-                    // Now wait to get our result back.  If this backstop timeout is reached without
-                    // the latch being thrown, flow will continue as though a result or "normal"
-                    // timeout had been produced.  In case of a real backstop timeout, mResult
-                    // will still contain the value it was constructed with, AGENT_ERROR, which
-                    // intentionaly falls into the "just report failure" code.
-                    mLatch.await(TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
-
-                    long totalSize = mResult.get();
-                    // If preflight timed out, mResult will contain error code as int.
-                    if (totalSize < 0) {
-                        return (int) totalSize;
-                    }
-                    if (MORE_DEBUG) {
-                        Slog.v(TAG, "Got preflight response; size=" + totalSize);
-                    }
-
-                    result = mTransport.checkFullBackupSize(totalSize);
-                    if (result == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
-                        if (MORE_DEBUG) {
-                            Slog.d(TAG, "Package hit quota limit on preflight " +
-                                    pkg.packageName + ": " + totalSize + " of " + mQuota);
-                        }
-                        agent.doQuotaExceeded(totalSize, mQuota);
-                    }
-                } catch (Exception e) {
-                    Slog.w(TAG, "Exception preflighting " + pkg.packageName + ": " + e.getMessage());
-                    result = BackupTransport.AGENT_ERROR;
-                }
-                return result;
-            }
-
-            @Override
-            public void execute() {
-                // Unused.
-            }
-
-            @Override
-            public void operationComplete(long result) {
-                // got the callback, and our preflightFullBackup() method is waiting for the result
-                if (MORE_DEBUG) {
-                    Slog.i(TAG, "Preflight op complete, result=" + result);
-                }
-                mResult.set(result);
-                mLatch.countDown();
-                removeOperation(mCurrentOpToken);
-            }
-
-            @Override
-            public void handleCancel(boolean cancelAll) {
-                if (MORE_DEBUG) {
-                    Slog.i(TAG, "Preflight cancelled; failing");
-                }
-                mResult.set(BackupTransport.AGENT_ERROR);
-                mLatch.countDown();
-                removeOperation(mCurrentOpToken);
-            }
-
-            @Override
-            public long getExpectedSizeOrErrorCode() {
-                try {
-                    mLatch.await(TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
-                    return mResult.get();
-                } catch (InterruptedException e) {
-                    return BackupTransport.NO_MORE_DATA;
-                }
-            }
-        }
-
-        class SinglePackageBackupRunner implements Runnable, BackupRestoreTask {
-            final ParcelFileDescriptor mOutput;
-            final PackageInfo mTarget;
-            final SinglePackageBackupPreflight mPreflight;
-            final CountDownLatch mPreflightLatch;
-            final CountDownLatch mBackupLatch;
-            private final int mCurrentOpToken;
-            private final int mEphemeralToken;
-            private FullBackupEngine mEngine;
-            private volatile int mPreflightResult;
-            private volatile int mBackupResult;
-            private final long mQuota;
-            private volatile boolean mIsCancelled;
-
-            SinglePackageBackupRunner(ParcelFileDescriptor output, PackageInfo target,
-                    IBackupTransport transport, long quota, int currentOpToken) throws IOException {
-                mOutput = ParcelFileDescriptor.dup(output.getFileDescriptor());
-                mTarget = target;
-                mCurrentOpToken = currentOpToken;
-                mEphemeralToken = generateRandomIntegerToken();
-                mPreflight = new SinglePackageBackupPreflight(transport, quota, mEphemeralToken);
-                mPreflightLatch = new CountDownLatch(1);
-                mBackupLatch = new CountDownLatch(1);
-                mPreflightResult = BackupTransport.AGENT_ERROR;
-                mBackupResult = BackupTransport.AGENT_ERROR;
-                mQuota = quota;
-                registerTask();
-            }
-
-            void registerTask() {
-                synchronized (mCurrentOpLock) {
-                    mCurrentOperations.put(mCurrentOpToken, new Operation(OP_PENDING, this,
-                            OP_TYPE_BACKUP_WAIT));
-                }
-            }
-
-            void unregisterTask() {
-                synchronized (mCurrentOpLock) {
-                    mCurrentOperations.remove(mCurrentOpToken);
-                }
-            }
-
-            @Override
-            public void run() {
-                FileOutputStream out = new FileOutputStream(mOutput.getFileDescriptor());
-                mEngine = new FullBackupEngine(out, mPreflight, mTarget, false, this, mQuota, mCurrentOpToken);
-                try {
-                    try {
-                        if (!mIsCancelled) {
-                            mPreflightResult = mEngine.preflightCheck();
-                        }
-                    } finally {
-                        mPreflightLatch.countDown();
-                    }
-                    // If there is no error on preflight, continue backup.
-                    if (mPreflightResult == BackupTransport.TRANSPORT_OK) {
-                        if (!mIsCancelled) {
-                            mBackupResult = mEngine.backupOnePackage();
-                        }
-                    }
-                } catch (Exception e) {
-                    Slog.e(TAG, "Exception during full package backup of " + mTarget.packageName);
-                } finally {
-                    unregisterTask();
-                    mBackupLatch.countDown();
-                    try {
-                        mOutput.close();
-                    } catch (IOException e) {
-                        Slog.w(TAG, "Error closing transport pipe in runner");
-                    }
-                }
-            }
-
-            public void sendQuotaExceeded(final long backupDataBytes, final long quotaBytes) {
-                mEngine.sendQuotaExceeded(backupDataBytes, quotaBytes);
-            }
-
-            // If preflight succeeded, returns positive number - preflight size,
-            // otherwise return negative error code.
-            long getPreflightResultBlocking() {
-                try {
-                    mPreflightLatch.await(TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
-                    if (mIsCancelled) {
-                        return BackupManager.ERROR_BACKUP_CANCELLED;
-                    }
-                    if (mPreflightResult == BackupTransport.TRANSPORT_OK) {
-                        return mPreflight.getExpectedSizeOrErrorCode();
-                    } else {
-                        return mPreflightResult;
-                    }
-                } catch (InterruptedException e) {
-                    return BackupTransport.AGENT_ERROR;
-                }
-            }
-
-            int getBackupResultBlocking() {
-                try {
-                    mBackupLatch.await(TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
-                    if (mIsCancelled) {
-                        return BackupManager.ERROR_BACKUP_CANCELLED;
-                    }
-                    return mBackupResult;
-                } catch (InterruptedException e) {
-                    return BackupTransport.AGENT_ERROR;
-                }
-            }
-
-
-            // BackupRestoreTask interface: specifically, timeout detection
-
-            @Override
-            public void execute() { /* intentionally empty */ }
-
-            @Override
-            public void operationComplete(long result) { /* intentionally empty */ }
-
-            @Override
-            public void handleCancel(boolean cancelAll) {
-                if (DEBUG) {
-                    Slog.w(TAG, "Full backup cancel of " + mTarget.packageName);
-                }
-
-                mMonitor = monitorEvent(mMonitor,
-                        BackupManagerMonitor.LOG_EVENT_ID_FULL_BACKUP_CANCEL,
-                        mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
-                mIsCancelled = true;
-                // Cancel tasks spun off by this task.
-                RefactoredBackupManagerService.this.handleCancel(mEphemeralToken, cancelAll);
-                tearDownAgentAndKill(mTarget.applicationInfo);
-                // Free up everyone waiting on this task and its children.
-                mPreflightLatch.countDown();
-                mBackupLatch.countDown();
-                // We are done with this operation.
-                removeOperation(mCurrentOpToken);
-            }
-        }
-    }
-
     // ----- Full-data backup scheduling -----
 
     /**
      * Schedule a job to tell us when it's a good time to run a full backup
      */
-    private void scheduleNextFullBackupJob(long transportMinLatency) {
+    public void scheduleNextFullBackupJob(long transportMinLatency) {
         synchronized (mQueueLock) {
             if (mFullBackupQueue.size() > 0) {
                 // schedule the next job at the point in the future when the least-recently
@@ -5396,7 +2104,7 @@
     /**
      * Enqueue full backup for the given app, with a note about when it last ran.
      */
-    private void enqueueFullBackup(String packageName, long lastBackedUp) {
+    public void enqueueFullBackup(String packageName, long lastBackedUp) {
         FullBackupEntry newEntry = new FullBackupEntry(packageName, lastBackedUp);
         synchronized (mQueueLock) {
             // First, sanity check that we aren't adding a duplicate.  Slow but
@@ -5608,7 +2316,7 @@
             mFullBackupQueue.remove(0);
             CountDownLatch latch = new CountDownLatch(1);
             String[] pkg = new String[] {entry.packageName};
-            mRunningFullBackupTask = new PerformFullTransportBackupTask(null, pkg, true,
+            mRunningFullBackupTask = new PerformFullTransportBackupTask(this, null, pkg, true,
                     scheduledJob, latch, null, null, false /* userInitiated */);
             // Acquiring wakelock for PerformFullTransportBackupTask before its start.
             mWakelock.acquire();
@@ -5632,2769 +2340,10 @@
         }
     }
 
-    // ----- Restore infrastructure -----
-
-    abstract class RestoreEngine {
-        private static final String TAG = "RestoreEngine";
-
-        public static final int SUCCESS = 0;
-        public static final int TARGET_FAILURE = -2;
-        public static final int TRANSPORT_FAILURE = -3;
-
-        private AtomicBoolean mRunning = new AtomicBoolean(false);
-        private AtomicInteger mResult = new AtomicInteger(SUCCESS);
-
-        public boolean isRunning() {
-            return mRunning.get();
-        }
-
-        public void setRunning(boolean stillRunning) {
-            synchronized (mRunning) {
-                mRunning.set(stillRunning);
-                mRunning.notifyAll();
-            }
-        }
-
-        public int waitForResult() {
-            synchronized (mRunning) {
-                while (isRunning()) {
-                    try {
-                        mRunning.wait();
-                    } catch (InterruptedException e) {}
-                }
-            }
-            return getResult();
-        }
-
-        public int getResult() {
-            return mResult.get();
-        }
-
-        public void setResult(int result) {
-            mResult.set(result);
-        }
-
-        // TODO: abstract restore state and APIs
-    }
-
-    // ----- Full restore from a file/socket -----
-
-    enum RestorePolicy {
-        IGNORE,
-        ACCEPT,
-        ACCEPT_IF_APK
-    }
-
-    // Full restore engine, used by both adb restore and transport-based full restore
-    class FullRestoreEngine extends RestoreEngine {
-        // Task in charge of monitoring timeouts
-        BackupRestoreTask mMonitorTask;
-
-        // Dedicated observer, if any
-        IFullBackupRestoreObserver mObserver;
-
-        IBackupManagerMonitor mMonitor;
-
-        // Where we're delivering the file data as we go
-        IBackupAgent mAgent;
-
-        // Are we permitted to only deliver a specific package's metadata?
-        PackageInfo mOnlyPackage;
-
-        boolean mAllowApks;
-        boolean mAllowObbs;
-
-        // Which package are we currently handling data for?
-        String mAgentPackage;
-
-        // Info for working with the target app process
-        ApplicationInfo mTargetApp;
-
-        // Machinery for restoring OBBs
-        FullBackupObbConnection mObbConnection = null;
-
-        // possible handling states for a given package in the restore dataset
-        final HashMap<String, RestorePolicy> mPackagePolicies
-                = new HashMap<>();
-
-        // installer package names for each encountered app, derived from the manifests
-        final HashMap<String, String> mPackageInstallers = new HashMap<>();
-
-        // Signatures for a given package found in its manifest file
-        final HashMap<String, Signature[]> mManifestSignatures
-                = new HashMap<>();
-
-        // Packages we've already wiped data on when restoring their first file
-        final HashSet<String> mClearedPackages = new HashSet<>();
-
-        // How much data have we moved?
-        long mBytes;
-
-        // Working buffer
-        byte[] mBuffer;
-
-        // Pipes for moving data
-        ParcelFileDescriptor[] mPipes = null;
-
-        // Widget blob to be restored out-of-band
-        byte[] mWidgetData = null;
-
-        private final int mEphemeralOpToken;
-
-        // Runner that can be placed in a separate thread to do in-process
-        // invocations of the full restore API asynchronously. Used by adb restore.
-        class RestoreFileRunnable implements Runnable {
-            IBackupAgent mAgent;
-            FileMetadata mInfo;
-            ParcelFileDescriptor mSocket;
-            int mToken;
-
-            RestoreFileRunnable(IBackupAgent agent, FileMetadata info,
-                    ParcelFileDescriptor socket, int token) throws IOException {
-                mAgent = agent;
-                mInfo = info;
-                mToken = token;
-
-                // This class is used strictly for process-local binder invocations.  The
-                // semantics of ParcelFileDescriptor differ in this case; in particular, we
-                // do not automatically get a 'dup'ed descriptor that we can can continue
-                // to use asynchronously from the caller.  So, we make sure to dup it ourselves
-                // before proceeding to do the restore.
-                mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor());
-            }
-
-            @Override
-            public void run() {
-                try {
-                    mAgent.doRestoreFile(mSocket, mInfo.size, mInfo.type,
-                            mInfo.domain, mInfo.path, mInfo.mode, mInfo.mtime,
-                            mToken, mBackupManagerBinder);
-                } catch (RemoteException e) {
-                    // never happens; this is used strictly for local binder calls
-                }
-            }
-        }
-
-        public FullRestoreEngine(BackupRestoreTask monitorTask, IFullBackupRestoreObserver observer,
-                IBackupManagerMonitor monitor, PackageInfo onlyPackage, boolean allowApks,
-                boolean allowObbs, int ephemeralOpToken) {
-            mEphemeralOpToken = ephemeralOpToken;
-            mMonitorTask = monitorTask;
-            mObserver = observer;
-            mMonitor = monitor;
-            mOnlyPackage = onlyPackage;
-            mAllowApks = allowApks;
-            mAllowObbs = allowObbs;
-            mBuffer = new byte[32 * 1024];
-            mBytes = 0;
-        }
-
-        public IBackupAgent getAgent() {
-            return mAgent;
-        }
-
-        public byte[] getWidgetData() {
-            return mWidgetData;
-        }
-
-        public boolean restoreOneFile(InputStream instream, boolean mustKillAgent) {
-            if (!isRunning()) {
-                Slog.w(TAG, "Restore engine used after halting");
-                return false;
-            }
-
-            FileMetadata info;
-            try {
-                if (MORE_DEBUG) {
-                    Slog.v(TAG, "Reading tar header for restoring file");
-                }
-                info = readTarHeaders(instream);
-                if (info != null) {
-                    if (MORE_DEBUG) {
-                        dumpFileMetadata(info);
-                    }
-
-                    final String pkg = info.packageName;
-                    if (!pkg.equals(mAgentPackage)) {
-                        // In the single-package case, it's a semantic error to expect
-                        // one app's data but see a different app's on the wire
-                        if (mOnlyPackage != null) {
-                            if (!pkg.equals(mOnlyPackage.packageName)) {
-                                Slog.w(TAG, "Expected data for " + mOnlyPackage
-                                        + " but saw " + pkg);
-                                setResult(RestoreEngine.TRANSPORT_FAILURE);
-                                setRunning(false);
-                                return false;
-                            }
-                        }
-
-                        // okay, change in package; set up our various
-                        // bookkeeping if we haven't seen it yet
-                        if (!mPackagePolicies.containsKey(pkg)) {
-                            mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
-                        }
-
-                        // Clean up the previous agent relationship if necessary,
-                        // and let the observer know we're considering a new app.
-                        if (mAgent != null) {
-                            if (DEBUG) Slog.d(TAG, "Saw new package; finalizing old one");
-                            // Now we're really done
-                            tearDownPipes();
-                            tearDownAgent(mTargetApp);
-                            mTargetApp = null;
-                            mAgentPackage = null;
-                        }
-                    }
-
-                    if (info.path.equals(BACKUP_MANIFEST_FILENAME)) {
-                        mPackagePolicies.put(pkg, readAppManifest(info, instream));
-                        mPackageInstallers.put(pkg, info.installerPackageName);
-                        // We've read only the manifest content itself at this point,
-                        // so consume the footer before looping around to the next
-                        // input file
-                        skipTarPadding(info.size, instream);
-                        sendOnRestorePackage(pkg);
-                    } else if (info.path.equals(BACKUP_METADATA_FILENAME)) {
-                        // Metadata blobs!
-                        readMetadata(info, instream);
-                        skipTarPadding(info.size, instream);
-                    } else {
-                        // Non-manifest, so it's actual file data.  Is this a package
-                        // we're ignoring?
-                        boolean okay = true;
-                        RestorePolicy policy = mPackagePolicies.get(pkg);
-                        switch (policy) {
-                            case IGNORE:
-                                okay = false;
-                                break;
-
-                            case ACCEPT_IF_APK:
-                                // If we're in accept-if-apk state, then the first file we
-                                // see MUST be the apk.
-                                if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
-                                    if (DEBUG) Slog.d(TAG, "APK file; installing");
-                                    // Try to install the app.
-                                    String installerName = mPackageInstallers.get(pkg);
-                                    okay = installApk(info, installerName, instream);
-                                    // good to go; promote to ACCEPT
-                                    mPackagePolicies.put(pkg, (okay)
-                                            ? RestorePolicy.ACCEPT
-                                                    : RestorePolicy.IGNORE);
-                                    // At this point we've consumed this file entry
-                                    // ourselves, so just strip the tar footer and
-                                    // go on to the next file in the input stream
-                                    skipTarPadding(info.size, instream);
-                                    return true;
-                                } else {
-                                    // File data before (or without) the apk.  We can't
-                                    // handle it coherently in this case so ignore it.
-                                    mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
-                                    okay = false;
-                                }
-                                break;
-
-                            case ACCEPT:
-                                if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
-                                    if (DEBUG) Slog.d(TAG, "apk present but ACCEPT");
-                                    // we can take the data without the apk, so we
-                                    // *want* to do so.  skip the apk by declaring this
-                                    // one file not-okay without changing the restore
-                                    // policy for the package.
-                                    okay = false;
-                                }
-                                break;
-
-                            default:
-                                // Something has gone dreadfully wrong when determining
-                                // the restore policy from the manifest.  Ignore the
-                                // rest of this package's data.
-                                Slog.e(TAG, "Invalid policy from manifest");
-                                okay = false;
-                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
-                                break;
-                        }
-
-                        // Is it a *file* we need to drop?
-                        if (!isRestorableFile(info)) {
-                            okay = false;
-                        }
-
-                        // If the policy is satisfied, go ahead and set up to pipe the
-                        // data to the agent.
-                        if (MORE_DEBUG && okay && mAgent != null) {
-                            Slog.i(TAG, "Reusing existing agent instance");
-                        }
-                        if (okay && mAgent == null) {
-                            if (MORE_DEBUG) Slog.d(TAG, "Need to launch agent for " + pkg);
-
-                            try {
-                                mTargetApp = mPackageManager.getApplicationInfo(pkg, 0);
-
-                                // If we haven't sent any data to this app yet, we probably
-                                // need to clear it first.  Check that.
-                                if (!mClearedPackages.contains(pkg)) {
-                                    // apps with their own backup agents are
-                                    // responsible for coherently managing a full
-                                    // restore.
-                                    if (mTargetApp.backupAgentName == null) {
-                                        if (DEBUG) Slog.d(TAG, "Clearing app data preparatory to full restore");
-                                        clearApplicationDataSynchronous(pkg);
-                                    } else {
-                                        if (MORE_DEBUG) Slog.d(TAG, "backup agent ("
-                                                + mTargetApp.backupAgentName + ") => no clear");
-                                    }
-                                    mClearedPackages.add(pkg);
-                                } else {
-                                    if (MORE_DEBUG) {
-                                        Slog.d(TAG, "We've initialized this app already; no clear required");
-                                    }
-                                }
-
-                                // All set; now set up the IPC and launch the agent
-                                setUpPipes();
-                                mAgent = bindToAgentSynchronous(mTargetApp,
-                                        ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL);
-                                mAgentPackage = pkg;
-                            } catch (IOException e) {
-                                // fall through to error handling
-                            } catch (NameNotFoundException e) {
-                                // fall through to error handling
-                            }
-
-                            if (mAgent == null) {
-                                Slog.e(TAG, "Unable to create agent for " + pkg);
-                                okay = false;
-                                tearDownPipes();
-                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
-                            }
-                        }
-
-                        // Sanity check: make sure we never give data to the wrong app.  This
-                        // should never happen but a little paranoia here won't go amiss.
-                        if (okay && !pkg.equals(mAgentPackage)) {
-                            Slog.e(TAG, "Restoring data for " + pkg
-                                    + " but agent is for " + mAgentPackage);
-                            okay = false;
-                        }
-
-                        // At this point we have an agent ready to handle the full
-                        // restore data as well as a pipe for sending data to
-                        // that agent.  Tell the agent to start reading from the
-                        // pipe.
-                        if (okay) {
-                            boolean agentSuccess = true;
-                            long toCopy = info.size;
-                            try {
-                                prepareOperationTimeout(mEphemeralOpToken,
-                                        TIMEOUT_FULL_BACKUP_INTERVAL, mMonitorTask,
-                                        OP_TYPE_RESTORE_WAIT);
-
-                                if (info.domain.equals(FullBackup.OBB_TREE_TOKEN)) {
-                                    if (DEBUG) Slog.d(TAG, "Restoring OBB file for " + pkg
-                                            + " : " + info.path);
-                                    mObbConnection.restoreObbFile(pkg, mPipes[0],
-                                            info.size, info.type, info.path, info.mode,
-                                            info.mtime, mEphemeralOpToken, mBackupManagerBinder);
-                                } else {
-                                    if (MORE_DEBUG) Slog.d(TAG, "Invoking agent to restore file "
-                                            + info.path);
-                                    // fire up the app's agent listening on the socket.  If
-                                    // the agent is running in the system process we can't
-                                    // just invoke it asynchronously, so we provide a thread
-                                    // for it here.
-                                    if (mTargetApp.processName.equals("system")) {
-                                        Slog.d(TAG, "system process agent - spinning a thread");
-                                        RestoreFileRunnable runner = new RestoreFileRunnable(
-                                                mAgent, info, mPipes[0], mEphemeralOpToken);
-                                        new Thread(runner, "restore-sys-runner").start();
-                                    } else {
-                                        mAgent.doRestoreFile(mPipes[0], info.size, info.type,
-                                                info.domain, info.path, info.mode, info.mtime,
-                                                mEphemeralOpToken, mBackupManagerBinder);
-                                    }
-                                }
-                            } catch (IOException e) {
-                                // couldn't dup the socket for a process-local restore
-                                Slog.d(TAG, "Couldn't establish restore");
-                                agentSuccess = false;
-                                okay = false;
-                            } catch (RemoteException e) {
-                                // whoops, remote entity went away.  We'll eat the content
-                                // ourselves, then, and not copy it over.
-                                Slog.e(TAG, "Agent crashed during full restore");
-                                agentSuccess = false;
-                                okay = false;
-                            }
-
-                            // Copy over the data if the agent is still good
-                            if (okay) {
-                                if (MORE_DEBUG) {
-                                    Slog.v(TAG, "  copying to restore agent: "
-                                            + toCopy + " bytes");
-                                }
-                                boolean pipeOkay = true;
-                                FileOutputStream pipe = new FileOutputStream(
-                                        mPipes[1].getFileDescriptor());
-                                while (toCopy > 0) {
-                                    int toRead = (toCopy > mBuffer.length)
-                                            ? mBuffer.length : (int)toCopy;
-                                    int nRead = instream.read(mBuffer, 0, toRead);
-                                    if (nRead >= 0) mBytes += nRead;
-                                    if (nRead <= 0) break;
-                                    toCopy -= nRead;
-
-                                    // send it to the output pipe as long as things
-                                    // are still good
-                                    if (pipeOkay) {
-                                        try {
-                                            pipe.write(mBuffer, 0, nRead);
-                                        } catch (IOException e) {
-                                            Slog.e(TAG, "Failed to write to restore pipe: "
-                                                    + e.getMessage());
-                                            pipeOkay = false;
-                                        }
-                                    }
-                                }
-
-                                // done sending that file!  Now we just need to consume
-                                // the delta from info.size to the end of block.
-                                skipTarPadding(info.size, instream);
-
-                                // and now that we've sent it all, wait for the remote
-                                // side to acknowledge receipt
-                                agentSuccess = waitUntilOperationComplete(mEphemeralOpToken);
-                            }
-
-                            // okay, if the remote end failed at any point, deal with
-                            // it by ignoring the rest of the restore on it
-                            if (!agentSuccess) {
-                                Slog.w(TAG, "Agent failure; ending restore");
-                                mBackupHandler.removeMessages(MSG_RESTORE_OPERATION_TIMEOUT);
-                                tearDownPipes();
-                                tearDownAgent(mTargetApp);
-                                mAgent = null;
-                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
-
-                                // If this was a single-package restore, we halt immediately
-                                // with an agent error under these circumstances
-                                if (mOnlyPackage != null) {
-                                    setResult(RestoreEngine.TARGET_FAILURE);
-                                    setRunning(false);
-                                    return false;
-                                }
-                            }
-                        }
-
-                        // Problems setting up the agent communication, an explicitly
-                        // dropped file, or an already-ignored package: skip to the
-                        // next stream entry by reading and discarding this file.
-                        if (!okay) {
-                            if (MORE_DEBUG) Slog.d(TAG, "[discarding file content]");
-                            long bytesToConsume = (info.size + 511) & ~511;
-                            while (bytesToConsume > 0) {
-                                int toRead = (bytesToConsume > mBuffer.length)
-                                        ? mBuffer.length : (int)bytesToConsume;
-                                long nRead = instream.read(mBuffer, 0, toRead);
-                                if (nRead >= 0) mBytes += nRead;
-                                if (nRead <= 0) break;
-                                bytesToConsume -= nRead;
-                            }
-                        }
-                    }
-                }
-            } catch (IOException e) {
-                if (DEBUG) Slog.w(TAG, "io exception on restore socket read: " + e.getMessage());
-                setResult(RestoreEngine.TRANSPORT_FAILURE);
-                info = null;
-            }
-
-            // If we got here we're either running smoothly or we've finished
-            if (info == null) {
-                if (MORE_DEBUG) {
-                    Slog.i(TAG, "No [more] data for this package; tearing down");
-                }
-                tearDownPipes();
-                setRunning(false);
-                if (mustKillAgent) {
-                    tearDownAgent(mTargetApp);
-                }
-            }
-            return (info != null);
-        }
-
-        void setUpPipes() throws IOException {
-            mPipes = ParcelFileDescriptor.createPipe();
-        }
-
-        void tearDownPipes() {
-            // Teardown might arise from the inline restore processing or from the asynchronous
-            // timeout mechanism, and these might race.  Make sure we don't try to close and
-            // null out the pipes twice.
-            synchronized (this) {
-                if (mPipes != null) {
-                    try {
-                        mPipes[0].close();
-                        mPipes[0] = null;
-                        mPipes[1].close();
-                        mPipes[1] = null;
-                    } catch (IOException e) {
-                        Slog.w(TAG, "Couldn't close agent pipes", e);
-                    }
-                    mPipes = null;
-                }
-            }
-        }
-
-        void tearDownAgent(ApplicationInfo app) {
-            if (mAgent != null) {
-                tearDownAgentAndKill(app);
-                mAgent = null;
-            }
-        }
-
-        void handleTimeout() {
-            tearDownPipes();
-            setResult(RestoreEngine.TARGET_FAILURE);
-            setRunning(false);
-        }
-
-        class RestoreInstallObserver extends PackageInstallObserver {
-            final AtomicBoolean mDone = new AtomicBoolean();
-            String mPackageName;
-            int mResult;
-
-            public void reset() {
-                synchronized (mDone) {
-                    mDone.set(false);
-                }
-            }
-
-            public void waitForCompletion() {
-                synchronized (mDone) {
-                    while (mDone.get() == false) {
-                        try {
-                            mDone.wait();
-                        } catch (InterruptedException e) { }
-                    }
-                }
-            }
-
-            int getResult() {
-                return mResult;
-            }
-
-            @Override
-            public void onPackageInstalled(String packageName, int returnCode,
-                    String msg, Bundle extras) {
-                synchronized (mDone) {
-                    mResult = returnCode;
-                    mPackageName = packageName;
-                    mDone.set(true);
-                    mDone.notifyAll();
-                }
-            }
-        }
-
-        class RestoreDeleteObserver extends IPackageDeleteObserver.Stub {
-            final AtomicBoolean mDone = new AtomicBoolean();
-            int mResult;
-
-            public void reset() {
-                synchronized (mDone) {
-                    mDone.set(false);
-                }
-            }
-
-            public void waitForCompletion() {
-                synchronized (mDone) {
-                    while (mDone.get() == false) {
-                        try {
-                            mDone.wait();
-                        } catch (InterruptedException e) { }
-                    }
-                }
-            }
-
-            @Override
-            public void packageDeleted(String packageName, int returnCode) throws RemoteException {
-                synchronized (mDone) {
-                    mResult = returnCode;
-                    mDone.set(true);
-                    mDone.notifyAll();
-                }
-            }
-        }
-
-        final RestoreInstallObserver mInstallObserver = new RestoreInstallObserver();
-        final RestoreDeleteObserver mDeleteObserver = new RestoreDeleteObserver();
-
-        boolean installApk(FileMetadata info, String installerPackage, InputStream instream) {
-            boolean okay = true;
-
-            if (DEBUG) Slog.d(TAG, "Installing from backup: " + info.packageName);
-
-            // The file content is an .apk file.  Copy it out to a staging location and
-            // attempt to install it.
-            File apkFile = new File(mDataDir, info.packageName);
-            try {
-                FileOutputStream apkStream = new FileOutputStream(apkFile);
-                byte[] buffer = new byte[32 * 1024];
-                long size = info.size;
-                while (size > 0) {
-                    long toRead = (buffer.length < size) ? buffer.length : size;
-                    int didRead = instream.read(buffer, 0, (int)toRead);
-                    if (didRead >= 0) mBytes += didRead;
-                    apkStream.write(buffer, 0, didRead);
-                    size -= didRead;
-                }
-                apkStream.close();
-
-                // make sure the installer can read it
-                apkFile.setReadable(true, false);
-
-                // Now install it
-                Uri packageUri = Uri.fromFile(apkFile);
-                mInstallObserver.reset();
-                mPackageManager.installPackage(packageUri, mInstallObserver,
-                        PackageManager.INSTALL_REPLACE_EXISTING | PackageManager.INSTALL_FROM_ADB,
-                        installerPackage);
-                mInstallObserver.waitForCompletion();
-
-                if (mInstallObserver.getResult() != PackageManager.INSTALL_SUCCEEDED) {
-                    // The only time we continue to accept install of data even if the
-                    // apk install failed is if we had already determined that we could
-                    // accept the data regardless.
-                    if (mPackagePolicies.get(info.packageName) != RestorePolicy.ACCEPT) {
-                        okay = false;
-                    }
-                } else {
-                    // Okay, the install succeeded.  Make sure it was the right app.
-                    boolean uninstall = false;
-                    if (!mInstallObserver.mPackageName.equals(info.packageName)) {
-                        Slog.w(TAG, "Restore stream claimed to include apk for "
-                                + info.packageName + " but apk was really "
-                                + mInstallObserver.mPackageName);
-                        // delete the package we just put in place; it might be fraudulent
-                        okay = false;
-                        uninstall = true;
-                    } else {
-                        try {
-                            PackageInfo pkg = mPackageManager.getPackageInfo(info.packageName,
-                                    PackageManager.GET_SIGNATURES);
-                            if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) {
-                                Slog.w(TAG, "Restore stream contains apk of package "
-                                        + info.packageName + " but it disallows backup/restore");
-                                okay = false;
-                            } else {
-                                // So far so good -- do the signatures match the manifest?
-                                Signature[] sigs = mManifestSignatures.get(info.packageName);
-                                if (signaturesMatch(sigs, pkg)) {
-                                    // If this is a system-uid app without a declared backup agent,
-                                    // don't restore any of the file data.
-                                    if ((pkg.applicationInfo.uid < Process.FIRST_APPLICATION_UID)
-                                            && (pkg.applicationInfo.backupAgentName == null)) {
-                                        Slog.w(TAG, "Installed app " + info.packageName
-                                                + " has restricted uid and no agent");
-                                        okay = false;
-                                    }
-                                } else {
-                                    Slog.w(TAG, "Installed app " + info.packageName
-                                            + " signatures do not match restore manifest");
-                                    okay = false;
-                                    uninstall = true;
-                                }
-                            }
-                        } catch (NameNotFoundException e) {
-                            Slog.w(TAG, "Install of package " + info.packageName
-                                    + " succeeded but now not found");
-                            okay = false;
-                        }
-                    }
-
-                    // If we're not okay at this point, we need to delete the package
-                    // that we just installed.
-                    if (uninstall) {
-                        mDeleteObserver.reset();
-                        mPackageManager.deletePackage(mInstallObserver.mPackageName,
-                                mDeleteObserver, 0);
-                        mDeleteObserver.waitForCompletion();
-                    }
-                }
-            } catch (IOException e) {
-                Slog.e(TAG, "Unable to transcribe restored apk for install");
-                okay = false;
-            } finally {
-                apkFile.delete();
-            }
-
-            return okay;
-        }
-
-        // Given an actual file content size, consume the post-content padding mandated
-        // by the tar format.
-        void skipTarPadding(long size, InputStream instream) throws IOException {
-            long partial = (size + 512) % 512;
-            if (partial > 0) {
-                final int needed = 512 - (int)partial;
-                if (MORE_DEBUG) {
-                    Slog.i(TAG, "Skipping tar padding: " + needed + " bytes");
-                }
-                byte[] buffer = new byte[needed];
-                if (readExactly(instream, buffer, 0, needed) == needed) {
-                    mBytes += needed;
-                } else throw new IOException("Unexpected EOF in padding");
-            }
-        }
-
-        // Read a widget metadata file, returning the restored blob
-        void readMetadata(FileMetadata info, InputStream instream) throws IOException {
-            // Fail on suspiciously large widget dump files
-            if (info.size > 64 * 1024) {
-                throw new IOException("Metadata too big; corrupt? size=" + info.size);
-            }
-
-            byte[] buffer = new byte[(int) info.size];
-            if (readExactly(instream, buffer, 0, (int)info.size) == info.size) {
-                mBytes += info.size;
-            } else throw new IOException("Unexpected EOF in widget data");
-
-            String[] str = new String[1];
-            int offset = extractLine(buffer, 0, str);
-            int version = Integer.parseInt(str[0]);
-            if (version == BACKUP_MANIFEST_VERSION) {
-                offset = extractLine(buffer, offset, str);
-                final String pkg = str[0];
-                if (info.packageName.equals(pkg)) {
-                    // Data checks out -- the rest of the buffer is a concatenation of
-                    // binary blobs as described in the comment at writeAppWidgetData()
-                    ByteArrayInputStream bin = new ByteArrayInputStream(buffer,
-                            offset, buffer.length - offset);
-                    DataInputStream in = new DataInputStream(bin);
-                    while (bin.available() > 0) {
-                        int token = in.readInt();
-                        int size = in.readInt();
-                        if (size > 64 * 1024) {
-                            throw new IOException("Datum "
-                                    + Integer.toHexString(token)
-                                    + " too big; corrupt? size=" + info.size);
-                        }
-                        switch (token) {
-                            case BACKUP_WIDGET_METADATA_TOKEN:
-                            {
-                                if (MORE_DEBUG) {
-                                    Slog.i(TAG, "Got widget metadata for " + info.packageName);
-                                }
-                                mWidgetData = new byte[size];
-                                in.read(mWidgetData);
-                                break;
-                            }
-                            default:
-                            {
-                                if (DEBUG) {
-                                    Slog.i(TAG, "Ignoring metadata blob "
-                                            + Integer.toHexString(token)
-                                            + " for " + info.packageName);
-                                }
-                                in.skipBytes(size);
-                                break;
-                            }
-                        }
-                    }
-                } else {
-                    Slog.w(TAG, "Metadata mismatch: package " + info.packageName
-                            + " but widget data for " + pkg);
-
-                    Bundle monitoringExtras = putMonitoringExtra(null,
-                            EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
-                    monitoringExtras = putMonitoringExtra(monitoringExtras,
-                            BackupManagerMonitor.EXTRA_LOG_WIDGET_PACKAGE_NAME, pkg);
-                    mMonitor = monitorEvent(mMonitor,
-                            BackupManagerMonitor.LOG_EVENT_ID_WIDGET_METADATA_MISMATCH,
-                            null,
-                            LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                            monitoringExtras);
-                }
-            } else {
-                Slog.w(TAG, "Unsupported metadata version " + version);
-
-                Bundle monitoringExtras = putMonitoringExtra(null, EXTRA_LOG_EVENT_PACKAGE_NAME,
-                        info.packageName);
-                monitoringExtras = putMonitoringExtra(monitoringExtras,
-                        EXTRA_LOG_EVENT_PACKAGE_VERSION, version);
-                mMonitor = monitorEvent(mMonitor,
-                        BackupManagerMonitor.LOG_EVENT_ID_WIDGET_UNKNOWN_VERSION,
-                        null,
-                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                        monitoringExtras);
-            }
-        }
-
-        // Returns a policy constant
-        RestorePolicy readAppManifest(FileMetadata info, InputStream instream)
-                throws IOException {
-            // Fail on suspiciously large manifest files
-            if (info.size > 64 * 1024) {
-                throw new IOException("Restore manifest too big; corrupt? size=" + info.size);
-            }
-
-            byte[] buffer = new byte[(int) info.size];
-            if (MORE_DEBUG) {
-                Slog.i(TAG, "   readAppManifest() looking for " + info.size + " bytes, "
-                        + mBytes + " already consumed");
-            }
-            if (readExactly(instream, buffer, 0, (int)info.size) == info.size) {
-                mBytes += info.size;
-            } else throw new IOException("Unexpected EOF in manifest");
-
-            RestorePolicy policy = RestorePolicy.IGNORE;
-            String[] str = new String[1];
-            int offset = 0;
-
-            try {
-                offset = extractLine(buffer, offset, str);
-                int version = Integer.parseInt(str[0]);
-                if (version == BACKUP_MANIFEST_VERSION) {
-                    offset = extractLine(buffer, offset, str);
-                    String manifestPackage = str[0];
-                    // TODO: handle <original-package>
-                    if (manifestPackage.equals(info.packageName)) {
-                        offset = extractLine(buffer, offset, str);
-                        version = Integer.parseInt(str[0]);  // app version
-                        offset = extractLine(buffer, offset, str);
-                        // This is the platform version, which we don't use, but we parse it
-                        // as a safety against corruption in the manifest.
-                        Integer.parseInt(str[0]);
-                        offset = extractLine(buffer, offset, str);
-                        info.installerPackageName = (str[0].length() > 0) ? str[0] : null;
-                        offset = extractLine(buffer, offset, str);
-                        boolean hasApk = str[0].equals("1");
-                        offset = extractLine(buffer, offset, str);
-                        int numSigs = Integer.parseInt(str[0]);
-                        if (numSigs > 0) {
-                            Signature[] sigs = new Signature[numSigs];
-                            for (int i = 0; i < numSigs; i++) {
-                                offset = extractLine(buffer, offset, str);
-                                sigs[i] = new Signature(str[0]);
-                            }
-                            mManifestSignatures.put(info.packageName, sigs);
-
-                            // Okay, got the manifest info we need...
-                            try {
-                                PackageInfo pkgInfo = mPackageManager.getPackageInfo(
-                                        info.packageName, PackageManager.GET_SIGNATURES);
-                                // Fall through to IGNORE if the app explicitly disallows backup
-                                final int flags = pkgInfo.applicationInfo.flags;
-                                if ((flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) {
-                                    // Restore system-uid-space packages only if they have
-                                    // defined a custom backup agent
-                                    if ((pkgInfo.applicationInfo.uid >= Process.FIRST_APPLICATION_UID)
-                                            || (pkgInfo.applicationInfo.backupAgentName != null)) {
-                                        // Verify signatures against any installed version; if they
-                                        // don't match, then we fall though and ignore the data.  The
-                                        // signatureMatch() method explicitly ignores the signature
-                                        // check for packages installed on the system partition, because
-                                        // such packages are signed with the platform cert instead of
-                                        // the app developer's cert, so they're different on every
-                                        // device.
-                                        if (signaturesMatch(sigs, pkgInfo)) {
-                                            if ((pkgInfo.applicationInfo.flags
-                                                    & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) != 0) {
-                                                Slog.i(TAG, "Package has restoreAnyVersion; taking data");
-                                                mMonitor = monitorEvent(mMonitor,
-                                                        LOG_EVENT_ID_RESTORE_ANY_VERSION,
-                                                        pkgInfo,
-                                                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                                        null);
-                                                policy = RestorePolicy.ACCEPT;
-                                            } else if (pkgInfo.versionCode >= version) {
-                                                Slog.i(TAG, "Sig + version match; taking data");
-                                                policy = RestorePolicy.ACCEPT;
-                                                mMonitor = monitorEvent(mMonitor,
-                                                        LOG_EVENT_ID_VERSIONS_MATCH,
-                                                        pkgInfo,
-                                                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                                        null);
-                                            } else {
-                                                // The data is from a newer version of the app than
-                                                // is presently installed.  That means we can only
-                                                // use it if the matching apk is also supplied.
-                                                if (mAllowApks) {
-                                                    Slog.i(TAG, "Data version " + version
-                                                            + " is newer than installed version "
-                                                            + pkgInfo.versionCode
-                                                            + " - requiring apk");
-                                                    policy = RestorePolicy.ACCEPT_IF_APK;
-                                                } else {
-                                                    Slog.i(TAG, "Data requires newer version "
-                                                            + version + "; ignoring");
-                                                    mMonitor = monitorEvent(mMonitor,
-                                                            LOG_EVENT_ID_VERSION_OF_BACKUP_OLDER,
-                                                            pkgInfo,
-                                                            LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                                            putMonitoringExtra(null,
-                                                                    EXTRA_LOG_OLD_VERSION,
-                                                                    version));
-
-                                                    policy = RestorePolicy.IGNORE;
-                                                }
-                                            }
-                                        } else {
-                                            Slog.w(TAG, "Restore manifest signatures do not match "
-                                                    + "installed application for " + info.packageName);
-                                            mMonitor = monitorEvent(mMonitor,
-                                                    LOG_EVENT_ID_FULL_RESTORE_SIGNATURE_MISMATCH,
-                                                    pkgInfo,
-                                                    LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                                    null);
-                                        }
-                                    } else {
-                                        Slog.w(TAG, "Package " + info.packageName
-                                                + " is system level with no agent");
-                                        mMonitor = monitorEvent(mMonitor,
-                                                LOG_EVENT_ID_SYSTEM_APP_NO_AGENT,
-                                                pkgInfo,
-                                                LOG_EVENT_CATEGORY_AGENT,
-                                                null);
-                                    }
-                                } else {
-                                    if (DEBUG) Slog.i(TAG, "Restore manifest from "
-                                            + info.packageName + " but allowBackup=false");
-                                    mMonitor = monitorEvent(mMonitor,
-                                            LOG_EVENT_ID_FULL_RESTORE_ALLOW_BACKUP_FALSE,
-                                            pkgInfo,
-                                            LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                            null);
-                                }
-                            } catch (NameNotFoundException e) {
-                                // Okay, the target app isn't installed.  We can process
-                                // the restore properly only if the dataset provides the
-                                // apk file and we can successfully install it.
-                                if (mAllowApks) {
-                                    if (DEBUG) Slog.i(TAG, "Package " + info.packageName
-                                            + " not installed; requiring apk in dataset");
-                                    policy = RestorePolicy.ACCEPT_IF_APK;
-                                } else {
-                                    policy = RestorePolicy.IGNORE;
-                                }
-                                Bundle monitoringExtras = putMonitoringExtra(null,
-                                        EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
-                                monitoringExtras = putMonitoringExtra(monitoringExtras,
-                                        EXTRA_LOG_POLICY_ALLOW_APKS, mAllowApks);
-                                mMonitor = monitorEvent(mMonitor,
-                                        LOG_EVENT_ID_APK_NOT_INSTALLED,
-                                        null,
-                                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                        monitoringExtras);
-                            }
-
-                            if (policy == RestorePolicy.ACCEPT_IF_APK && !hasApk) {
-                                Slog.i(TAG, "Cannot restore package " + info.packageName
-                                        + " without the matching .apk");
-                                mMonitor = monitorEvent(mMonitor,
-                                        LOG_EVENT_ID_CANNOT_RESTORE_WITHOUT_APK,
-                                        null,
-                                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                        putMonitoringExtra(null,
-                                                EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName));
-                            }
-                        } else {
-                            Slog.i(TAG, "Missing signature on backed-up package "
-                                    + info.packageName);
-                            mMonitor = monitorEvent(mMonitor,
-                                    LOG_EVENT_ID_MISSING_SIGNATURE,
-                                    null,
-                                    LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                    putMonitoringExtra(null,
-                                            EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName));
-                        }
-                    } else {
-                        Slog.i(TAG, "Expected package " + info.packageName
-                                + " but restore manifest claims " + manifestPackage);
-                        Bundle monitoringExtras = putMonitoringExtra(null,
-                                EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
-                        monitoringExtras = putMonitoringExtra(monitoringExtras,
-                                EXTRA_LOG_MANIFEST_PACKAGE_NAME, manifestPackage);
-                        mMonitor = monitorEvent(mMonitor,
-                                LOG_EVENT_ID_EXPECTED_DIFFERENT_PACKAGE,
-                                null,
-                                LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                monitoringExtras);
-                    }
-                } else {
-                    Slog.i(TAG, "Unknown restore manifest version " + version
-                            + " for package " + info.packageName);
-                    Bundle monitoringExtras = putMonitoringExtra(null,
-                            EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
-                    monitoringExtras = putMonitoringExtra(monitoringExtras,
-                            EXTRA_LOG_EVENT_PACKAGE_VERSION, version);
-                    mMonitor = monitorEvent(mMonitor,
-                            BackupManagerMonitor.LOG_EVENT_ID_UNKNOWN_VERSION,
-                            null,
-                            LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                            monitoringExtras);
-
-                }
-            } catch (NumberFormatException e) {
-                Slog.w(TAG, "Corrupt restore manifest for package " + info.packageName);
-                mMonitor = monitorEvent(mMonitor,
-                        BackupManagerMonitor.LOG_EVENT_ID_CORRUPT_MANIFEST,
-                        null,
-                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                        putMonitoringExtra(null, EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName));
-            } catch (IllegalArgumentException e) {
-                Slog.w(TAG, e.getMessage());
-            }
-
-            return policy;
-        }
-
-        // Builds a line from a byte buffer starting at 'offset', and returns
-        // the index of the next unconsumed data in the buffer.
-        int extractLine(byte[] buffer, int offset, String[] outStr) throws IOException {
-            final int end = buffer.length;
-            if (offset >= end) throw new IOException("Incomplete data");
-
-            int pos;
-            for (pos = offset; pos < end; pos++) {
-                byte c = buffer[pos];
-                // at LF we declare end of line, and return the next char as the
-                // starting point for the next time through
-                if (c == '\n') {
-                    break;
-                }
-            }
-            outStr[0] = new String(buffer, offset, pos - offset);
-            pos++;  // may be pointing an extra byte past the end but that's okay
-            return pos;
-        }
-
-        void dumpFileMetadata(FileMetadata info) {
-            if (MORE_DEBUG) {
-                StringBuilder b = new StringBuilder(128);
-
-                // mode string
-                b.append((info.type == BackupAgent.TYPE_DIRECTORY) ? 'd' : '-');
-                b.append(((info.mode & 0400) != 0) ? 'r' : '-');
-                b.append(((info.mode & 0200) != 0) ? 'w' : '-');
-                b.append(((info.mode & 0100) != 0) ? 'x' : '-');
-                b.append(((info.mode & 0040) != 0) ? 'r' : '-');
-                b.append(((info.mode & 0020) != 0) ? 'w' : '-');
-                b.append(((info.mode & 0010) != 0) ? 'x' : '-');
-                b.append(((info.mode & 0004) != 0) ? 'r' : '-');
-                b.append(((info.mode & 0002) != 0) ? 'w' : '-');
-                b.append(((info.mode & 0001) != 0) ? 'x' : '-');
-                b.append(String.format(" %9d ", info.size));
-
-                Date stamp = new Date(info.mtime);
-                b.append(new SimpleDateFormat("MMM dd HH:mm:ss ").format(stamp));
-
-                b.append(info.packageName);
-                b.append(" :: ");
-                b.append(info.domain);
-                b.append(" :: ");
-                b.append(info.path);
-
-                Slog.i(TAG, b.toString());
-            }
-        }
-
-        // Consume a tar file header block [sequence] and accumulate the relevant metadata
-        FileMetadata readTarHeaders(InputStream instream) throws IOException {
-            byte[] block = new byte[512];
-            FileMetadata info = null;
-
-            boolean gotHeader = readTarHeader(instream, block);
-            if (gotHeader) {
-                try {
-                    // okay, presume we're okay, and extract the various metadata
-                    info = new FileMetadata();
-                    info.size = extractRadix(block, TAR_HEADER_OFFSET_FILESIZE,
-                            TAR_HEADER_LENGTH_FILESIZE, TAR_HEADER_LONG_RADIX);
-                    info.mtime = extractRadix(block, TAR_HEADER_OFFSET_MODTIME,
-                            TAR_HEADER_LENGTH_MODTIME, TAR_HEADER_LONG_RADIX);
-                    info.mode = extractRadix(block, TAR_HEADER_OFFSET_MODE,
-                            TAR_HEADER_LENGTH_MODE, TAR_HEADER_LONG_RADIX);
-
-                    info.path = extractString(block, TAR_HEADER_OFFSET_PATH_PREFIX,
-                            TAR_HEADER_LENGTH_PATH_PREFIX);
-                    String path = extractString(block, TAR_HEADER_OFFSET_PATH,
-                            TAR_HEADER_LENGTH_PATH);
-                    if (path.length() > 0) {
-                        if (info.path.length() > 0) info.path += '/';
-                        info.path += path;
-                    }
-
-                    // tar link indicator field: 1 byte at offset 156 in the header.
-                    int typeChar = block[TAR_HEADER_OFFSET_TYPE_CHAR];
-                    if (typeChar == 'x') {
-                        // pax extended header, so we need to read that
-                        gotHeader = readPaxExtendedHeader(instream, info);
-                        if (gotHeader) {
-                            // and after a pax extended header comes another real header -- read
-                            // that to find the real file type
-                            gotHeader = readTarHeader(instream, block);
-                        }
-                        if (!gotHeader) throw new IOException("Bad or missing pax header");
-
-                        typeChar = block[TAR_HEADER_OFFSET_TYPE_CHAR];
-                    }
-
-                    switch (typeChar) {
-                        case '0': info.type = BackupAgent.TYPE_FILE; break;
-                        case '5': {
-                            info.type = BackupAgent.TYPE_DIRECTORY;
-                            if (info.size != 0) {
-                                Slog.w(TAG, "Directory entry with nonzero size in header");
-                                info.size = 0;
-                            }
-                            break;
-                        }
-                        case 0: {
-                            // presume EOF
-                            if (MORE_DEBUG) Slog.w(TAG, "Saw type=0 in tar header block, info=" + info);
-                            return null;
-                        }
-                        default: {
-                            Slog.e(TAG, "Unknown tar entity type: " + typeChar);
-                            throw new IOException("Unknown entity type " + typeChar);
-                        }
-                    }
-
-                    // Parse out the path
-                    //
-                    // first: apps/shared/unrecognized
-                    if (FullBackup.SHARED_PREFIX.regionMatches(0,
-                            info.path, 0, FullBackup.SHARED_PREFIX.length())) {
-                        // File in shared storage.  !!! TODO: implement this.
-                        info.path = info.path.substring(FullBackup.SHARED_PREFIX.length());
-                        info.packageName = SHARED_BACKUP_AGENT_PACKAGE;
-                        info.domain = FullBackup.SHARED_STORAGE_TOKEN;
-                        if (DEBUG) Slog.i(TAG, "File in shared storage: " + info.path);
-                    } else if (FullBackup.APPS_PREFIX.regionMatches(0,
-                            info.path, 0, FullBackup.APPS_PREFIX.length())) {
-                        // App content!  Parse out the package name and domain
-
-                        // strip the apps/ prefix
-                        info.path = info.path.substring(FullBackup.APPS_PREFIX.length());
-
-                        // extract the package name
-                        int slash = info.path.indexOf('/');
-                        if (slash < 0) throw new IOException("Illegal semantic path in " + info.path);
-                        info.packageName = info.path.substring(0, slash);
-                        info.path = info.path.substring(slash+1);
-
-                        // if it's a manifest or metadata payload we're done, otherwise parse
-                        // out the domain into which the file will be restored
-                        if (!info.path.equals(BACKUP_MANIFEST_FILENAME)
-                                && !info.path.equals(BACKUP_METADATA_FILENAME)) {
-                            slash = info.path.indexOf('/');
-                            if (slash < 0) {
-                                throw new IOException("Illegal semantic path in non-manifest "
-                                        + info.path);
-                            }
-                            info.domain = info.path.substring(0, slash);
-                            info.path = info.path.substring(slash + 1);
-                        }
-                    }
-                } catch (IOException e) {
-                    if (DEBUG) {
-                        Slog.e(TAG, "Parse error in header: " + e.getMessage());
-                        if (MORE_DEBUG) {
-                            HEXLOG(block);
-                        }
-                    }
-                    throw e;
-                }
-            }
-            return info;
-        }
-
-        private boolean isRestorableFile(FileMetadata info) {
-            if (FullBackup.CACHE_TREE_TOKEN.equals(info.domain)) {
-                if (MORE_DEBUG) {
-                    Slog.i(TAG, "Dropping cache file path " + info.path);
-                }
-                return false;
-            }
-
-            if (FullBackup.ROOT_TREE_TOKEN.equals(info.domain)) {
-                // It's possible this is "no-backup" dir contents in an archive stream
-                // produced on a device running a version of the OS that predates that
-                // API.  Respect the no-backup intention and don't let the data get to
-                // the app.
-                if (info.path.startsWith("no_backup/")) {
-                    if (MORE_DEBUG) {
-                        Slog.i(TAG, "Dropping no_backup file path " + info.path);
-                    }
-                    return false;
-                }
-            }
-
-            // The path needs to be canonical
-            if (info.path.contains("..") || info.path.contains("//")) {
-                if (MORE_DEBUG) {
-                    Slog.w(TAG, "Dropping invalid path " + info.path);
-                }
-                return false;
-            }
-
-            // Otherwise we think this file is good to go
-            return true;
-        }
-
-        private void HEXLOG(byte[] block) {
-            int offset = 0;
-            int todo = block.length;
-            StringBuilder buf = new StringBuilder(64);
-            while (todo > 0) {
-                buf.append(String.format("%04x   ", offset));
-                int numThisLine = (todo > 16) ? 16 : todo;
-                for (int i = 0; i < numThisLine; i++) {
-                    buf.append(String.format("%02x ", block[offset+i]));
-                }
-                Slog.i("hexdump", buf.toString());
-                buf.setLength(0);
-                todo -= numThisLine;
-                offset += numThisLine;
-            }
-        }
-
-        // Read exactly the given number of bytes into a buffer at the stated offset.
-        // Returns false if EOF is encountered before the requested number of bytes
-        // could be read.
-        int readExactly(InputStream in, byte[] buffer, int offset, int size)
-                throws IOException {
-            if (size <= 0) throw new IllegalArgumentException("size must be > 0");
-if (MORE_DEBUG) Slog.i(TAG, "  ... readExactly(" + size + ") called");
-            int soFar = 0;
-            while (soFar < size) {
-                int nRead = in.read(buffer, offset + soFar, size - soFar);
-                if (nRead <= 0) {
-                    if (MORE_DEBUG) Slog.w(TAG, "- wanted exactly " + size + " but got only " + soFar);
-                    break;
-                }
-                soFar += nRead;
-if (MORE_DEBUG) Slog.v(TAG, "   + got " + nRead + "; now wanting " + (size - soFar));
-            }
-            return soFar;
-        }
-
-        boolean readTarHeader(InputStream instream, byte[] block) throws IOException {
-            final int got = readExactly(instream, block, 0, 512);
-            if (got == 0) return false;     // Clean EOF
-            if (got < 512) throw new IOException("Unable to read full block header");
-            mBytes += 512;
-            return true;
-        }
-
-        // overwrites 'info' fields based on the pax extended header
-        boolean readPaxExtendedHeader(InputStream instream, FileMetadata info)
-                throws IOException {
-            // We should never see a pax extended header larger than this
-            if (info.size > 32*1024) {
-                Slog.w(TAG, "Suspiciously large pax header size " + info.size
-                        + " - aborting");
-                throw new IOException("Sanity failure: pax header size " + info.size);
-            }
-
-            // read whole blocks, not just the content size
-            int numBlocks = (int)((info.size + 511) >> 9);
-            byte[] data = new byte[numBlocks * 512];
-            if (readExactly(instream, data, 0, data.length) < data.length) {
-                throw new IOException("Unable to read full pax header");
-            }
-            mBytes += data.length;
-
-            final int contentSize = (int) info.size;
-            int offset = 0;
-            do {
-                // extract the line at 'offset'
-                int eol = offset+1;
-                while (eol < contentSize && data[eol] != ' ') eol++;
-                if (eol >= contentSize) {
-                    // error: we just hit EOD looking for the end of the size field
-                    throw new IOException("Invalid pax data");
-                }
-                // eol points to the space between the count and the key
-                int linelen = (int) extractRadix(data, offset, eol - offset, 10);
-                int key = eol + 1;  // start of key=value
-                eol = offset + linelen - 1; // trailing LF
-                int value;
-                for (value = key+1; data[value] != '=' && value <= eol; value++);
-                if (value > eol) {
-                    throw new IOException("Invalid pax declaration");
-                }
-
-                // pax requires that key/value strings be in UTF-8
-                String keyStr = new String(data, key, value-key, "UTF-8");
-                // -1 to strip the trailing LF
-                String valStr = new String(data, value+1, eol-value-1, "UTF-8");
-
-                if ("path".equals(keyStr)) {
-                    info.path = valStr;
-                } else if ("size".equals(keyStr)) {
-                    info.size = Long.parseLong(valStr);
-                } else {
-                    if (DEBUG) Slog.i(TAG, "Unhandled pax key: " + key);
-                }
-
-                offset += linelen;
-            } while (offset < contentSize);
-
-            return true;
-        }
-
-        long extractRadix(byte[] data, int offset, int maxChars, int radix)
-                throws IOException {
-            long value = 0;
-            final int end = offset + maxChars;
-            for (int i = offset; i < end; i++) {
-                final byte b = data[i];
-                // Numeric fields in tar can terminate with either NUL or SPC
-                if (b == 0 || b == ' ') break;
-                if (b < '0' || b > ('0' + radix - 1)) {
-                    throw new IOException("Invalid number in header: '" + (char)b
-                            + "' for radix " + radix);
-                }
-                value = radix * value + (b - '0');
-            }
-            return value;
-        }
-
-        String extractString(byte[] data, int offset, int maxChars) throws IOException {
-            final int end = offset + maxChars;
-            int eos = offset;
-            // tar string fields terminate early with a NUL
-            while (eos < end && data[eos] != 0) eos++;
-            return new String(data, offset, eos-offset, "US-ASCII");
-        }
-
-        void sendStartRestore() {
-            if (mObserver != null) {
-                try {
-                    mObserver.onStartRestore();
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "full restore observer went away: startRestore");
-                    mObserver = null;
-                }
-            }
-        }
-
-        void sendOnRestorePackage(String name) {
-            if (mObserver != null) {
-                try {
-                    // TODO: use a more user-friendly name string
-                    mObserver.onRestorePackage(name);
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "full restore observer went away: restorePackage");
-                    mObserver = null;
-                }
-            }
-        }
-
-        void sendEndRestore() {
-            if (mObserver != null) {
-                try {
-                    mObserver.onEndRestore();
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "full restore observer went away: endRestore");
-                    mObserver = null;
-                }
-            }
-        }
-    }
-
-    // ***** end new engine class ***
-
-    // Used for synchronizing doRestoreFinished during adb restore
-    class AdbRestoreFinishedLatch implements BackupRestoreTask {
-        private static final String TAG = "AdbRestoreFinishedLatch";
-        final CountDownLatch mLatch;
-        private final int mCurrentOpToken;
-
-        AdbRestoreFinishedLatch(int currentOpToken) {
-            mLatch = new CountDownLatch(1);
-            mCurrentOpToken = currentOpToken;
-        }
-
-        void await() {
-            boolean latched = false;
-            try {
-                latched = mLatch.await(TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
-            } catch (InterruptedException e) {
-                Slog.w(TAG, "Interrupted!");
-            }
-        }
-
-        @Override
-        public void execute() {
-            // Unused
-        }
-
-        @Override
-        public void operationComplete(long result) {
-            if (MORE_DEBUG) {
-                Slog.w(TAG, "adb onRestoreFinished() complete");
-            }
-            mLatch.countDown();
-            removeOperation(mCurrentOpToken);
-        }
-
-        @Override
-        public void handleCancel(boolean cancelAll) {
-            if (DEBUG) {
-                Slog.w(TAG, "adb onRestoreFinished() timed out");
-            }
-            mLatch.countDown();
-            removeOperation(mCurrentOpToken);
-        }
-    }
-
-    class PerformAdbRestoreTask implements Runnable {
-        ParcelFileDescriptor mInputFile;
-        String mCurrentPassword;
-        String mDecryptPassword;
-        IFullBackupRestoreObserver mObserver;
-        AtomicBoolean mLatchObject;
-        IBackupAgent mAgent;
-        PackageManagerBackupAgent mPackageManagerBackupAgent;
-        String mAgentPackage;
-        ApplicationInfo mTargetApp;
-        FullBackupObbConnection mObbConnection = null;
-        ParcelFileDescriptor[] mPipes = null;
-        byte[] mWidgetData = null;
-
-        long mBytes;
-
-        // Runner that can be placed on a separate thread to do in-process invocation
-        // of the "restore finished" API asynchronously.  Used by adb restore.
-        class RestoreFinishedRunnable implements Runnable {
-            final IBackupAgent mAgent;
-            final int mToken;
-
-            RestoreFinishedRunnable(IBackupAgent agent, int token) {
-                mAgent = agent;
-                mToken = token;
-            }
-
-            @Override
-            public void run() {
-                try {
-                    mAgent.doRestoreFinished(mToken, mBackupManagerBinder);
-                } catch (RemoteException e) {
-                    // never happens; this is used only for local binder calls
-                }
-            }
-        }
-
-        // possible handling states for a given package in the restore dataset
-        final HashMap<String, RestorePolicy> mPackagePolicies
-                = new HashMap<>();
-
-        // installer package names for each encountered app, derived from the manifests
-        final HashMap<String, String> mPackageInstallers = new HashMap<>();
-
-        // Signatures for a given package found in its manifest file
-        final HashMap<String, Signature[]> mManifestSignatures
-                = new HashMap<>();
-
-        // Packages we've already wiped data on when restoring their first file
-        final HashSet<String> mClearedPackages = new HashSet<>();
-
-        PerformAdbRestoreTask(ParcelFileDescriptor fd, String curPassword, String decryptPassword,
-                IFullBackupRestoreObserver observer, AtomicBoolean latch) {
-            mInputFile = fd;
-            mCurrentPassword = curPassword;
-            mDecryptPassword = decryptPassword;
-            mObserver = observer;
-            mLatchObject = latch;
-            mAgent = null;
-            mPackageManagerBackupAgent = new PackageManagerBackupAgent(mPackageManager);
-            mAgentPackage = null;
-            mTargetApp = null;
-            mObbConnection = new FullBackupObbConnection();
-
-            // Which packages we've already wiped data on.  We prepopulate this
-            // with a whitelist of packages known to be unclearable.
-            mClearedPackages.add("android");
-            mClearedPackages.add(SETTINGS_PACKAGE);
-        }
-
-        class RestoreFileRunnable implements Runnable {
-            IBackupAgent mAgent;
-            FileMetadata mInfo;
-            ParcelFileDescriptor mSocket;
-            int mToken;
-
-            RestoreFileRunnable(IBackupAgent agent, FileMetadata info,
-                    ParcelFileDescriptor socket, int token) throws IOException {
-                mAgent = agent;
-                mInfo = info;
-                mToken = token;
-
-                // This class is used strictly for process-local binder invocations.  The
-                // semantics of ParcelFileDescriptor differ in this case; in particular, we
-                // do not automatically get a 'dup'ed descriptor that we can can continue
-                // to use asynchronously from the caller.  So, we make sure to dup it ourselves
-                // before proceeding to do the restore.
-                mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor());
-            }
-
-            @Override
-            public void run() {
-                try {
-                    mAgent.doRestoreFile(mSocket, mInfo.size, mInfo.type,
-                            mInfo.domain, mInfo.path, mInfo.mode, mInfo.mtime,
-                            mToken, mBackupManagerBinder);
-                } catch (RemoteException e) {
-                    // never happens; this is used strictly for local binder calls
-                }
-            }
-        }
-
-        @Override
-        public void run() {
-            Slog.i(TAG, "--- Performing full-dataset restore ---");
-            mObbConnection.establish();
-            sendStartRestore();
-
-            // Are we able to restore shared-storage data?
-            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
-                mPackagePolicies.put(SHARED_BACKUP_AGENT_PACKAGE, RestorePolicy.ACCEPT);
-            }
-
-            FileInputStream rawInStream = null;
-            DataInputStream rawDataIn = null;
-            try {
-                if (!backupPasswordMatches(mCurrentPassword)) {
-                    if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting");
-                    return;
-                }
-
-                mBytes = 0;
-                byte[] buffer = new byte[32 * 1024];
-                rawInStream = new FileInputStream(mInputFile.getFileDescriptor());
-                rawDataIn = new DataInputStream(rawInStream);
-
-                // First, parse out the unencrypted/uncompressed header
-                boolean compressed = false;
-                InputStream preCompressStream = rawInStream;
-                final InputStream in;
-
-                boolean okay = false;
-                final int headerLen = BACKUP_FILE_HEADER_MAGIC.length();
-                byte[] streamHeader = new byte[headerLen];
-                rawDataIn.readFully(streamHeader);
-                byte[] magicBytes = BACKUP_FILE_HEADER_MAGIC.getBytes("UTF-8");
-                if (Arrays.equals(magicBytes, streamHeader)) {
-                    // okay, header looks good.  now parse out the rest of the fields.
-                    String s = readHeaderLine(rawInStream);
-                    final int archiveVersion = Integer.parseInt(s);
-                    if (archiveVersion <= BACKUP_FILE_VERSION) {
-                        // okay, it's a version we recognize.  if it's version 1, we may need
-                        // to try two different PBKDF2 regimes to compare checksums.
-                        final boolean pbkdf2Fallback = (archiveVersion == 1);
-
-                        s = readHeaderLine(rawInStream);
-                        compressed = (Integer.parseInt(s) != 0);
-                        s = readHeaderLine(rawInStream);
-                        if (s.equals("none")) {
-                            // no more header to parse; we're good to go
-                            okay = true;
-                        } else if (mDecryptPassword != null && mDecryptPassword.length() > 0) {
-                            preCompressStream = decodeAesHeaderAndInitialize(s, pbkdf2Fallback,
-                                    rawInStream);
-                            if (preCompressStream != null) {
-                                okay = true;
-                            }
-                        } else Slog.w(TAG, "Archive is encrypted but no password given");
-                    } else Slog.w(TAG, "Wrong header version: " + s);
-                } else Slog.w(TAG, "Didn't read the right header magic");
-
-                if (!okay) {
-                    Slog.w(TAG, "Invalid restore data; aborting.");
-                    return;
-                }
-
-                // okay, use the right stream layer based on compression
-                in = (compressed) ? new InflaterInputStream(preCompressStream) : preCompressStream;
-
-                boolean didRestore;
-                do {
-                    didRestore = restoreOneFile(in, buffer);
-                } while (didRestore);
-
-                if (MORE_DEBUG) Slog.v(TAG, "Done consuming input tarfile, total bytes=" + mBytes);
-            } catch (IOException e) {
-                Slog.e(TAG, "Unable to read restore input");
-            } finally {
-                tearDownPipes();
-                tearDownAgent(mTargetApp, true);
-
-                try {
-                    if (rawDataIn != null) rawDataIn.close();
-                    if (rawInStream != null) rawInStream.close();
-                    mInputFile.close();
-                } catch (IOException e) {
-                    Slog.w(TAG, "Close of restore data pipe threw", e);
-                    /* nothing we can do about this */
-                }
-                synchronized (mLatchObject) {
-                    mLatchObject.set(true);
-                    mLatchObject.notifyAll();
-                }
-                mObbConnection.tearDown();
-                sendEndRestore();
-                Slog.d(TAG, "Full restore pass complete.");
-                mWakelock.release();
-            }
-        }
-
-        String readHeaderLine(InputStream in) throws IOException {
-            int c;
-            StringBuilder buffer = new StringBuilder(80);
-            while ((c = in.read()) >= 0) {
-                if (c == '\n') break;   // consume and discard the newlines
-                buffer.append((char)c);
-            }
-            return buffer.toString();
-        }
-
-        InputStream attemptMasterKeyDecryption(String algorithm, byte[] userSalt, byte[] ckSalt,
-                int rounds, String userIvHex, String masterKeyBlobHex, InputStream rawInStream,
-                boolean doLog) {
-            InputStream result = null;
-
-            try {
-                Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
-                SecretKey userKey = buildPasswordKey(algorithm, mDecryptPassword, userSalt,
-                        rounds);
-                byte[] IV = hexToByteArray(userIvHex);
-                IvParameterSpec ivSpec = new IvParameterSpec(IV);
-                c.init(Cipher.DECRYPT_MODE,
-                        new SecretKeySpec(userKey.getEncoded(), "AES"),
-                        ivSpec);
-                byte[] mkCipher = hexToByteArray(masterKeyBlobHex);
-                byte[] mkBlob = c.doFinal(mkCipher);
-
-                // first, the master key IV
-                int offset = 0;
-                int len = mkBlob[offset++];
-                IV = Arrays.copyOfRange(mkBlob, offset, offset + len);
-                offset += len;
-                // then the master key itself
-                len = mkBlob[offset++];
-                byte[] mk = Arrays.copyOfRange(mkBlob,
-                        offset, offset + len);
-                offset += len;
-                // and finally the master key checksum hash
-                len = mkBlob[offset++];
-                byte[] mkChecksum = Arrays.copyOfRange(mkBlob,
-                        offset, offset + len);
-
-                // now validate the decrypted master key against the checksum
-                byte[] calculatedCk = makeKeyChecksum(algorithm, mk, ckSalt, rounds);
-                if (Arrays.equals(calculatedCk, mkChecksum)) {
-                    ivSpec = new IvParameterSpec(IV);
-                    c.init(Cipher.DECRYPT_MODE,
-                            new SecretKeySpec(mk, "AES"),
-                            ivSpec);
-                    // Only if all of the above worked properly will 'result' be assigned
-                    result = new CipherInputStream(rawInStream, c);
-                } else if (doLog) Slog.w(TAG, "Incorrect password");
-            } catch (InvalidAlgorithmParameterException e) {
-                if (doLog) Slog.e(TAG, "Needed parameter spec unavailable!", e);
-            } catch (BadPaddingException e) {
-                // This case frequently occurs when the wrong password is used to decrypt
-                // the master key.  Use the identical "incorrect password" log text as is
-                // used in the checksum failure log in order to avoid providing additional
-                // information to an attacker.
-                if (doLog) Slog.w(TAG, "Incorrect password");
-            } catch (IllegalBlockSizeException e) {
-                if (doLog) Slog.w(TAG, "Invalid block size in master key");
-            } catch (NoSuchAlgorithmException e) {
-                if (doLog) Slog.e(TAG, "Needed decryption algorithm unavailable!");
-            } catch (NoSuchPaddingException e) {
-                if (doLog) Slog.e(TAG, "Needed padding mechanism unavailable!");
-            } catch (InvalidKeyException e) {
-                if (doLog) Slog.w(TAG, "Illegal password; aborting");
-            }
-
-            return result;
-        }
-
-        InputStream decodeAesHeaderAndInitialize(String encryptionName, boolean pbkdf2Fallback,
-                InputStream rawInStream) {
-            InputStream result = null;
-            try {
-                if (encryptionName.equals(ENCRYPTION_ALGORITHM_NAME)) {
-
-                    String userSaltHex = readHeaderLine(rawInStream); // 5
-                    byte[] userSalt = hexToByteArray(userSaltHex);
-
-                    String ckSaltHex = readHeaderLine(rawInStream); // 6
-                    byte[] ckSalt = hexToByteArray(ckSaltHex);
-
-                    int rounds = Integer.parseInt(readHeaderLine(rawInStream)); // 7
-                    String userIvHex = readHeaderLine(rawInStream); // 8
-
-                    String masterKeyBlobHex = readHeaderLine(rawInStream); // 9
-
-                    // decrypt the master key blob
-                    result = attemptMasterKeyDecryption(PBKDF_CURRENT, userSalt, ckSalt,
-                            rounds, userIvHex, masterKeyBlobHex, rawInStream, false);
-                    if (result == null && pbkdf2Fallback) {
-                        result = attemptMasterKeyDecryption(PBKDF_FALLBACK, userSalt, ckSalt,
-                                rounds, userIvHex, masterKeyBlobHex, rawInStream, true);
-                    }
-                } else Slog.w(TAG, "Unsupported encryption method: " + encryptionName);
-            } catch (NumberFormatException e) {
-                Slog.w(TAG, "Can't parse restore data header");
-            } catch (IOException e) {
-                Slog.w(TAG, "Can't read input header");
-            }
-
-            return result;
-        }
-
-        boolean restoreOneFile(InputStream instream, byte[] buffer) {
-            FileMetadata info;
-            try {
-                info = readTarHeaders(instream);
-                if (info != null) {
-                    if (MORE_DEBUG) {
-                        dumpFileMetadata(info);
-                    }
-
-                    final String pkg = info.packageName;
-                    if (!pkg.equals(mAgentPackage)) {
-                        // okay, change in package; set up our various
-                        // bookkeeping if we haven't seen it yet
-                        if (!mPackagePolicies.containsKey(pkg)) {
-                            mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
-                        }
-
-                        // Clean up the previous agent relationship if necessary,
-                        // and let the observer know we're considering a new app.
-                        if (mAgent != null) {
-                            if (DEBUG) Slog.d(TAG, "Saw new package; finalizing old one");
-                            // Now we're really done
-                            tearDownPipes();
-                            tearDownAgent(mTargetApp, true);
-                            mTargetApp = null;
-                            mAgentPackage = null;
-                        }
-                    }
-
-                    if (info.path.equals(BACKUP_MANIFEST_FILENAME)) {
-                        mPackagePolicies.put(pkg, readAppManifest(info, instream));
-                        mPackageInstallers.put(pkg, info.installerPackageName);
-                        // We've read only the manifest content itself at this point,
-                        // so consume the footer before looping around to the next
-                        // input file
-                        skipTarPadding(info.size, instream);
-                        sendOnRestorePackage(pkg);
-                    } else if (info.path.equals(BACKUP_METADATA_FILENAME)) {
-                        // Metadata blobs!
-                        readMetadata(info, instream);
-                        skipTarPadding(info.size, instream);
-                    } else {
-                        // Non-manifest, so it's actual file data.  Is this a package
-                        // we're ignoring?
-                        boolean okay = true;
-                        RestorePolicy policy = mPackagePolicies.get(pkg);
-                        switch (policy) {
-                            case IGNORE:
-                                okay = false;
-                                break;
-
-                            case ACCEPT_IF_APK:
-                                // If we're in accept-if-apk state, then the first file we
-                                // see MUST be the apk.
-                                if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
-                                    if (DEBUG) Slog.d(TAG, "APK file; installing");
-                                    // Try to install the app.
-                                    String installerName = mPackageInstallers.get(pkg);
-                                    okay = installApk(info, installerName, instream);
-                                    // good to go; promote to ACCEPT
-                                    mPackagePolicies.put(pkg, (okay)
-                                            ? RestorePolicy.ACCEPT
-                                            : RestorePolicy.IGNORE);
-                                    // At this point we've consumed this file entry
-                                    // ourselves, so just strip the tar footer and
-                                    // go on to the next file in the input stream
-                                    skipTarPadding(info.size, instream);
-                                    return true;
-                                } else {
-                                    // File data before (or without) the apk.  We can't
-                                    // handle it coherently in this case so ignore it.
-                                    mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
-                                    okay = false;
-                                }
-                                break;
-
-                            case ACCEPT:
-                                if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
-                                    if (DEBUG) Slog.d(TAG, "apk present but ACCEPT");
-                                    // we can take the data without the apk, so we
-                                    // *want* to do so.  skip the apk by declaring this
-                                    // one file not-okay without changing the restore
-                                    // policy for the package.
-                                    okay = false;
-                                }
-                                break;
-
-                            default:
-                                // Something has gone dreadfully wrong when determining
-                                // the restore policy from the manifest.  Ignore the
-                                // rest of this package's data.
-                                Slog.e(TAG, "Invalid policy from manifest");
-                                okay = false;
-                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
-                                break;
-                        }
-
-                        // The path needs to be canonical
-                        if (info.path.contains("..") || info.path.contains("//")) {
-                            if (MORE_DEBUG) {
-                                Slog.w(TAG, "Dropping invalid path " + info.path);
-                            }
-                            okay = false;
-                        }
-
-                        // If the policy is satisfied, go ahead and set up to pipe the
-                        // data to the agent.
-                        if (DEBUG && okay && mAgent != null) {
-                            Slog.i(TAG, "Reusing existing agent instance");
-                        }
-                        if (okay && mAgent == null) {
-                            if (DEBUG) Slog.d(TAG, "Need to launch agent for " + pkg);
-
-                            try {
-                                mTargetApp = mPackageManager.getApplicationInfo(pkg, 0);
-
-                                // If we haven't sent any data to this app yet, we probably
-                                // need to clear it first.  Check that.
-                                if (!mClearedPackages.contains(pkg)) {
-                                    // apps with their own backup agents are
-                                    // responsible for coherently managing a full
-                                    // restore.
-                                    if (mTargetApp.backupAgentName == null) {
-                                        if (DEBUG) Slog.d(TAG, "Clearing app data preparatory to full restore");
-                                        clearApplicationDataSynchronous(pkg);
-                                    } else {
-                                        if (DEBUG) Slog.d(TAG, "backup agent ("
-                                                + mTargetApp.backupAgentName + ") => no clear");
-                                    }
-                                    mClearedPackages.add(pkg);
-                                } else {
-                                    if (DEBUG) Slog.d(TAG, "We've initialized this app already; no clear required");
-                                }
-
-                                // All set; now set up the IPC and launch the agent
-                                setUpPipes();
-                                mAgent = bindToAgentSynchronous(mTargetApp,
-                                        ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL);
-                                mAgentPackage = pkg;
-                            } catch (IOException e) {
-                                // fall through to error handling
-                            } catch (NameNotFoundException e) {
-                                // fall through to error handling
-                            }
-
-                            if (mAgent == null) {
-                                if (DEBUG) Slog.d(TAG, "Unable to create agent for " + pkg);
-                                okay = false;
-                                tearDownPipes();
-                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
-                            }
-                        }
-
-                        // Sanity check: make sure we never give data to the wrong app.  This
-                        // should never happen but a little paranoia here won't go amiss.
-                        if (okay && !pkg.equals(mAgentPackage)) {
-                            Slog.e(TAG, "Restoring data for " + pkg
-                                    + " but agent is for " + mAgentPackage);
-                            okay = false;
-                        }
-
-                        // At this point we have an agent ready to handle the full
-                        // restore data as well as a pipe for sending data to
-                        // that agent.  Tell the agent to start reading from the
-                        // pipe.
-                        if (okay) {
-                            boolean agentSuccess = true;
-                            long toCopy = info.size;
-                            final int token = generateRandomIntegerToken();
-                            try {
-                                prepareOperationTimeout(token, TIMEOUT_RESTORE_INTERVAL, null,
-                                        OP_TYPE_RESTORE_WAIT);
-                                if (FullBackup.OBB_TREE_TOKEN.equals(info.domain)) {
-                                    if (DEBUG) Slog.d(TAG, "Restoring OBB file for " + pkg
-                                            + " : " + info.path);
-                                    mObbConnection.restoreObbFile(pkg, mPipes[0],
-                                            info.size, info.type, info.path, info.mode,
-                                            info.mtime, token, mBackupManagerBinder);
-                                } else if (FullBackup.KEY_VALUE_DATA_TOKEN.equals(info.domain)) {
-                                    if (DEBUG) Slog.d(TAG, "Restoring key-value file for " + pkg
-                                            + " : " + info.path);
-                                    KeyValueAdbRestoreEngine restoreEngine =
-                                            new KeyValueAdbRestoreEngine(RefactoredBackupManagerService.this,
-                                                    mDataDir, info, mPipes[0], mAgent, token);
-                                    new Thread(restoreEngine, "restore-key-value-runner").start();
-                                } else {
-                                    if (DEBUG) Slog.d(TAG, "Invoking agent to restore file "
-                                            + info.path);
-                                    // fire up the app's agent listening on the socket.  If
-                                    // the agent is running in the system process we can't
-                                    // just invoke it asynchronously, so we provide a thread
-                                    // for it here.
-                                    if (mTargetApp.processName.equals("system")) {
-                                        Slog.d(TAG, "system process agent - spinning a thread");
-                                        RestoreFileRunnable runner = new RestoreFileRunnable(
-                                                mAgent, info, mPipes[0], token);
-                                        new Thread(runner, "restore-sys-runner").start();
-                                    } else {
-                                        mAgent.doRestoreFile(mPipes[0], info.size, info.type,
-                                                info.domain, info.path, info.mode, info.mtime,
-                                                token, mBackupManagerBinder);
-                                    }
-                                }
-                            } catch (IOException e) {
-                                // couldn't dup the socket for a process-local restore
-                                Slog.d(TAG, "Couldn't establish restore");
-                                agentSuccess = false;
-                                okay = false;
-                            } catch (RemoteException e) {
-                                // whoops, remote entity went away.  We'll eat the content
-                                // ourselves, then, and not copy it over.
-                                Slog.e(TAG, "Agent crashed during full restore");
-                                agentSuccess = false;
-                                okay = false;
-                            }
-
-                            // Copy over the data if the agent is still good
-                            if (okay) {
-                                boolean pipeOkay = true;
-                                FileOutputStream pipe = new FileOutputStream(
-                                        mPipes[1].getFileDescriptor());
-                                while (toCopy > 0) {
-                                    int toRead = (toCopy > buffer.length)
-                                    ? buffer.length : (int)toCopy;
-                                    int nRead = instream.read(buffer, 0, toRead);
-                                    if (nRead >= 0) mBytes += nRead;
-                                    if (nRead <= 0) break;
-                                    toCopy -= nRead;
-
-                                    // send it to the output pipe as long as things
-                                    // are still good
-                                    if (pipeOkay) {
-                                        try {
-                                            pipe.write(buffer, 0, nRead);
-                                        } catch (IOException e) {
-                                            Slog.e(TAG, "Failed to write to restore pipe", e);
-                                            pipeOkay = false;
-                                        }
-                                    }
-                                }
-
-                                // done sending that file!  Now we just need to consume
-                                // the delta from info.size to the end of block.
-                                skipTarPadding(info.size, instream);
-
-                                // and now that we've sent it all, wait for the remote
-                                // side to acknowledge receipt
-                                agentSuccess = waitUntilOperationComplete(token);
-                            }
-
-                            // okay, if the remote end failed at any point, deal with
-                            // it by ignoring the rest of the restore on it
-                            if (!agentSuccess) {
-                                if (DEBUG) {
-                                    Slog.d(TAG, "Agent failure restoring " + pkg + "; now ignoring");
-                                }
-                                mBackupHandler.removeMessages(MSG_RESTORE_OPERATION_TIMEOUT);
-                                tearDownPipes();
-                                tearDownAgent(mTargetApp, false);
-                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
-                            }
-                        }
-
-                        // Problems setting up the agent communication, or an already-
-                        // ignored package: skip to the next tar stream entry by
-                        // reading and discarding this file.
-                        if (!okay) {
-                            if (DEBUG) Slog.d(TAG, "[discarding file content]");
-                            long bytesToConsume = (info.size + 511) & ~511;
-                            while (bytesToConsume > 0) {
-                                int toRead = (bytesToConsume > buffer.length)
-                                ? buffer.length : (int)bytesToConsume;
-                                long nRead = instream.read(buffer, 0, toRead);
-                                if (nRead >= 0) mBytes += nRead;
-                                if (nRead <= 0) break;
-                                bytesToConsume -= nRead;
-                            }
-                        }
-                    }
-                }
-            } catch (IOException e) {
-                if (DEBUG) Slog.w(TAG, "io exception on restore socket read", e);
-                // treat as EOF
-                info = null;
-            }
-
-            return (info != null);
-        }
-
-        void setUpPipes() throws IOException {
-            mPipes = ParcelFileDescriptor.createPipe();
-        }
-
-        void tearDownPipes() {
-            if (mPipes != null) {
-                try {
-                    mPipes[0].close();
-                    mPipes[0] = null;
-                    mPipes[1].close();
-                    mPipes[1] = null;
-                } catch (IOException e) {
-                    Slog.w(TAG, "Couldn't close agent pipes", e);
-                }
-                mPipes = null;
-            }
-        }
-
-        void tearDownAgent(ApplicationInfo app, boolean doRestoreFinished) {
-            if (mAgent != null) {
-                try {
-                    // In the adb restore case, we do restore-finished here
-                    if (doRestoreFinished) {
-                        final int token = generateRandomIntegerToken();
-                        final AdbRestoreFinishedLatch latch = new AdbRestoreFinishedLatch(token);
-                        prepareOperationTimeout(token, TIMEOUT_FULL_BACKUP_INTERVAL, latch,
-                                OP_TYPE_RESTORE_WAIT);
-                        if (mTargetApp.processName.equals("system")) {
-                            if (MORE_DEBUG) {
-                                Slog.d(TAG, "system agent - restoreFinished on thread");
-                            }
-                            Runnable runner = new RestoreFinishedRunnable(mAgent, token);
-                            new Thread(runner, "restore-sys-finished-runner").start();
-                        } else {
-                            mAgent.doRestoreFinished(token, mBackupManagerBinder);
-                        }
-
-                        latch.await();
-                    }
-
-                    // unbind and tidy up even on timeout or failure, just in case
-                    mActivityManager.unbindBackupAgent(app);
-
-                    // The agent was running with a stub Application object, so shut it down.
-                    // !!! We hardcode the confirmation UI's package name here rather than use a
-                    //     manifest flag!  TODO something less direct.
-                    if (app.uid >= Process.FIRST_APPLICATION_UID
-                            && !app.packageName.equals("com.android.backupconfirm")) {
-                        if (DEBUG) Slog.d(TAG, "Killing host process");
-                        mActivityManager.killApplicationProcess(app.processName, app.uid);
-                    } else {
-                        if (DEBUG) Slog.d(TAG, "Not killing after full restore");
-                    }
-                } catch (RemoteException e) {
-                    Slog.d(TAG, "Lost app trying to shut down");
-                }
-                mAgent = null;
-            }
-        }
-
-        class RestoreInstallObserver extends PackageInstallObserver {
-            final AtomicBoolean mDone = new AtomicBoolean();
-            String mPackageName;
-            int mResult;
-
-            public void reset() {
-                synchronized (mDone) {
-                    mDone.set(false);
-                }
-            }
-
-            public void waitForCompletion() {
-                synchronized (mDone) {
-                    while (mDone.get() == false) {
-                        try {
-                            mDone.wait();
-                        } catch (InterruptedException e) { }
-                    }
-                }
-            }
-
-            int getResult() {
-                return mResult;
-            }
-
-            @Override
-            public void onPackageInstalled(String packageName, int returnCode,
-                    String msg, Bundle extras) {
-                synchronized (mDone) {
-                    mResult = returnCode;
-                    mPackageName = packageName;
-                    mDone.set(true);
-                    mDone.notifyAll();
-                }
-            }
-        }
-
-        class RestoreDeleteObserver extends IPackageDeleteObserver.Stub {
-            final AtomicBoolean mDone = new AtomicBoolean();
-            int mResult;
-
-            public void reset() {
-                synchronized (mDone) {
-                    mDone.set(false);
-                }
-            }
-
-            public void waitForCompletion() {
-                synchronized (mDone) {
-                    while (mDone.get() == false) {
-                        try {
-                            mDone.wait();
-                        } catch (InterruptedException e) { }
-                    }
-                }
-            }
-
-            @Override
-            public void packageDeleted(String packageName, int returnCode) throws RemoteException {
-                synchronized (mDone) {
-                    mResult = returnCode;
-                    mDone.set(true);
-                    mDone.notifyAll();
-                }
-            }
-        }
-
-        final RestoreInstallObserver mInstallObserver = new RestoreInstallObserver();
-        final RestoreDeleteObserver mDeleteObserver = new RestoreDeleteObserver();
-
-        boolean installApk(FileMetadata info, String installerPackage, InputStream instream) {
-            boolean okay = true;
-
-            if (DEBUG) Slog.d(TAG, "Installing from backup: " + info.packageName);
-
-            // The file content is an .apk file.  Copy it out to a staging location and
-            // attempt to install it.
-            File apkFile = new File(mDataDir, info.packageName);
-            try {
-                FileOutputStream apkStream = new FileOutputStream(apkFile);
-                byte[] buffer = new byte[32 * 1024];
-                long size = info.size;
-                while (size > 0) {
-                    long toRead = (buffer.length < size) ? buffer.length : size;
-                    int didRead = instream.read(buffer, 0, (int)toRead);
-                    if (didRead >= 0) mBytes += didRead;
-                    apkStream.write(buffer, 0, didRead);
-                    size -= didRead;
-                }
-                apkStream.close();
-
-                // make sure the installer can read it
-                apkFile.setReadable(true, false);
-
-                // Now install it
-                Uri packageUri = Uri.fromFile(apkFile);
-                mInstallObserver.reset();
-                mPackageManager.installPackage(packageUri, mInstallObserver,
-                        PackageManager.INSTALL_REPLACE_EXISTING | PackageManager.INSTALL_FROM_ADB,
-                        installerPackage);
-                mInstallObserver.waitForCompletion();
-
-                if (mInstallObserver.getResult() != PackageManager.INSTALL_SUCCEEDED) {
-                    // The only time we continue to accept install of data even if the
-                    // apk install failed is if we had already determined that we could
-                    // accept the data regardless.
-                    if (mPackagePolicies.get(info.packageName) != RestorePolicy.ACCEPT) {
-                        okay = false;
-                    }
-                } else {
-                    // Okay, the install succeeded.  Make sure it was the right app.
-                    boolean uninstall = false;
-                    if (!mInstallObserver.mPackageName.equals(info.packageName)) {
-                        Slog.w(TAG, "Restore stream claimed to include apk for "
-                                + info.packageName + " but apk was really "
-                                + mInstallObserver.mPackageName);
-                        // delete the package we just put in place; it might be fraudulent
-                        okay = false;
-                        uninstall = true;
-                    } else {
-                        try {
-                            PackageInfo pkg = mPackageManager.getPackageInfo(info.packageName,
-                                    PackageManager.GET_SIGNATURES);
-                            if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) {
-                                Slog.w(TAG, "Restore stream contains apk of package "
-                                        + info.packageName + " but it disallows backup/restore");
-                                okay = false;
-                            } else {
-                                // So far so good -- do the signatures match the manifest?
-                                Signature[] sigs = mManifestSignatures.get(info.packageName);
-                                if (signaturesMatch(sigs, pkg)) {
-                                    // If this is a system-uid app without a declared backup agent,
-                                    // don't restore any of the file data.
-                                    if ((pkg.applicationInfo.uid < Process.FIRST_APPLICATION_UID)
-                                            && (pkg.applicationInfo.backupAgentName == null)) {
-                                        Slog.w(TAG, "Installed app " + info.packageName
-                                                + " has restricted uid and no agent");
-                                        okay = false;
-                                    }
-                                } else {
-                                    Slog.w(TAG, "Installed app " + info.packageName
-                                            + " signatures do not match restore manifest");
-                                    okay = false;
-                                    uninstall = true;
-                                }
-                            }
-                        } catch (NameNotFoundException e) {
-                            Slog.w(TAG, "Install of package " + info.packageName
-                                    + " succeeded but now not found");
-                            okay = false;
-                        }
-                    }
-
-                    // If we're not okay at this point, we need to delete the package
-                    // that we just installed.
-                    if (uninstall) {
-                        mDeleteObserver.reset();
-                        mPackageManager.deletePackage(mInstallObserver.mPackageName,
-                                mDeleteObserver, 0);
-                        mDeleteObserver.waitForCompletion();
-                    }
-                }
-            } catch (IOException e) {
-                Slog.e(TAG, "Unable to transcribe restored apk for install");
-                okay = false;
-            } finally {
-                apkFile.delete();
-            }
-
-            return okay;
-        }
-
-        // Given an actual file content size, consume the post-content padding mandated
-        // by the tar format.
-        void skipTarPadding(long size, InputStream instream) throws IOException {
-            long partial = (size + 512) % 512;
-            if (partial > 0) {
-                final int needed = 512 - (int)partial;
-                byte[] buffer = new byte[needed];
-                if (readExactly(instream, buffer, 0, needed) == needed) {
-                    mBytes += needed;
-                } else throw new IOException("Unexpected EOF in padding");
-            }
-        }
-
-        // Read a widget metadata file, returning the restored blob
-        void readMetadata(FileMetadata info, InputStream instream) throws IOException {
-            // Fail on suspiciously large widget dump files
-            if (info.size > 64 * 1024) {
-                throw new IOException("Metadata too big; corrupt? size=" + info.size);
-            }
-
-            byte[] buffer = new byte[(int) info.size];
-            if (readExactly(instream, buffer, 0, (int)info.size) == info.size) {
-                mBytes += info.size;
-            } else throw new IOException("Unexpected EOF in widget data");
-
-            String[] str = new String[1];
-            int offset = extractLine(buffer, 0, str);
-            int version = Integer.parseInt(str[0]);
-            if (version == BACKUP_MANIFEST_VERSION) {
-                offset = extractLine(buffer, offset, str);
-                final String pkg = str[0];
-                if (info.packageName.equals(pkg)) {
-                    // Data checks out -- the rest of the buffer is a concatenation of
-                    // binary blobs as described in the comment at writeAppWidgetData()
-                    ByteArrayInputStream bin = new ByteArrayInputStream(buffer,
-                            offset, buffer.length - offset);
-                    DataInputStream in = new DataInputStream(bin);
-                    while (bin.available() > 0) {
-                        int token = in.readInt();
-                        int size = in.readInt();
-                        if (size > 64 * 1024) {
-                            throw new IOException("Datum "
-                                    + Integer.toHexString(token)
-                                    + " too big; corrupt? size=" + info.size);
-                        }
-                        switch (token) {
-                            case BACKUP_WIDGET_METADATA_TOKEN:
-                            {
-                                if (MORE_DEBUG) {
-                                    Slog.i(TAG, "Got widget metadata for " + info.packageName);
-                                }
-                                mWidgetData = new byte[size];
-                                in.read(mWidgetData);
-                                break;
-                            }
-                            default:
-                            {
-                                if (DEBUG) {
-                                    Slog.i(TAG, "Ignoring metadata blob "
-                                            + Integer.toHexString(token)
-                                            + " for " + info.packageName);
-                                }
-                                in.skipBytes(size);
-                                break;
-                            }
-                        }
-                    }
-                } else {
-                    Slog.w(TAG, "Metadata mismatch: package " + info.packageName
-                            + " but widget data for " + pkg);
-                }
-            } else {
-                Slog.w(TAG, "Unsupported metadata version " + version);
-            }
-        }
-
-        // Returns a policy constant; takes a buffer arg to reduce memory churn
-        RestorePolicy readAppManifest(FileMetadata info, InputStream instream)
-                throws IOException {
-            // Fail on suspiciously large manifest files
-            if (info.size > 64 * 1024) {
-                throw new IOException("Restore manifest too big; corrupt? size=" + info.size);
-            }
-
-            byte[] buffer = new byte[(int) info.size];
-            if (readExactly(instream, buffer, 0, (int)info.size) == info.size) {
-                mBytes += info.size;
-            } else throw new IOException("Unexpected EOF in manifest");
-
-            RestorePolicy policy = RestorePolicy.IGNORE;
-            String[] str = new String[1];
-            int offset = 0;
-
-            try {
-                offset = extractLine(buffer, offset, str);
-                int version = Integer.parseInt(str[0]);
-                if (version == BACKUP_MANIFEST_VERSION) {
-                    offset = extractLine(buffer, offset, str);
-                    String manifestPackage = str[0];
-                    // TODO: handle <original-package>
-                    if (manifestPackage.equals(info.packageName)) {
-                        offset = extractLine(buffer, offset, str);
-                        version = Integer.parseInt(str[0]);  // app version
-                        offset = extractLine(buffer, offset, str);
-                        // This is the platform version, which we don't use, but we parse it
-                        // as a safety against corruption in the manifest.
-                        Integer.parseInt(str[0]);
-                        offset = extractLine(buffer, offset, str);
-                        info.installerPackageName = (str[0].length() > 0) ? str[0] : null;
-                        offset = extractLine(buffer, offset, str);
-                        boolean hasApk = str[0].equals("1");
-                        offset = extractLine(buffer, offset, str);
-                        int numSigs = Integer.parseInt(str[0]);
-                        if (numSigs > 0) {
-                            Signature[] sigs = new Signature[numSigs];
-                            for (int i = 0; i < numSigs; i++) {
-                                offset = extractLine(buffer, offset, str);
-                                sigs[i] = new Signature(str[0]);
-                            }
-                            mManifestSignatures.put(info.packageName, sigs);
-
-                            // Okay, got the manifest info we need...
-                            try {
-                                PackageInfo pkgInfo = mPackageManager.getPackageInfo(
-                                        info.packageName, PackageManager.GET_SIGNATURES);
-                                // Fall through to IGNORE if the app explicitly disallows backup
-                                final int flags = pkgInfo.applicationInfo.flags;
-                                if ((flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) {
-                                    // Restore system-uid-space packages only if they have
-                                    // defined a custom backup agent
-                                    if ((pkgInfo.applicationInfo.uid >= Process.FIRST_APPLICATION_UID)
-                                            || (pkgInfo.applicationInfo.backupAgentName != null)) {
-                                        // Verify signatures against any installed version; if they
-                                        // don't match, then we fall though and ignore the data.  The
-                                        // signatureMatch() method explicitly ignores the signature
-                                        // check for packages installed on the system partition, because
-                                        // such packages are signed with the platform cert instead of
-                                        // the app developer's cert, so they're different on every
-                                        // device.
-                                        if (signaturesMatch(sigs, pkgInfo)) {
-                                            if ((pkgInfo.applicationInfo.flags
-                                                    & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) != 0) {
-                                                Slog.i(TAG, "Package has restoreAnyVersion; taking data");
-                                                policy = RestorePolicy.ACCEPT;
-                                            } else if (pkgInfo.versionCode >= version) {
-                                                Slog.i(TAG, "Sig + version match; taking data");
-                                                policy = RestorePolicy.ACCEPT;
-                                            } else {
-                                                // The data is from a newer version of the app than
-                                                // is presently installed.  That means we can only
-                                                // use it if the matching apk is also supplied.
-                                                Slog.d(TAG, "Data version " + version
-                                                        + " is newer than installed version "
-                                                        + pkgInfo.versionCode + " - requiring apk");
-                                                policy = RestorePolicy.ACCEPT_IF_APK;
-                                            }
-                                        } else {
-                                            Slog.w(TAG, "Restore manifest signatures do not match "
-                                                    + "installed application for " + info.packageName);
-                                        }
-                                    } else {
-                                        Slog.w(TAG, "Package " + info.packageName
-                                                + " is system level with no agent");
-                                    }
-                                } else {
-                                    if (DEBUG) Slog.i(TAG, "Restore manifest from "
-                                            + info.packageName + " but allowBackup=false");
-                                }
-                            } catch (NameNotFoundException e) {
-                                // Okay, the target app isn't installed.  We can process
-                                // the restore properly only if the dataset provides the
-                                // apk file and we can successfully install it.
-                                if (DEBUG) Slog.i(TAG, "Package " + info.packageName
-                                        + " not installed; requiring apk in dataset");
-                                policy = RestorePolicy.ACCEPT_IF_APK;
-                            }
-
-                            if (policy == RestorePolicy.ACCEPT_IF_APK && !hasApk) {
-                                Slog.i(TAG, "Cannot restore package " + info.packageName
-                                        + " without the matching .apk");
-                            }
-                        } else {
-                            Slog.i(TAG, "Missing signature on backed-up package "
-                                    + info.packageName);
-                        }
-                    } else {
-                        Slog.i(TAG, "Expected package " + info.packageName
-                                + " but restore manifest claims " + manifestPackage);
-                    }
-                } else {
-                    Slog.i(TAG, "Unknown restore manifest version " + version
-                            + " for package " + info.packageName);
-                }
-            } catch (NumberFormatException e) {
-                Slog.w(TAG, "Corrupt restore manifest for package " + info.packageName);
-            } catch (IllegalArgumentException e) {
-                Slog.w(TAG, e.getMessage());
-            }
-
-            return policy;
-        }
-
-        // Builds a line from a byte buffer starting at 'offset', and returns
-        // the index of the next unconsumed data in the buffer.
-        int extractLine(byte[] buffer, int offset, String[] outStr) throws IOException {
-            final int end = buffer.length;
-            if (offset >= end) throw new IOException("Incomplete data");
-
-            int pos;
-            for (pos = offset; pos < end; pos++) {
-                byte c = buffer[pos];
-                // at LF we declare end of line, and return the next char as the
-                // starting point for the next time through
-                if (c == '\n') {
-                    break;
-                }
-            }
-            outStr[0] = new String(buffer, offset, pos - offset);
-            pos++;  // may be pointing an extra byte past the end but that's okay
-            return pos;
-        }
-
-        void dumpFileMetadata(FileMetadata info) {
-            if (DEBUG) {
-                StringBuilder b = new StringBuilder(128);
-
-                // mode string
-                b.append((info.type == BackupAgent.TYPE_DIRECTORY) ? 'd' : '-');
-                b.append(((info.mode & 0400) != 0) ? 'r' : '-');
-                b.append(((info.mode & 0200) != 0) ? 'w' : '-');
-                b.append(((info.mode & 0100) != 0) ? 'x' : '-');
-                b.append(((info.mode & 0040) != 0) ? 'r' : '-');
-                b.append(((info.mode & 0020) != 0) ? 'w' : '-');
-                b.append(((info.mode & 0010) != 0) ? 'x' : '-');
-                b.append(((info.mode & 0004) != 0) ? 'r' : '-');
-                b.append(((info.mode & 0002) != 0) ? 'w' : '-');
-                b.append(((info.mode & 0001) != 0) ? 'x' : '-');
-                b.append(String.format(" %9d ", info.size));
-
-                Date stamp = new Date(info.mtime);
-                b.append(new SimpleDateFormat("MMM dd HH:mm:ss ").format(stamp));
-
-                b.append(info.packageName);
-                b.append(" :: ");
-                b.append(info.domain);
-                b.append(" :: ");
-                b.append(info.path);
-
-                Slog.i(TAG, b.toString());
-            }
-        }
-
-        // Consume a tar file header block [sequence] and accumulate the relevant metadata
-        FileMetadata readTarHeaders(InputStream instream) throws IOException {
-            byte[] block = new byte[512];
-            FileMetadata info = null;
-
-            boolean gotHeader = readTarHeader(instream, block);
-            if (gotHeader) {
-                try {
-                    // okay, presume we're okay, and extract the various metadata
-                    info = new FileMetadata();
-                    info.size = extractRadix(block, 124, 12, 8);
-                    info.mtime = extractRadix(block, 136, 12, 8);
-                    info.mode = extractRadix(block, 100, 8, 8);
-
-                    info.path = extractString(block, 345, 155); // prefix
-                    String path = extractString(block, 0, 100);
-                    if (path.length() > 0) {
-                        if (info.path.length() > 0) info.path += '/';
-                        info.path += path;
-                    }
-
-                    // tar link indicator field: 1 byte at offset 156 in the header.
-                    int typeChar = block[156];
-                    if (typeChar == 'x') {
-                        // pax extended header, so we need to read that
-                        gotHeader = readPaxExtendedHeader(instream, info);
-                        if (gotHeader) {
-                            // and after a pax extended header comes another real header -- read
-                            // that to find the real file type
-                            gotHeader = readTarHeader(instream, block);
-                        }
-                        if (!gotHeader) throw new IOException("Bad or missing pax header");
-
-                        typeChar = block[156];
-                    }
-
-                    switch (typeChar) {
-                        case '0': info.type = BackupAgent.TYPE_FILE; break;
-                        case '5': {
-                            info.type = BackupAgent.TYPE_DIRECTORY;
-                            if (info.size != 0) {
-                                Slog.w(TAG, "Directory entry with nonzero size in header");
-                                info.size = 0;
-                            }
-                            break;
-                        }
-                        case 0: {
-                            // presume EOF
-                            if (DEBUG) Slog.w(TAG, "Saw type=0 in tar header block, info=" + info);
-                            return null;
-                        }
-                        default: {
-                            Slog.e(TAG, "Unknown tar entity type: " + typeChar);
-                            throw new IOException("Unknown entity type " + typeChar);
-                        }
-                    }
-
-                    // Parse out the path
-                    //
-                    // first: apps/shared/unrecognized
-                    if (FullBackup.SHARED_PREFIX.regionMatches(0,
-                            info.path, 0, FullBackup.SHARED_PREFIX.length())) {
-                        // File in shared storage.  !!! TODO: implement this.
-                        info.path = info.path.substring(FullBackup.SHARED_PREFIX.length());
-                        info.packageName = SHARED_BACKUP_AGENT_PACKAGE;
-                        info.domain = FullBackup.SHARED_STORAGE_TOKEN;
-                        if (DEBUG) Slog.i(TAG, "File in shared storage: " + info.path);
-                    } else if (FullBackup.APPS_PREFIX.regionMatches(0,
-                            info.path, 0, FullBackup.APPS_PREFIX.length())) {
-                        // App content!  Parse out the package name and domain
-
-                        // strip the apps/ prefix
-                        info.path = info.path.substring(FullBackup.APPS_PREFIX.length());
-
-                        // extract the package name
-                        int slash = info.path.indexOf('/');
-                        if (slash < 0) throw new IOException("Illegal semantic path in " + info.path);
-                        info.packageName = info.path.substring(0, slash);
-                        info.path = info.path.substring(slash+1);
-
-                        // if it's a manifest or metadata payload we're done, otherwise parse
-                        // out the domain into which the file will be restored
-                        if (!info.path.equals(BACKUP_MANIFEST_FILENAME)
-                                && !info.path.equals(BACKUP_METADATA_FILENAME)) {
-                            slash = info.path.indexOf('/');
-                            if (slash < 0) throw new IOException("Illegal semantic path in non-manifest " + info.path);
-                            info.domain = info.path.substring(0, slash);
-                            info.path = info.path.substring(slash + 1);
-                        }
-                    }
-                } catch (IOException e) {
-                    if (DEBUG) {
-                        Slog.e(TAG, "Parse error in header: " + e.getMessage());
-                        HEXLOG(block);
-                    }
-                    throw e;
-                }
-            }
-            return info;
-        }
-
-        private void HEXLOG(byte[] block) {
-            int offset = 0;
-            int todo = block.length;
-            StringBuilder buf = new StringBuilder(64);
-            while (todo > 0) {
-                buf.append(String.format("%04x   ", offset));
-                int numThisLine = (todo > 16) ? 16 : todo;
-                for (int i = 0; i < numThisLine; i++) {
-                    buf.append(String.format("%02x ", block[offset+i]));
-                }
-                Slog.i("hexdump", buf.toString());
-                buf.setLength(0);
-                todo -= numThisLine;
-                offset += numThisLine;
-            }
-        }
-
-        // Read exactly the given number of bytes into a buffer at the stated offset.
-        // Returns false if EOF is encountered before the requested number of bytes
-        // could be read.
-        int readExactly(InputStream in, byte[] buffer, int offset, int size)
-                throws IOException {
-            if (size <= 0) throw new IllegalArgumentException("size must be > 0");
-
-            int soFar = 0;
-            while (soFar < size) {
-                int nRead = in.read(buffer, offset + soFar, size - soFar);
-                if (nRead <= 0) {
-                    if (MORE_DEBUG) Slog.w(TAG, "- wanted exactly " + size + " but got only " + soFar);
-                    break;
-                }
-                soFar += nRead;
-            }
-            return soFar;
-        }
-
-        boolean readTarHeader(InputStream instream, byte[] block) throws IOException {
-            final int got = readExactly(instream, block, 0, 512);
-            if (got == 0) return false;     // Clean EOF
-            if (got < 512) throw new IOException("Unable to read full block header");
-            mBytes += 512;
-            return true;
-        }
-
-        // overwrites 'info' fields based on the pax extended header
-        boolean readPaxExtendedHeader(InputStream instream, FileMetadata info)
-                throws IOException {
-            // We should never see a pax extended header larger than this
-            if (info.size > 32*1024) {
-                Slog.w(TAG, "Suspiciously large pax header size " + info.size
-                        + " - aborting");
-                throw new IOException("Sanity failure: pax header size " + info.size);
-            }
-
-            // read whole blocks, not just the content size
-            int numBlocks = (int)((info.size + 511) >> 9);
-            byte[] data = new byte[numBlocks * 512];
-            if (readExactly(instream, data, 0, data.length) < data.length) {
-                throw new IOException("Unable to read full pax header");
-            }
-            mBytes += data.length;
-
-            final int contentSize = (int) info.size;
-            int offset = 0;
-            do {
-                // extract the line at 'offset'
-                int eol = offset+1;
-                while (eol < contentSize && data[eol] != ' ') eol++;
-                if (eol >= contentSize) {
-                    // error: we just hit EOD looking for the end of the size field
-                    throw new IOException("Invalid pax data");
-                }
-                // eol points to the space between the count and the key
-                int linelen = (int) extractRadix(data, offset, eol - offset, 10);
-                int key = eol + 1;  // start of key=value
-                eol = offset + linelen - 1; // trailing LF
-                int value;
-                for (value = key+1; data[value] != '=' && value <= eol; value++);
-                if (value > eol) {
-                    throw new IOException("Invalid pax declaration");
-                }
-
-                // pax requires that key/value strings be in UTF-8
-                String keyStr = new String(data, key, value-key, "UTF-8");
-                // -1 to strip the trailing LF
-                String valStr = new String(data, value+1, eol-value-1, "UTF-8");
-
-                if ("path".equals(keyStr)) {
-                    info.path = valStr;
-                } else if ("size".equals(keyStr)) {
-                    info.size = Long.parseLong(valStr);
-                } else {
-                    if (DEBUG) Slog.i(TAG, "Unhandled pax key: " + key);
-                }
-
-                offset += linelen;
-            } while (offset < contentSize);
-
-            return true;
-        }
-
-        long extractRadix(byte[] data, int offset, int maxChars, int radix)
-                throws IOException {
-            long value = 0;
-            final int end = offset + maxChars;
-            for (int i = offset; i < end; i++) {
-                final byte b = data[i];
-                // Numeric fields in tar can terminate with either NUL or SPC
-                if (b == 0 || b == ' ') break;
-                if (b < '0' || b > ('0' + radix - 1)) {
-                    throw new IOException("Invalid number in header: '" + (char)b + "' for radix " + radix);
-                }
-                value = radix * value + (b - '0');
-            }
-            return value;
-        }
-
-        String extractString(byte[] data, int offset, int maxChars) throws IOException {
-            final int end = offset + maxChars;
-            int eos = offset;
-            // tar string fields terminate early with a NUL
-            while (eos < end && data[eos] != 0) eos++;
-            return new String(data, offset, eos-offset, "US-ASCII");
-        }
-
-        void sendStartRestore() {
-            if (mObserver != null) {
-                try {
-                    mObserver.onStartRestore();
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "full restore observer went away: startRestore");
-                    mObserver = null;
-                }
-            }
-        }
-
-        void sendOnRestorePackage(String name) {
-            if (mObserver != null) {
-                try {
-                    // TODO: use a more user-friendly name string
-                    mObserver.onRestorePackage(name);
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "full restore observer went away: restorePackage");
-                    mObserver = null;
-                }
-            }
-        }
-
-        void sendEndRestore() {
-            if (mObserver != null) {
-                try {
-                    mObserver.onEndRestore();
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "full restore observer went away: endRestore");
-                    mObserver = null;
-                }
-            }
-        }
-    }
-
     // ----- Restore handling -----
 
     // Old style: directly match the stored vs on device signature blocks
-    private static boolean signaturesMatch(Signature[] storedSigs, PackageInfo target) {
+    public static boolean signaturesMatch(Signature[] storedSigs, PackageInfo target) {
         if (target == null) {
             return false;
         }
@@ -8444,7 +2393,7 @@
     }
 
     // Used by both incremental and full restore
-    private void restoreWidgetData(String packageName, byte[] widgetData) {
+    public void restoreWidgetData(String packageName, byte[] widgetData) {
         // Apply the restored widget state and generate the ID update for the app
         // TODO: http://b/22388012
         if (MORE_DEBUG) {
@@ -8457,1301 +2406,7 @@
     // NEW UNIFIED RESTORE IMPLEMENTATION
     // *****************************
 
-    // states of the unified-restore state machine
-    enum UnifiedRestoreState {
-        INITIAL,
-        RUNNING_QUEUE,
-        RESTORE_KEYVALUE,
-        RESTORE_FULL,
-        RESTORE_FINISHED,
-        FINAL
-    }
-
-    class PerformUnifiedRestoreTask implements BackupRestoreTask {
-        // Transport we're working with to do the restore
-        private IBackupTransport mTransport;
-
-        // Where per-transport saved state goes
-        File mStateDir;
-
-        // Restore observer; may be null
-        private IRestoreObserver mObserver;
-
-        // BackuoManagerMonitor; may be null
-        private IBackupManagerMonitor mMonitor;
-
-        // Token identifying the dataset to the transport
-        private long mToken;
-
-        // When this is a restore-during-install, this is the token identifying the
-        // operation to the Package Manager, and we must ensure that we let it know
-        // when we're finished.
-        private int mPmToken;
-
-        // When this is restore-during-install, we need to tell the package manager
-        // whether we actually launched the app, because this affects notifications
-        // around externally-visible state transitions.
-        private boolean mDidLaunch;
-
-        // Is this a whole-system restore, i.e. are we establishing a new ancestral
-        // dataset to base future restore-at-install operations from?
-        private boolean mIsSystemRestore;
-
-        // If this is a single-package restore, what package are we interested in?
-        private PackageInfo mTargetPackage;
-
-        // In all cases, the calculated list of packages that we are trying to restore
-        private List<PackageInfo> mAcceptSet;
-
-        // Our bookkeeping about the ancestral dataset
-        private PackageManagerBackupAgent mPmAgent;
-
-        // Currently-bound backup agent for restore + restoreFinished purposes
-        private IBackupAgent mAgent;
-
-        // What sort of restore we're doing now
-        private RestoreDescription mRestoreDescription;
-
-        // The package we're currently restoring
-        private PackageInfo mCurrentPackage;
-
-        // Widget-related data handled as part of this restore operation
-        private byte[] mWidgetData;
-
-        // Number of apps restored in this pass
-        private int mCount;
-
-        // When did we start?
-        private long mStartRealtime;
-
-        // State machine progress
-        private UnifiedRestoreState mState;
-
-        // How are things going?
-        private int mStatus;
-
-        // Done?
-        private boolean mFinished;
-
-        // Key/value: bookkeeping about staged data and files for agent access
-        private File mBackupDataName;
-        private File mStageName;
-        private File mSavedStateName;
-        private File mNewStateName;
-        ParcelFileDescriptor mBackupData;
-        ParcelFileDescriptor mNewState;
-
-        private final int mEphemeralOpToken;
-
-        // Invariant: mWakelock is already held, and this task is responsible for
-        // releasing it at the end of the restore operation.
-        PerformUnifiedRestoreTask(IBackupTransport transport, IRestoreObserver observer,
-                IBackupManagerMonitor monitor, long restoreSetToken, PackageInfo targetPackage,
-                int pmToken, boolean isFullSystemRestore, String[] filterSet) {
-            mEphemeralOpToken = generateRandomIntegerToken();
-            mState = UnifiedRestoreState.INITIAL;
-            mStartRealtime = SystemClock.elapsedRealtime();
-
-            mTransport = transport;
-            mObserver = observer;
-            mMonitor = monitor;
-            mToken = restoreSetToken;
-            mPmToken = pmToken;
-            mTargetPackage = targetPackage;
-            mIsSystemRestore = isFullSystemRestore;
-            mFinished = false;
-            mDidLaunch = false;
-
-            if (targetPackage != null) {
-                // Single package restore
-                mAcceptSet = new ArrayList<>();
-                mAcceptSet.add(targetPackage);
-            } else {
-                // Everything possible, or a target set
-                if (filterSet == null) {
-                    // We want everything and a pony
-                    List<PackageInfo> apps =
-                            PackageManagerBackupAgent.getStorableApplications(mPackageManager);
-                    filterSet = packagesToNames(apps);
-                    if (DEBUG) {
-                        Slog.i(TAG, "Full restore; asking about " + filterSet.length + " apps");
-                    }
-                }
-
-                mAcceptSet = new ArrayList<>(filterSet.length);
-
-                // Pro tem, we insist on moving the settings provider package to last place.
-                // Keep track of whether it's in the list, and bump it down if so.  We also
-                // want to do the system package itself first if it's called for.
-                boolean hasSystem = false;
-                boolean hasSettings = false;
-                for (int i = 0; i < filterSet.length; i++) {
-                    try {
-                        PackageInfo info = mPackageManager.getPackageInfo(filterSet[i], 0);
-                        if ("android".equals(info.packageName)) {
-                            hasSystem = true;
-                            continue;
-                        }
-                        if (SETTINGS_PACKAGE.equals(info.packageName)) {
-                            hasSettings = true;
-                            continue;
-                        }
-
-                        if (appIsEligibleForBackup(info.applicationInfo)) {
-                            mAcceptSet.add(info);
-                        }
-                    } catch (NameNotFoundException e) {
-                        // requested package name doesn't exist; ignore it
-                    }
-                }
-                if (hasSystem) {
-                    try {
-                        mAcceptSet.add(0, mPackageManager.getPackageInfo("android", 0));
-                    } catch (NameNotFoundException e) {
-                        // won't happen; we know a priori that it's valid
-                    }
-                }
-                if (hasSettings) {
-                    try {
-                        mAcceptSet.add(mPackageManager.getPackageInfo(SETTINGS_PACKAGE, 0));
-                    } catch (NameNotFoundException e) {
-                        // this one is always valid too
-                    }
-                }
-            }
-
-            if (MORE_DEBUG) {
-                Slog.v(TAG, "Restore; accept set size is " + mAcceptSet.size());
-                for (PackageInfo info : mAcceptSet) {
-                    Slog.v(TAG, "   " + info.packageName);
-                }
-            }
-        }
-
-        private String[] packagesToNames(List<PackageInfo> apps) {
-            final int N = apps.size();
-            String[] names = new String[N];
-            for (int i = 0; i < N; i++) {
-                names[i] = apps.get(i).packageName;
-            }
-            return names;
-        }
-
-        // Execute one tick of whatever state machine the task implements
-        @Override
-        public void execute() {
-            if (MORE_DEBUG) Slog.v(TAG, "*** Executing restore step " + mState);
-            switch (mState) {
-                case INITIAL:
-                    startRestore();
-                    break;
-
-                case RUNNING_QUEUE:
-                    dispatchNextRestore();
-                    break;
-
-                case RESTORE_KEYVALUE:
-                    restoreKeyValue();
-                    break;
-
-                case RESTORE_FULL:
-                    restoreFull();
-                    break;
-
-                case RESTORE_FINISHED:
-                    restoreFinished();
-                    break;
-
-                case FINAL:
-                    if (!mFinished) finalizeRestore();
-                    else {
-                        Slog.e(TAG, "Duplicate finish");
-                    }
-                    mFinished = true;
-                    break;
-            }
-        }
-
-        /*
-         * SKETCH OF OPERATION
-         *
-         * create one of these PerformUnifiedRestoreTask objects, telling it which
-         * dataset & transport to address, and then parameters within the restore
-         * operation: single target package vs many, etc.
-         *
-         * 1. transport.startRestore(token, list-of-packages).  If we need @pm@  it is
-         * always placed first and the settings provider always placed last [for now].
-         *
-         * 1a [if we needed @pm@ then nextRestorePackage() and restore the PMBA inline]
-         *
-         *   [ state change => RUNNING_QUEUE ]
-         *
-         * NOW ITERATE:
-         *
-         * { 3. t.nextRestorePackage()
-         *   4. does the metadata for this package allow us to restore it?
-         *      does the on-disk app permit us to restore it? [re-check allowBackup etc]
-         *   5. is this a key/value dataset?  => key/value agent restore
-         *       [ state change => RESTORE_KEYVALUE ]
-         *       5a. spin up agent
-         *       5b. t.getRestoreData() to stage it properly
-         *       5c. call into agent to perform restore
-         *       5d. tear down agent
-         *       [ state change => RUNNING_QUEUE ]
-         *
-         *   6. else it's a stream dataset:
-         *       [ state change => RESTORE_FULL ]
-         *       6a. instantiate the engine for a stream restore: engine handles agent lifecycles
-         *       6b. spin off engine runner on separate thread
-         *       6c. ITERATE getNextFullRestoreDataChunk() and copy data to engine runner socket
-         *       [ state change => RUNNING_QUEUE ]
-         * }
-         *
-         *   [ state change => FINAL ]
-         *
-         * 7. t.finishRestore(), release wakelock, etc.
-         *
-         *
-         */
-
-        // state INITIAL : set up for the restore and read the metadata if necessary
-        private  void startRestore() {
-            sendStartRestore(mAcceptSet.size());
-
-            // If we're starting a full-system restore, set up to begin widget ID remapping
-            if (mIsSystemRestore) {
-                // TODO: http://b/22388012
-                AppWidgetBackupBridge.restoreStarting(UserHandle.USER_SYSTEM);
-            }
-
-            try {
-                String transportDir = mTransport.transportDirName();
-                mStateDir = new File(mBaseStateDir, transportDir);
-
-                // Fetch the current metadata from the dataset first
-                PackageInfo pmPackage = new PackageInfo();
-                pmPackage.packageName = PACKAGE_MANAGER_SENTINEL;
-                mAcceptSet.add(0, pmPackage);
-
-                PackageInfo[] packages = mAcceptSet.toArray(new PackageInfo[0]);
-                mStatus = mTransport.startRestore(mToken, packages);
-                if (mStatus != BackupTransport.TRANSPORT_OK) {
-                    Slog.e(TAG, "Transport error " + mStatus + "; no restore possible");
-                    mStatus = BackupTransport.TRANSPORT_ERROR;
-                    executeNextState(UnifiedRestoreState.FINAL);
-                    return;
-                }
-
-                RestoreDescription desc = mTransport.nextRestorePackage();
-                if (desc == null) {
-                    Slog.e(TAG, "No restore metadata available; halting");
-                    mMonitor = monitorEvent(mMonitor,
-                            BackupManagerMonitor.LOG_EVENT_ID_NO_RESTORE_METADATA_AVAILABLE,
-                            mCurrentPackage,
-                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
-                    mStatus = BackupTransport.TRANSPORT_ERROR;
-                    executeNextState(UnifiedRestoreState.FINAL);
-                    return;
-                }
-                if (!PACKAGE_MANAGER_SENTINEL.equals(desc.getPackageName())) {
-                    Slog.e(TAG, "Required package metadata but got "
-                            + desc.getPackageName());
-                    mMonitor = monitorEvent(mMonitor,
-                            BackupManagerMonitor.LOG_EVENT_ID_NO_PM_METADATA_RECEIVED,
-                            mCurrentPackage,
-                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
-                    mStatus = BackupTransport.TRANSPORT_ERROR;
-                    executeNextState(UnifiedRestoreState.FINAL);
-                    return;
-                }
-
-                // Pull the Package Manager metadata from the restore set first
-                mCurrentPackage = new PackageInfo();
-                mCurrentPackage.packageName = PACKAGE_MANAGER_SENTINEL;
-                mPmAgent = new PackageManagerBackupAgent(mPackageManager, null);
-                mAgent = IBackupAgent.Stub.asInterface(mPmAgent.onBind());
-                if (MORE_DEBUG) {
-                    Slog.v(TAG, "initiating restore for PMBA");
-                }
-                initiateOneRestore(mCurrentPackage, 0);
-                // The PM agent called operationComplete() already, because our invocation
-                // of it is process-local and therefore synchronous.  That means that the
-                // next-state message (RUNNING_QUEUE) is already enqueued.  Only if we're
-                // unable to proceed with running the queue do we remove that pending
-                // message and jump straight to the FINAL state.  Because this was
-                // synchronous we also know that we should cancel the pending timeout
-                // message.
-                mBackupHandler.removeMessages(MSG_RESTORE_OPERATION_TIMEOUT);
-
-                // Verify that the backup set includes metadata.  If not, we can't do
-                // signature/version verification etc, so we simply do not proceed with
-                // the restore operation.
-                if (!mPmAgent.hasMetadata()) {
-                    Slog.e(TAG, "PM agent has no metadata, so not restoring");
-                    mMonitor = monitorEvent(mMonitor,
-                            BackupManagerMonitor.LOG_EVENT_ID_PM_AGENT_HAS_NO_METADATA,
-                            mCurrentPackage,
-                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
-                    EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
-                            PACKAGE_MANAGER_SENTINEL,
-                            "Package manager restore metadata missing");
-                    mStatus = BackupTransport.TRANSPORT_ERROR;
-                    mBackupHandler.removeMessages(MSG_BACKUP_RESTORE_STEP, this);
-                    executeNextState(UnifiedRestoreState.FINAL);
-                    return;
-                }
-
-                // Success; cache the metadata and continue as expected with the
-                // next state already enqueued
-
-            } catch (Exception e) {
-                // If we lost the transport at any time, halt
-                Slog.e(TAG, "Unable to contact transport for restore: " + e.getMessage());
-                mMonitor = monitorEvent(mMonitor,
-                        BackupManagerMonitor.LOG_EVENT_ID_LOST_TRANSPORT,
-                        null,
-                        BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT, null);
-                mStatus = BackupTransport.TRANSPORT_ERROR;
-                mBackupHandler.removeMessages(MSG_BACKUP_RESTORE_STEP, this);
-                executeNextState(UnifiedRestoreState.FINAL);
-                return;
-            }
-        }
-
-        // state RUNNING_QUEUE : figure out what the next thing to be restored is,
-        // and fire the appropriate next step
-        private void dispatchNextRestore() {
-            UnifiedRestoreState nextState = UnifiedRestoreState.FINAL;
-            try {
-                mRestoreDescription = mTransport.nextRestorePackage();
-                final String pkgName = (mRestoreDescription != null)
-                        ? mRestoreDescription.getPackageName() : null;
-                if (pkgName == null) {
-                    Slog.e(TAG, "Failure getting next package name");
-                    EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
-                    nextState = UnifiedRestoreState.FINAL;
-                    return;
-                } else if (mRestoreDescription == RestoreDescription.NO_MORE_PACKAGES) {
-                    // Yay we've reached the end cleanly
-                    if (DEBUG) {
-                        Slog.v(TAG, "No more packages; finishing restore");
-                    }
-                    int millis = (int) (SystemClock.elapsedRealtime() - mStartRealtime);
-                    EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, mCount, millis);
-                    nextState = UnifiedRestoreState.FINAL;
-                    return;
-                }
-
-                if (DEBUG) {
-                    Slog.i(TAG, "Next restore package: " + mRestoreDescription);
-                }
-                sendOnRestorePackage(pkgName);
-
-                Metadata metaInfo = mPmAgent.getRestoredMetadata(pkgName);
-                if (metaInfo == null) {
-                    Slog.e(TAG, "No metadata for " + pkgName);
-                    EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, pkgName,
-                            "Package metadata missing");
-                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
-                    return;
-                }
-
-                try {
-                    mCurrentPackage = mPackageManager.getPackageInfo(
-                            pkgName, PackageManager.GET_SIGNATURES);
-                } catch (NameNotFoundException e) {
-                    // Whoops, we thought we could restore this package but it
-                    // turns out not to be present.  Skip it.
-                    Slog.e(TAG, "Package not present: " + pkgName);
-                    mMonitor = monitorEvent(mMonitor,
-                            BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_NOT_PRESENT,
-                            mCurrentPackage,
-                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                            null);
-                    EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, pkgName,
-                            "Package missing on device");
-                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
-                    return;
-                }
-
-                if (metaInfo.versionCode > mCurrentPackage.versionCode) {
-                    // Data is from a "newer" version of the app than we have currently
-                    // installed.  If the app has not declared that it is prepared to
-                    // handle this case, we do not attempt the restore.
-                    if ((mCurrentPackage.applicationInfo.flags
-                            & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) == 0) {
-                        String message = "Source version " + metaInfo.versionCode
-                                + " > installed version " + mCurrentPackage.versionCode;
-                        Slog.w(TAG, "Package " + pkgName + ": " + message);
-                        Bundle monitoringExtras = putMonitoringExtra(null,
-                                BackupManagerMonitor.EXTRA_LOG_RESTORE_VERSION,
-                                metaInfo.versionCode);
-                        monitoringExtras = putMonitoringExtra(monitoringExtras,
-                                BackupManagerMonitor.EXTRA_LOG_RESTORE_ANYWAY, false);
-                        mMonitor = monitorEvent(mMonitor,
-                                BackupManagerMonitor.LOG_EVENT_ID_RESTORE_VERSION_HIGHER,
-                                mCurrentPackage,
-                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                monitoringExtras);
-                        EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
-                                pkgName, message);
-                        nextState = UnifiedRestoreState.RUNNING_QUEUE;
-                        return;
-                    } else {
-                        if (DEBUG) Slog.v(TAG, "Source version " + metaInfo.versionCode
-                                + " > installed version " + mCurrentPackage.versionCode
-                                + " but restoreAnyVersion");
-                        Bundle monitoringExtras = putMonitoringExtra(null,
-                                BackupManagerMonitor.EXTRA_LOG_RESTORE_VERSION,
-                                metaInfo.versionCode);
-                        monitoringExtras = putMonitoringExtra(monitoringExtras,
-                                BackupManagerMonitor.EXTRA_LOG_RESTORE_ANYWAY, true);
-                        mMonitor = monitorEvent(mMonitor,
-                                BackupManagerMonitor.LOG_EVENT_ID_RESTORE_VERSION_HIGHER,
-                                mCurrentPackage,
-                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
-                                monitoringExtras);
-                    }
-                }
-
-                if (MORE_DEBUG) Slog.v(TAG, "Package " + pkgName
-                        + " restore version [" + metaInfo.versionCode
-                        + "] is compatible with installed version ["
-                        + mCurrentPackage.versionCode + "]");
-
-                // Reset per-package preconditions and fire the appropriate next state
-                mWidgetData = null;
-                final int type = mRestoreDescription.getDataType();
-                if (type == RestoreDescription.TYPE_KEY_VALUE) {
-                    nextState = UnifiedRestoreState.RESTORE_KEYVALUE;
-                } else if (type == RestoreDescription.TYPE_FULL_STREAM) {
-                    nextState = UnifiedRestoreState.RESTORE_FULL;
-                } else {
-                    // Unknown restore type; ignore this package and move on
-                    Slog.e(TAG, "Unrecognized restore type " + type);
-                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
-                    return;
-                }
-            } catch (Exception e) {
-                Slog.e(TAG, "Can't get next restore target from transport; halting: "
-                        + e.getMessage());
-                EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
-                nextState = UnifiedRestoreState.FINAL;
-                return;
-            } finally {
-                executeNextState(nextState);
-            }
-        }
-
-        // state RESTORE_KEYVALUE : restore one package via key/value API set
-        private void restoreKeyValue() {
-            // Initiating the restore will pass responsibility for the state machine's
-            // progress to the agent callback, so we do not always execute the
-            // next state here.
-            final String packageName = mCurrentPackage.packageName;
-            // Validate some semantic requirements that apply in this way
-            // only to the key/value restore API flow
-            if (mCurrentPackage.applicationInfo.backupAgentName == null
-                    || "".equals(mCurrentPackage.applicationInfo.backupAgentName)) {
-                if (MORE_DEBUG) {
-                    Slog.i(TAG, "Data exists for package " + packageName
-                            + " but app has no agent; skipping");
-                }
-                mMonitor = monitorEvent(mMonitor,
-                        BackupManagerMonitor.LOG_EVENT_ID_APP_HAS_NO_AGENT, mCurrentPackage,
-                        BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
-                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
-                        "Package has no agent");
-                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
-                return;
-            }
-
-            Metadata metaInfo = mPmAgent.getRestoredMetadata(packageName);
-            if (!BackupUtils.signaturesMatch(metaInfo.sigHashes, mCurrentPackage)) {
-                Slog.w(TAG, "Signature mismatch restoring " + packageName);
-                mMonitor = monitorEvent(mMonitor,
-                        BackupManagerMonitor.LOG_EVENT_ID_SIGNATURE_MISMATCH, mCurrentPackage,
-                        BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
-                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
-                        "Signature mismatch");
-                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
-                return;
-            }
-
-            // Good to go!  Set up and bind the agent...
-            mAgent = bindToAgentSynchronous(
-                    mCurrentPackage.applicationInfo,
-                    ApplicationThreadConstants.BACKUP_MODE_INCREMENTAL);
-            if (mAgent == null) {
-                Slog.w(TAG, "Can't find backup agent for " + packageName);
-                mMonitor = monitorEvent(mMonitor,
-                        BackupManagerMonitor.LOG_EVENT_ID_CANT_FIND_AGENT, mCurrentPackage,
-                        BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
-                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
-                        "Restore agent missing");
-                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
-                return;
-            }
-
-            // Whatever happens next, we've launched the target app now; remember that.
-            mDidLaunch = true;
-
-            // And then finally start the restore on this agent
-            try {
-                initiateOneRestore(mCurrentPackage, metaInfo.versionCode);
-                ++mCount;
-            } catch (Exception e) {
-                Slog.e(TAG, "Error when attempting restore: " + e.toString());
-                keyValueAgentErrorCleanup();
-                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
-            }
-        }
-
-        // Guts of a key/value restore operation
-        void initiateOneRestore(PackageInfo app, int appVersionCode) {
-            final String packageName = app.packageName;
-
-            if (DEBUG) Slog.d(TAG, "initiateOneRestore packageName=" + packageName);
-
-            // !!! TODO: get the dirs from the transport
-            mBackupDataName = new File(mDataDir, packageName + ".restore");
-            mStageName = new File(mDataDir, packageName + ".stage");
-            mNewStateName = new File(mStateDir, packageName + ".new");
-            mSavedStateName = new File(mStateDir, packageName);
-
-            // don't stage the 'android' package where the wallpaper data lives.  this is
-            // an optimization: we know there's no widget data hosted/published by that
-            // package, and this way we avoid doing a spurious copy of MB-sized wallpaper
-            // data following the download.
-            boolean staging = !packageName.equals("android");
-            ParcelFileDescriptor stage;
-            File downloadFile = (staging) ? mStageName : mBackupDataName;
-
-            try {
-                // Run the transport's restore pass
-                stage = ParcelFileDescriptor.open(downloadFile,
-                        ParcelFileDescriptor.MODE_READ_WRITE |
-                        ParcelFileDescriptor.MODE_CREATE |
-                        ParcelFileDescriptor.MODE_TRUNCATE);
-
-                if (mTransport.getRestoreData(stage) != BackupTransport.TRANSPORT_OK) {
-                    // Transport-level failure, so we wind everything up and
-                    // terminate the restore operation.
-                    Slog.e(TAG, "Error getting restore data for " + packageName);
-                    EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
-                    stage.close();
-                    downloadFile.delete();
-                    executeNextState(UnifiedRestoreState.FINAL);
-                    return;
-                }
-
-                // We have the data from the transport. Now we extract and strip
-                // any per-package metadata (typically widget-related information)
-                // if appropriate
-                if (staging) {
-                    stage.close();
-                    stage = ParcelFileDescriptor.open(downloadFile,
-                            ParcelFileDescriptor.MODE_READ_ONLY);
-
-                    mBackupData = ParcelFileDescriptor.open(mBackupDataName,
-                            ParcelFileDescriptor.MODE_READ_WRITE |
-                            ParcelFileDescriptor.MODE_CREATE |
-                            ParcelFileDescriptor.MODE_TRUNCATE);
-
-                    BackupDataInput in = new BackupDataInput(stage.getFileDescriptor());
-                    BackupDataOutput out = new BackupDataOutput(mBackupData.getFileDescriptor());
-                    byte[] buffer = new byte[8192]; // will grow when needed
-                    while (in.readNextHeader()) {
-                        final String key = in.getKey();
-                        final int size = in.getDataSize();
-
-                        // is this a special key?
-                        if (key.equals(KEY_WIDGET_STATE)) {
-                            if (DEBUG) {
-                                Slog.i(TAG, "Restoring widget state for " + packageName);
-                            }
-                            mWidgetData = new byte[size];
-                            in.readEntityData(mWidgetData, 0, size);
-                        } else {
-                            if (size > buffer.length) {
-                                buffer = new byte[size];
-                            }
-                            in.readEntityData(buffer, 0, size);
-                            out.writeEntityHeader(key, size);
-                            out.writeEntityData(buffer, size);
-                        }
-                    }
-
-                    mBackupData.close();
-                }
-
-                // Okay, we have the data.  Now have the agent do the restore.
-                stage.close();
-
-                mBackupData = ParcelFileDescriptor.open(mBackupDataName,
-                        ParcelFileDescriptor.MODE_READ_ONLY);
-
-                mNewState = ParcelFileDescriptor.open(mNewStateName,
-                        ParcelFileDescriptor.MODE_READ_WRITE |
-                        ParcelFileDescriptor.MODE_CREATE |
-                        ParcelFileDescriptor.MODE_TRUNCATE);
-
-                // Kick off the restore, checking for hung agents.  The timeout or
-                // the operationComplete() callback will schedule the next step,
-                // so we do not do that here.
-                prepareOperationTimeout(mEphemeralOpToken, TIMEOUT_RESTORE_INTERVAL,
-                        this, OP_TYPE_RESTORE_WAIT);
-                mAgent.doRestore(mBackupData, appVersionCode, mNewState,
-                        mEphemeralOpToken, mBackupManagerBinder);
-            } catch (Exception e) {
-                Slog.e(TAG, "Unable to call app for restore: " + packageName, e);
-                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
-                        packageName, e.toString());
-                keyValueAgentErrorCleanup();    // clears any pending timeout messages as well
-
-                // After a restore failure we go back to running the queue.  If there
-                // are no more packages to be restored that will be handled by the
-                // next step.
-                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
-            }
-        }
-
-        // state RESTORE_FULL : restore one package via streaming engine
-        private void restoreFull() {
-            // None of this can run on the work looper here, so we spin asynchronous
-            // work like this:
-            //
-            //   StreamFeederThread: read data from mTransport.getNextFullRestoreDataChunk()
-            //                       write it into the pipe to the engine
-            //   EngineThread: FullRestoreEngine thread communicating with the target app
-            //
-            // When finished, StreamFeederThread executes next state as appropriate on the
-            // backup looper, and the overall unified restore task resumes
-            try {
-                StreamFeederThread feeder = new StreamFeederThread();
-                if (MORE_DEBUG) {
-                    Slog.i(TAG, "Spinning threads for stream restore of "
-                            + mCurrentPackage.packageName);
-                }
-                new Thread(feeder, "unified-stream-feeder").start();
-
-                // At this point the feeder is responsible for advancing the restore
-                // state, so we're done here.
-            } catch (IOException e) {
-                // Unable to instantiate the feeder thread -- we need to bail on the
-                // current target.  We haven't asked the transport for data yet, though,
-                // so we can do that simply by going back to running the restore queue.
-                Slog.e(TAG, "Unable to construct pipes for stream restore!");
-                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
-            }
-        }
-
-        // state RESTORE_FINISHED : provide the "no more data" signpost callback at the end
-        private void restoreFinished() {
-            try {
-                prepareOperationTimeout(mEphemeralOpToken, TIMEOUT_RESTORE_FINISHED_INTERVAL, this,
-                        OP_TYPE_RESTORE_WAIT);
-                mAgent.doRestoreFinished(mEphemeralOpToken, mBackupManagerBinder);
-                // If we get this far, the callback or timeout will schedule the
-                // next restore state, so we're done
-            } catch (Exception e) {
-                final String packageName = mCurrentPackage.packageName;
-                Slog.e(TAG, "Unable to finalize restore of " + packageName);
-                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
-                        packageName, e.toString());
-                keyValueAgentErrorCleanup();
-                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
-            }
-        }
-
-        class StreamFeederThread extends RestoreEngine implements Runnable, BackupRestoreTask {
-            final String TAG = "StreamFeederThread";
-            FullRestoreEngine mEngine;
-            EngineThread mEngineThread;
-
-            // pipe through which we read data from the transport. [0] read, [1] write
-            ParcelFileDescriptor[] mTransportPipes;
-
-            // pipe through which the engine will read data.  [0] read, [1] write
-            ParcelFileDescriptor[] mEnginePipes;
-
-            private final int mEphemeralOpToken;
-
-            public StreamFeederThread() throws IOException {
-                mEphemeralOpToken = generateRandomIntegerToken();
-                mTransportPipes = ParcelFileDescriptor.createPipe();
-                mEnginePipes = ParcelFileDescriptor.createPipe();
-                setRunning(true);
-            }
-
-            @Override
-            public void run() {
-                UnifiedRestoreState nextState = UnifiedRestoreState.RUNNING_QUEUE;
-                int status = BackupTransport.TRANSPORT_OK;
-
-                EventLog.writeEvent(EventLogTags.FULL_RESTORE_PACKAGE,
-                        mCurrentPackage.packageName);
-
-                mEngine = new FullRestoreEngine(this, null, mMonitor, mCurrentPackage, false, false, mEphemeralOpToken);
-                mEngineThread = new EngineThread(mEngine, mEnginePipes[0]);
-
-                ParcelFileDescriptor eWriteEnd = mEnginePipes[1];
-                ParcelFileDescriptor tReadEnd = mTransportPipes[0];
-                ParcelFileDescriptor tWriteEnd = mTransportPipes[1];
-
-                int bufferSize = 32 * 1024;
-                byte[] buffer = new byte[bufferSize];
-                FileOutputStream engineOut = new FileOutputStream(eWriteEnd.getFileDescriptor());
-                FileInputStream transportIn = new FileInputStream(tReadEnd.getFileDescriptor());
-
-                // spin up the engine and start moving data to it
-                new Thread(mEngineThread, "unified-restore-engine").start();
-
-                try {
-                    while (status == BackupTransport.TRANSPORT_OK) {
-                        // have the transport write some of the restoring data to us
-                        int result = mTransport.getNextFullRestoreDataChunk(tWriteEnd);
-                        if (result > 0) {
-                            // The transport wrote this many bytes of restore data to the
-                            // pipe, so pass it along to the engine.
-                            if (MORE_DEBUG) {
-                                Slog.v(TAG, "  <- transport provided chunk size " + result);
-                            }
-                            if (result > bufferSize) {
-                                bufferSize = result;
-                                buffer = new byte[bufferSize];
-                            }
-                            int toCopy = result;
-                            while (toCopy > 0) {
-                                int n = transportIn.read(buffer, 0, toCopy);
-                                engineOut.write(buffer, 0, n);
-                                toCopy -= n;
-                                if (MORE_DEBUG) {
-                                    Slog.v(TAG, "  -> wrote " + n + " to engine, left=" + toCopy);
-                                }
-                            }
-                        } else if (result == BackupTransport.NO_MORE_DATA) {
-                            // Clean finish.  Wind up and we're done!
-                            if (MORE_DEBUG) {
-                                Slog.i(TAG, "Got clean full-restore EOF for "
-                                        + mCurrentPackage.packageName);
-                            }
-                            status = BackupTransport.TRANSPORT_OK;
-                            break;
-                        } else {
-                            // Transport reported some sort of failure; the fall-through
-                            // handling will deal properly with that.
-                            Slog.e(TAG, "Error " + result + " streaming restore for "
-                                    + mCurrentPackage.packageName);
-                            EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
-                            status = result;
-                        }
-                    }
-                    if (MORE_DEBUG) Slog.v(TAG, "Done copying to engine, falling through");
-                } catch (IOException e) {
-                    // We lost our ability to communicate via the pipes.  That's worrying
-                    // but potentially recoverable; abandon this package's restore but
-                    // carry on with the next restore target.
-                    Slog.e(TAG, "Unable to route data for restore");
-                    EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
-                            mCurrentPackage.packageName, "I/O error on pipes");
-                    status = BackupTransport.AGENT_ERROR;
-                } catch (Exception e) {
-                    // The transport threw; terminate the whole operation.  Closing
-                    // the sockets will wake up the engine and it will then tidy up the
-                    // remote end.
-                    Slog.e(TAG, "Transport failed during restore: " + e.getMessage());
-                    EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
-                    status = BackupTransport.TRANSPORT_ERROR;
-                } finally {
-                    // Close the transport pipes and *our* end of the engine pipe,
-                    // but leave the engine thread's end open so that it properly
-                    // hits EOF and winds up its operations.
-                    IoUtils.closeQuietly(mEnginePipes[1]);
-                    IoUtils.closeQuietly(mTransportPipes[0]);
-                    IoUtils.closeQuietly(mTransportPipes[1]);
-
-                    // Don't proceed until the engine has wound up operations
-                    mEngineThread.waitForResult();
-
-                    // Now we're really done with this one too
-                    IoUtils.closeQuietly(mEnginePipes[0]);
-
-                    // In all cases we want to remember whether we launched
-                    // the target app as part of our work so far.
-                    mDidLaunch = (mEngine.getAgent() != null);
-
-                    // If we hit a transport-level error, we are done with everything;
-                    // if we hit an agent error we just go back to running the queue.
-                    if (status == BackupTransport.TRANSPORT_OK) {
-                        // Clean finish means we issue the restore-finished callback
-                        nextState = UnifiedRestoreState.RESTORE_FINISHED;
-
-                        // the engine bound the target's agent, so recover that binding
-                        // to use for the callback.
-                        mAgent = mEngine.getAgent();
-
-                        // and the restored widget data, if any
-                        mWidgetData = mEngine.getWidgetData();
-                    } else {
-                        // Something went wrong somewhere.  Whether it was at the transport
-                        // level is immaterial; we need to tell the transport to bail
-                        try {
-                            mTransport.abortFullRestore();
-                        } catch (Exception e) {
-                            // transport itself is dead; make sure we handle this as a
-                            // fatal error
-                            Slog.e(TAG, "Transport threw from abortFullRestore: " + e.getMessage());
-                            status = BackupTransport.TRANSPORT_ERROR;
-                        }
-
-                        // We also need to wipe the current target's data, as it's probably
-                        // in an incoherent state.
-                        clearApplicationDataSynchronous(mCurrentPackage.packageName);
-
-                        // Schedule the next state based on the nature of our failure
-                        if (status == BackupTransport.TRANSPORT_ERROR) {
-                            nextState = UnifiedRestoreState.FINAL;
-                        } else {
-                            nextState = UnifiedRestoreState.RUNNING_QUEUE;
-                        }
-                    }
-                    executeNextState(nextState);
-                    setRunning(false);
-                }
-            }
-
-            // BackupRestoreTask interface, specifically for timeout handling
-
-            @Override
-            public void execute() { /* intentionally empty */ }
-
-            @Override
-            public void operationComplete(long result) { /* intentionally empty */ }
-
-            // The app has timed out handling a restoring file
-            @Override
-            public void handleCancel(boolean cancelAll) {
-                removeOperation(mEphemeralOpToken);
-                if (DEBUG) {
-                    Slog.w(TAG, "Full-data restore target timed out; shutting down");
-                }
-
-                mMonitor = monitorEvent(mMonitor,
-                        BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_TIMEOUT,
-                        mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
-                mEngineThread.handleTimeout();
-
-                IoUtils.closeQuietly(mEnginePipes[1]);
-                mEnginePipes[1] = null;
-                IoUtils.closeQuietly(mEnginePipes[0]);
-                mEnginePipes[0] = null;
-            }
-        }
-
-        class EngineThread implements Runnable {
-            FullRestoreEngine mEngine;
-            FileInputStream mEngineStream;
-
-            EngineThread(FullRestoreEngine engine, ParcelFileDescriptor engineSocket) {
-                mEngine = engine;
-                engine.setRunning(true);
-                // We *do* want this FileInputStream to own the underlying fd, so that
-                // when we are finished with it, it closes this end of the pipe in a way
-                // that signals its other end.
-                mEngineStream = new FileInputStream(engineSocket.getFileDescriptor(), true);
-            }
-
-            public boolean isRunning() {
-                return mEngine.isRunning();
-            }
-
-            public int waitForResult() {
-                return mEngine.waitForResult();
-            }
-
-            @Override
-            public void run() {
-                try {
-                    while (mEngine.isRunning()) {
-                        // Tell it to be sure to leave the agent instance up after finishing
-                        mEngine.restoreOneFile(mEngineStream, false);
-                    }
-                } finally {
-                    // Because mEngineStream adopted its underlying FD, this also
-                    // closes this end of the pipe.
-                    IoUtils.closeQuietly(mEngineStream);
-                }
-            }
-
-            public void handleTimeout() {
-                IoUtils.closeQuietly(mEngineStream);
-                mEngine.handleTimeout();
-            }
-        }
-
-        // state FINAL : tear everything down and we're done.
-        private void finalizeRestore() {
-            if (MORE_DEBUG) Slog.d(TAG, "finishing restore mObserver=" + mObserver);
-
-            try {
-                mTransport.finishRestore();
-            } catch (Exception e) {
-                Slog.e(TAG, "Error finishing restore", e);
-            }
-
-            // Tell the observer we're done
-            if (mObserver != null) {
-                try {
-                    mObserver.restoreFinished(mStatus);
-                } catch (RemoteException e) {
-                    Slog.d(TAG, "Restore observer died at restoreFinished");
-                }
-            }
-
-            // Clear any ongoing session timeout.
-            mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
-
-            // If we have a PM token, we must under all circumstances be sure to
-            // handshake when we've finished.
-            if (mPmToken > 0) {
-                if (MORE_DEBUG) Slog.v(TAG, "finishing PM token " + mPmToken);
-                try {
-                    mPackageManagerBinder.finishPackageInstall(mPmToken, mDidLaunch);
-                } catch (RemoteException e) { /* can't happen */ }
-            } else {
-                // We were invoked via an active restore session, not by the Package
-                // Manager, so start up the session timeout again.
-                mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_SESSION_TIMEOUT,
-                        TIMEOUT_RESTORE_INTERVAL);
-            }
-
-            // Kick off any work that may be needed regarding app widget restores
-            // TODO: http://b/22388012
-            AppWidgetBackupBridge.restoreFinished(UserHandle.USER_SYSTEM);
-
-            // If this was a full-system restore, record the ancestral
-            // dataset information
-            if (mIsSystemRestore && mPmAgent != null) {
-                mAncestralPackages = mPmAgent.getRestoredPackages();
-                mAncestralToken = mToken;
-                writeRestoreTokens();
-            }
-
-            // done; we can finally release the wakelock and be legitimately done.
-            Slog.i(TAG, "Restore complete.");
-
-            synchronized (mPendingRestores) {
-                if (mPendingRestores.size() > 0) {
-                    if (DEBUG) {
-                        Slog.d(TAG, "Starting next pending restore.");
-                    }
-                    PerformUnifiedRestoreTask task = mPendingRestores.remove();
-                    mBackupHandler.sendMessage(
-                            mBackupHandler.obtainMessage(MSG_BACKUP_RESTORE_STEP, task));
-
-                } else {
-                    mIsRestoreInProgress = false;
-                    if (MORE_DEBUG) {
-                        Slog.d(TAG, "No pending restores.");
-                    }
-                }
-            }
-
-            mWakelock.release();
-        }
-
-        void keyValueAgentErrorCleanup() {
-            // If the agent fails restore, it might have put the app's data
-            // into an incoherent state.  For consistency we wipe its data
-            // again in this case before continuing with normal teardown
-            clearApplicationDataSynchronous(mCurrentPackage.packageName);
-            keyValueAgentCleanup();
-        }
-
-        // TODO: clean up naming; this is now used at finish by both k/v and stream restores
-        void keyValueAgentCleanup() {
-            mBackupDataName.delete();
-            mStageName.delete();
-            try { if (mBackupData != null) mBackupData.close(); } catch (IOException e) {}
-            try { if (mNewState != null) mNewState.close(); } catch (IOException e) {}
-            mBackupData = mNewState = null;
-
-            // if everything went okay, remember the recorded state now
-            //
-            // !!! TODO: the restored data could be migrated on the server
-            // side into the current dataset.  In that case the new state file
-            // we just created would reflect the data already extant in the
-            // backend, so there'd be nothing more to do.  Until that happens,
-            // however, we need to make sure that we record the data to the
-            // current backend dataset.  (Yes, this means shipping the data over
-            // the wire in both directions.  That's bad, but consistency comes
-            // first, then efficiency.)  Once we introduce server-side data
-            // migration to the newly-restored device's dataset, we will change
-            // the following from a discard of the newly-written state to the
-            // "correct" operation of renaming into the canonical state blob.
-            mNewStateName.delete();                      // TODO: remove; see above comment
-            //mNewStateName.renameTo(mSavedStateName);   // TODO: replace with this
-
-            // If this wasn't the PM pseudopackage, tear down the agent side
-            if (mCurrentPackage.applicationInfo != null) {
-                // unbind and tidy up even on timeout or failure
-                try {
-                    mActivityManager.unbindBackupAgent(mCurrentPackage.applicationInfo);
-
-                    // The agent was probably running with a stub Application object,
-                    // which isn't a valid run mode for the main app logic.  Shut
-                    // down the app so that next time it's launched, it gets the
-                    // usual full initialization.  Note that this is only done for
-                    // full-system restores: when a single app has requested a restore,
-                    // it is explicitly not killed following that operation.
-                    //
-                    // We execute this kill when these conditions hold:
-                    //    1. it's not a system-uid process,
-                    //    2. the app did not request its own restore (mTargetPackage == null), and either
-                    //    3a. the app is a full-data target (TYPE_FULL_STREAM) or
-                    //     b. the app does not state android:killAfterRestore="false" in its manifest
-                    final int appFlags = mCurrentPackage.applicationInfo.flags;
-                    final boolean killAfterRestore =
-                            (mCurrentPackage.applicationInfo.uid >= Process.FIRST_APPLICATION_UID)
-                            && ((mRestoreDescription.getDataType() == RestoreDescription.TYPE_FULL_STREAM)
-                                    || ((appFlags & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0));
-
-                    if (mTargetPackage == null && killAfterRestore) {
-                        if (DEBUG) Slog.d(TAG, "Restore complete, killing host process of "
-                                + mCurrentPackage.applicationInfo.processName);
-                        mActivityManager.killApplicationProcess(
-                                mCurrentPackage.applicationInfo.processName,
-                                mCurrentPackage.applicationInfo.uid);
-                    }
-                } catch (RemoteException e) {
-                    // can't happen; we run in the same process as the activity manager
-                }
-            }
-
-            // The caller is responsible for reestablishing the state machine; our
-            // responsibility here is to clear the decks for whatever comes next.
-            mBackupHandler.removeMessages(MSG_RESTORE_OPERATION_TIMEOUT, this);
-        }
-
-        @Override
-        public void operationComplete(long unusedResult) {
-            removeOperation(mEphemeralOpToken);
-            if (MORE_DEBUG) {
-                Slog.i(TAG, "operationComplete() during restore: target="
-                        + mCurrentPackage.packageName
-                        + " state=" + mState);
-            }
-
-            final UnifiedRestoreState nextState;
-            switch (mState) {
-                case INITIAL:
-                    // We've just (manually) restored the PMBA.  It doesn't need the
-                    // additional restore-finished callback so we bypass that and go
-                    // directly to running the queue.
-                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
-                    break;
-
-                case RESTORE_KEYVALUE:
-                case RESTORE_FULL: {
-                    // Okay, we've just heard back from the agent that it's done with
-                    // the restore itself.  We now have to send the same agent its
-                    // doRestoreFinished() callback, so roll into that state.
-                    nextState = UnifiedRestoreState.RESTORE_FINISHED;
-                    break;
-                }
-
-                case RESTORE_FINISHED: {
-                    // Okay, we're done with this package.  Tidy up and go on to the next
-                    // app in the queue.
-                    int size = (int) mBackupDataName.length();
-                    EventLog.writeEvent(EventLogTags.RESTORE_PACKAGE,
-                            mCurrentPackage.packageName, size);
-
-                    // Just go back to running the restore queue
-                    keyValueAgentCleanup();
-
-                    // If there was widget state associated with this app, get the OS to
-                    // incorporate it into current bookeeping and then pass that along to
-                    // the app as part of the restore-time work.
-                    if (mWidgetData != null) {
-                        restoreWidgetData(mCurrentPackage.packageName, mWidgetData);
-                    }
-
-                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
-                    break;
-                }
-
-                default: {
-                    // Some kind of horrible semantic error; we're in an unexpected state.
-                    // Back off hard and wind up.
-                    Slog.e(TAG, "Unexpected restore callback into state " + mState);
-                    keyValueAgentErrorCleanup();
-                    nextState = UnifiedRestoreState.FINAL;
-                    break;
-                }
-            }
-
-            executeNextState(nextState);
-        }
-
-        // A call to agent.doRestore() or agent.doRestoreFinished() has timed out
-        @Override
-        public void handleCancel(boolean cancelAll) {
-            removeOperation(mEphemeralOpToken);
-            Slog.e(TAG, "Timeout restoring application " + mCurrentPackage.packageName);
-            mMonitor = monitorEvent(mMonitor,
-                    BackupManagerMonitor.LOG_EVENT_ID_KEY_VALUE_RESTORE_TIMEOUT,
-                    mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
-            EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
-                    mCurrentPackage.packageName, "restore timeout");
-            // Handle like an agent that threw on invocation: wipe it and go on to the next
-            keyValueAgentErrorCleanup();
-            executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
-        }
-
-        void executeNextState(UnifiedRestoreState nextState) {
-            if (MORE_DEBUG) Slog.i(TAG, " => executing next step on "
-                    + this + " nextState=" + nextState);
-            mState = nextState;
-            Message msg = mBackupHandler.obtainMessage(MSG_BACKUP_RESTORE_STEP, this);
-            mBackupHandler.sendMessage(msg);
-        }
-
-        // restore observer support
-        void sendStartRestore(int numPackages) {
-            if (mObserver != null) {
-                try {
-                    mObserver.restoreStarting(numPackages);
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "Restore observer went away: startRestore");
-                    mObserver = null;
-                }
-            }
-        }
-
-        void sendOnRestorePackage(String name) {
-            if (mObserver != null) {
-                if (mObserver != null) {
-                    try {
-                        mObserver.onUpdate(mCount, name);
-                    } catch (RemoteException e) {
-                        Slog.d(TAG, "Restore observer died in onUpdate");
-                        mObserver = null;
-                    }
-                }
-            }
-        }
-
-        void sendEndRestore() {
-            if (mObserver != null) {
-                try {
-                    mObserver.restoreFinished(mStatus);
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "Restore observer went away: endRestore");
-                    mObserver = null;
-                }
-            }
-        }
-    }
-
-    class PerformClearTask implements Runnable {
-        IBackupTransport mTransport;
-        PackageInfo mPackage;
-
-        PerformClearTask(IBackupTransport transport, PackageInfo packageInfo) {
-            mTransport = transport;
-            mPackage = packageInfo;
-        }
-
-        public void run() {
-            try {
-                // Clear the on-device backup state to ensure a full backup next time
-                File stateDir = new File(mBaseStateDir, mTransport.transportDirName());
-                File stateFile = new File(stateDir, mPackage.packageName);
-                stateFile.delete();
-
-                // Tell the transport to remove all the persistent storage for the app
-                // TODO - need to handle failures
-                mTransport.clearBackupData(mPackage);
-            } catch (Exception e) {
-                Slog.e(TAG, "Transport threw clearing data for " + mPackage + ": " + e.getMessage());
-            } finally {
-                try {
-                    // TODO - need to handle failures
-                    mTransport.finishBackup();
-                } catch (Exception e) {
-                    // Nothing we can do here, alas
-                    Slog.e(TAG, "Unable to mark clear operation finished: " + e.getMessage());
-                }
-
-                // Last but not least, release the cpu
-                mWakelock.release();
-            }
-        }
-    }
-
-    class PerformInitializeTask implements Runnable {
-        HashSet<String> mQueue;
-
-        PerformInitializeTask(HashSet<String> transportNames) {
-            mQueue = transportNames;
-        }
-
-        public void run() {
-            try {
-                for (String transportName : mQueue) {
-                    IBackupTransport transport =
-                            mTransportManager.getTransportBinder(transportName);
-                    if (transport == null) {
-                        Slog.e(TAG, "Requested init for " + transportName + " but not found");
-                        continue;
-                    }
-
-                    Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName);
-                    EventLog.writeEvent(EventLogTags.BACKUP_START, transport.transportDirName());
-                    long startRealtime = SystemClock.elapsedRealtime();
-                    int status = transport.initializeDevice();
-
-                    if (status == BackupTransport.TRANSPORT_OK) {
-                        status = transport.finishBackup();
-                    }
-
-                    // Okay, the wipe really happened.  Clean up our local bookkeeping.
-                    if (status == BackupTransport.TRANSPORT_OK) {
-                        Slog.i(TAG, "Device init successful");
-                        int millis = (int) (SystemClock.elapsedRealtime() - startRealtime);
-                        EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE);
-                        resetBackupState(new File(mBaseStateDir, transport.transportDirName()));
-                        EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis);
-                        synchronized (mQueueLock) {
-                            recordInitPendingLocked(false, transportName);
-                        }
-                    } else {
-                        // If this didn't work, requeue this one and try again
-                        // after a suitable interval
-                        Slog.e(TAG, "Transport error in initializeDevice()");
-                        EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)");
-                        synchronized (mQueueLock) {
-                            recordInitPendingLocked(true, transportName);
-                        }
-                        // do this via another alarm to make sure of the wakelock states
-                        long delay = transport.requestBackupTime();
-                        Slog.w(TAG, "Init failed on " + transportName + " resched in " + delay);
-                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
-                                System.currentTimeMillis() + delay, mRunInitIntent);
-                    }
-                }
-            } catch (Exception e) {
-                Slog.e(TAG, "Unexpected error performing init", e);
-            } finally {
-                // Done; release the wakelock
-                mWakelock.release();
-            }
-        }
-    }
-
-    private void dataChangedImpl(String packageName) {
+    public void dataChangedImpl(String packageName) {
         HashSet<String> targets = dataChangedTargets(packageName);
         dataChangedImpl(packageName, targets);
     }
@@ -9948,7 +2603,7 @@
         }
     }
 
-    private boolean deviceIsProvisioned() {
+    public boolean deviceIsProvisioned() {
         final ContentResolver resolver = mContext.getContentResolver();
         return (Settings.Global.getInt(resolver, Settings.Global.DEVICE_PROVISIONED, 0) != 0);
     }
@@ -10056,7 +2711,7 @@
             final long oldId = Binder.clearCallingIdentity();
             try {
                 CountDownLatch latch = new CountDownLatch(1);
-                PerformFullTransportBackupTask task = new PerformFullTransportBackupTask(null,
+                PerformFullTransportBackupTask task = new PerformFullTransportBackupTask(this, null,
                         pkgNames, false, null, latch, null, null, false /* userInitiated */);
                 // Acquiring wakelock for PerformFullTransportBackupTask before its start.
                 mWakelock.acquire();
@@ -10175,7 +2830,7 @@
         }
     }
 
-    private void signalAdbBackupRestoreCompletion(AdbParams params) {
+    public void signalAdbBackupRestoreCompletion(AdbParams params) {
         synchronized (params.latch) {
             params.latch.set(true);
             params.latch.notifyAll();
@@ -10708,14 +3363,14 @@
                 Slog.i(TAG, "Restore session requested but currently running backups");
                 return null;
             }
-            mActiveRestoreSession = new ActiveRestoreSession(packageName, transport);
+            mActiveRestoreSession = new ActiveRestoreSession(this, packageName, transport);
             mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_SESSION_TIMEOUT,
                     TIMEOUT_RESTORE_INTERVAL);
         }
         return mActiveRestoreSession;
     }
 
-    private void clearRestoreSession(ActiveRestoreSession currentSession) {
+    public void clearRestoreSession(ActiveRestoreSession currentSession) {
         synchronized(this) {
             if (currentSession != mActiveRestoreSession) {
                 Slog.e(TAG, "ending non-current restore session");
@@ -10794,344 +3449,6 @@
         }
     }
 
-    // ----- Restore session -----
-
-    class ActiveRestoreSession extends IRestoreSession.Stub {
-        private static final String TAG = "RestoreSession";
-
-        private String mPackageName;
-        private IBackupTransport mRestoreTransport = null;
-        RestoreSet[] mRestoreSets = null;
-        boolean mEnded = false;
-        boolean mTimedOut = false;
-
-        ActiveRestoreSession(String packageName, String transport) {
-            mPackageName = packageName;
-            mRestoreTransport = mTransportManager.getTransportBinder(transport);
-        }
-
-        public void markTimedOut() {
-            mTimedOut = true;
-        }
-
-        // --- Binder interface ---
-        public synchronized int getAvailableRestoreSets(IRestoreObserver observer,
-                IBackupManagerMonitor monitor) {
-            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
-                    "getAvailableRestoreSets");
-            if (observer == null) {
-                throw new IllegalArgumentException("Observer must not be null");
-            }
-
-            if (mEnded) {
-                throw new IllegalStateException("Restore session already ended");
-            }
-
-            if (mTimedOut) {
-                Slog.i(TAG, "Session already timed out");
-                return -1;
-            }
-
-            long oldId = Binder.clearCallingIdentity();
-            try {
-                if (mRestoreTransport == null) {
-                    Slog.w(TAG, "Null transport getting restore sets");
-                    return -1;
-                }
-
-                // We know we're doing legit work now, so halt the timeout
-                // until we're done.  It gets started again when the result
-                // comes in.
-                mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
-
-                // spin off the transport request to our service thread
-                mWakelock.acquire();
-                Message msg = mBackupHandler.obtainMessage(MSG_RUN_GET_RESTORE_SETS,
-                        new RestoreGetSetsParams(mRestoreTransport, this, observer,
-                                monitor));
-                mBackupHandler.sendMessage(msg);
-                return 0;
-            } catch (Exception e) {
-                Slog.e(TAG, "Error in getAvailableRestoreSets", e);
-                return -1;
-            } finally {
-                Binder.restoreCallingIdentity(oldId);
-            }
-        }
-
-        public synchronized int restoreAll(long token, IRestoreObserver observer,
-                IBackupManagerMonitor monitor) {
-            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
-                    "performRestore");
-
-            if (DEBUG) Slog.d(TAG, "restoreAll token=" + Long.toHexString(token)
-                    + " observer=" + observer);
-
-            if (mEnded) {
-                throw new IllegalStateException("Restore session already ended");
-            }
-
-            if (mTimedOut) {
-                Slog.i(TAG, "Session already timed out");
-                return -1;
-            }
-
-            if (mRestoreTransport == null || mRestoreSets == null) {
-                Slog.e(TAG, "Ignoring restoreAll() with no restore set");
-                return -1;
-            }
-
-            if (mPackageName != null) {
-                Slog.e(TAG, "Ignoring restoreAll() on single-package session");
-                return -1;
-            }
-
-            String dirName;
-            try {
-                dirName = mRestoreTransport.transportDirName();
-            } catch (Exception e) {
-                // Transport went AWOL; fail.
-                Slog.e(TAG, "Unable to get transport dir for restore: " + e.getMessage());
-                return -1;
-            }
-
-            synchronized (mQueueLock) {
-                for (int i = 0; i < mRestoreSets.length; i++) {
-                    if (token == mRestoreSets[i].token) {
-                        // Real work, so stop the session timeout until we finalize the restore
-                        mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
-
-                        long oldId = Binder.clearCallingIdentity();
-                        mWakelock.acquire();
-                        if (MORE_DEBUG) {
-                            Slog.d(TAG, "restoreAll() kicking off");
-                        }
-                        Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
-                        msg.obj = new RestoreParams(mRestoreTransport, dirName,
-                                observer, monitor, token);
-                        mBackupHandler.sendMessage(msg);
-                        Binder.restoreCallingIdentity(oldId);
-                        return 0;
-                    }
-                }
-            }
-
-            Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found");
-            return -1;
-        }
-
-        // Restores of more than a single package are treated as 'system' restores
-        public synchronized int restoreSome(long token, IRestoreObserver observer,
-                IBackupManagerMonitor monitor, String[] packages) {
-            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
-                    "performRestore");
-
-            if (DEBUG) {
-                StringBuilder b = new StringBuilder(128);
-                b.append("restoreSome token=");
-                b.append(Long.toHexString(token));
-                b.append(" observer=");
-                b.append(observer.toString());
-                b.append(" monitor=");
-                if (monitor == null) {
-                    b.append("null");
-                } else {
-                    b.append(monitor.toString());
-                }
-                b.append(" packages=");
-                if (packages == null) {
-                    b.append("null");
-                } else {
-                    b.append('{');
-                    boolean first = true;
-                    for (String s : packages) {
-                        if (!first) {
-                            b.append(", ");
-                        } else first = false;
-                        b.append(s);
-                    }
-                    b.append('}');
-                }
-                Slog.d(TAG, b.toString());
-            }
-
-            if (mEnded) {
-                throw new IllegalStateException("Restore session already ended");
-            }
-
-            if (mTimedOut) {
-                Slog.i(TAG, "Session already timed out");
-                return -1;
-            }
-
-            if (mRestoreTransport == null || mRestoreSets == null) {
-                Slog.e(TAG, "Ignoring restoreAll() with no restore set");
-                return -1;
-            }
-
-            if (mPackageName != null) {
-                Slog.e(TAG, "Ignoring restoreAll() on single-package session");
-                return -1;
-            }
-
-            String dirName;
-            try {
-                dirName = mRestoreTransport.transportDirName();
-            } catch (Exception e) {
-                // Transport went AWOL; fail.
-                Slog.e(TAG, "Unable to get transport name for restoreSome: " + e.getMessage());
-                return -1;
-            }
-
-            synchronized (mQueueLock) {
-                for (int i = 0; i < mRestoreSets.length; i++) {
-                    if (token == mRestoreSets[i].token) {
-                        // Stop the session timeout until we finalize the restore
-                        mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
-
-                        long oldId = Binder.clearCallingIdentity();
-                        mWakelock.acquire();
-                        if (MORE_DEBUG) {
-                            Slog.d(TAG, "restoreSome() of " + packages.length + " packages");
-                        }
-                        Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
-                        msg.obj = new RestoreParams(mRestoreTransport, dirName, observer, monitor,
-                                token, packages, packages.length > 1);
-                        mBackupHandler.sendMessage(msg);
-                        Binder.restoreCallingIdentity(oldId);
-                        return 0;
-                    }
-                }
-            }
-
-            Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found");
-            return -1;
-        }
-
-        public synchronized int restorePackage(String packageName, IRestoreObserver observer,
-                IBackupManagerMonitor monitor) {
-            if (DEBUG) Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer
-                    + "monitor=" + monitor);
-
-            if (mEnded) {
-                throw new IllegalStateException("Restore session already ended");
-            }
-
-            if (mTimedOut) {
-                Slog.i(TAG, "Session already timed out");
-                return -1;
-            }
-
-            if (mPackageName != null) {
-                if (! mPackageName.equals(packageName)) {
-                    Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName
-                            + " on session for package " + mPackageName);
-                    return -1;
-                }
-            }
-
-            PackageInfo app = null;
-            try {
-                app = mPackageManager.getPackageInfo(packageName, 0);
-            } catch (NameNotFoundException nnf) {
-                Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName);
-                return -1;
-            }
-
-            // If the caller is not privileged and is not coming from the target
-            // app's uid, throw a permission exception back to the caller.
-            int perm = mContext.checkPermission(android.Manifest.permission.BACKUP,
-                    Binder.getCallingPid(), Binder.getCallingUid());
-            if ((perm == PackageManager.PERMISSION_DENIED) &&
-                    (app.applicationInfo.uid != Binder.getCallingUid())) {
-                Slog.w(TAG, "restorePackage: bad packageName=" + packageName
-                        + " or calling uid=" + Binder.getCallingUid());
-                throw new SecurityException("No permission to restore other packages");
-            }
-
-            // So far so good; we're allowed to try to restore this package.
-            long oldId = Binder.clearCallingIdentity();
-            try {
-                // Check whether there is data for it in the current dataset, falling back
-                // to the ancestral dataset if not.
-                long token = getAvailableRestoreToken(packageName);
-                if (DEBUG) Slog.v(TAG, "restorePackage pkg=" + packageName
-                        + " token=" + Long.toHexString(token));
-
-                // If we didn't come up with a place to look -- no ancestral dataset and
-                // the app has never been backed up from this device -- there's nothing
-                // to do but return failure.
-                if (token == 0) {
-                    if (DEBUG) Slog.w(TAG, "No data available for this package; not restoring");
-                    return -1;
-                }
-
-                String dirName;
-                try {
-                    dirName = mRestoreTransport.transportDirName();
-                } catch (Exception e) {
-                    // Transport went AWOL; fail.
-                    Slog.e(TAG, "Unable to get transport dir for restorePackage: " + e.getMessage());
-                    return -1;
-                }
-
-                // Stop the session timeout until we finalize the restore
-                mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
-
-                // Ready to go:  enqueue the restore request and claim success
-                mWakelock.acquire();
-                if (MORE_DEBUG) {
-                    Slog.d(TAG, "restorePackage() : " + packageName);
-                }
-                Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
-                msg.obj = new RestoreParams(mRestoreTransport, dirName, observer, monitor,
-                        token, app);
-                mBackupHandler.sendMessage(msg);
-            } finally {
-                Binder.restoreCallingIdentity(oldId);
-            }
-            return 0;
-        }
-
-        // Posted to the handler to tear down a restore session in a cleanly synchronized way
-        class EndRestoreRunnable implements Runnable {
-            RefactoredBackupManagerService mBackupManager;
-            ActiveRestoreSession mSession;
-
-            EndRestoreRunnable(RefactoredBackupManagerService manager, ActiveRestoreSession session) {
-                mBackupManager = manager;
-                mSession = session;
-            }
-
-            public void run() {
-                // clean up the session's bookkeeping
-                synchronized (mSession) {
-                    mSession.mRestoreTransport = null;
-                    mSession.mEnded = true;
-                }
-
-                // clean up the BackupManagerImpl side of the bookkeeping
-                // and cancel any pending timeout message
-                mBackupManager.clearRestoreSession(mSession);
-            }
-        }
-
-        public synchronized void endRestoreSession() {
-            if (DEBUG) Slog.d(TAG, "endRestoreSession");
-
-            if (mTimedOut) {
-                Slog.i(TAG, "Session already timed out");
-                return;
-            }
-
-            if (mEnded) {
-                throw new IllegalStateException("Restore session already ended");
-            }
-
-            mBackupHandler.post(new EndRestoreRunnable(RefactoredBackupManagerService.this, this));
-        }
-    }
-
     @Override
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return;
@@ -11261,8 +3578,8 @@
         }
     }
 
-    private static void sendBackupOnUpdate(IBackupObserver observer, String packageName,
-            BackupProgress progress) {
+    public static void sendBackupOnUpdate(IBackupObserver observer, String packageName,
+        BackupProgress progress) {
         if (observer != null) {
             try {
                 observer.onUpdate(packageName, progress);
@@ -11274,8 +3591,8 @@
         }
     }
 
-    private static void sendBackupOnPackageResult(IBackupObserver observer, String packageName,
-            int status) {
+    public static void sendBackupOnPackageResult(IBackupObserver observer, String packageName,
+        int status) {
         if (observer != null) {
             try {
                 observer.onResult(packageName, status);
@@ -11287,7 +3604,7 @@
         }
     }
 
-    private static void sendBackupFinished(IBackupObserver observer, int status) {
+    public static void sendBackupFinished(IBackupObserver observer, int status) {
         if (observer != null) {
             try {
                 observer.backupFinished(status);
@@ -11299,7 +3616,7 @@
         }
     }
 
-    private Bundle putMonitoringExtra(Bundle extras, String key, String value) {
+    public Bundle putMonitoringExtra(Bundle extras, String key, String value) {
         if (extras == null) {
             extras = new Bundle();
         }
@@ -11315,7 +3632,7 @@
         return extras;
     }
 
-    private Bundle putMonitoringExtra(Bundle extras, String key, long value) {
+    public Bundle putMonitoringExtra(Bundle extras, String key, long value) {
         if (extras == null) {
             extras = new Bundle();
         }
@@ -11324,7 +3641,7 @@
     }
 
 
-    private Bundle putMonitoringExtra(Bundle extras, String key, boolean value) {
+    public Bundle putMonitoringExtra(Bundle extras, String key, boolean value) {
         if (extras == null) {
             extras = new Bundle();
         }
@@ -11332,8 +3649,8 @@
         return extras;
     }
 
-    private static IBackupManagerMonitor monitorEvent(IBackupManagerMonitor monitor, int id,
-            PackageInfo pkg, int category, Bundle extras) {
+    public static IBackupManagerMonitor monitorEvent(IBackupManagerMonitor monitor, int id,
+        PackageInfo pkg, int category, Bundle extras) {
         if (monitor != null) {
             try {
                 Bundle bundle = new Bundle();
diff --git a/services/backup/java/com/android/server/backup/TransportManager.java b/services/backup/java/com/android/server/backup/TransportManager.java
index 67f105e..90caae8 100644
--- a/services/backup/java/com/android/server/backup/TransportManager.java
+++ b/services/backup/java/com/android/server/backup/TransportManager.java
@@ -53,7 +53,7 @@
 /**
  * Handles in-memory bookkeeping of all BackupTransport objects.
  */
-class TransportManager {
+public class TransportManager {
 
     private static final String TAG = "BackupTransportManager";
 
@@ -149,7 +149,7 @@
         }
     }
 
-    IBackupTransport getTransportBinder(String transportName) {
+    public IBackupTransport getTransportBinder(String transportName) {
         synchronized (mTransportLock) {
             ComponentName component = mBoundTransports.get(transportName);
             if (component == null) {
@@ -165,11 +165,11 @@
         }
     }
 
-    IBackupTransport getCurrentTransportBinder() {
+    public IBackupTransport getCurrentTransportBinder() {
         return getTransportBinder(mCurrentTransportName);
     }
 
-    String getTransportName(IBackupTransport binder) {
+    public String getTransportName(IBackupTransport binder) {
         synchronized (mTransportLock) {
             for (TransportConnection conn : mValidTransports.values()) {
                 if (conn.getBinder() == binder) {
diff --git a/services/backup/java/com/android/server/backup/fullbackup/FullBackupEngine.java b/services/backup/java/com/android/server/backup/fullbackup/FullBackupEngine.java
new file mode 100644
index 0000000..0f7644c
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/fullbackup/FullBackupEngine.java
@@ -0,0 +1,336 @@
+/*
+ * Copyright (C) 2017 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.server.backup.fullbackup;
+
+import android.app.ApplicationThreadConstants;
+import android.app.IBackupAgent;
+import android.app.backup.BackupTransport;
+import android.app.backup.FullBackup;
+import android.app.backup.FullBackupDataOutput;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.os.Environment.UserEnvironment;
+import android.os.ParcelFileDescriptor;
+import android.os.RemoteException;
+import android.os.UserHandle;
+import android.util.Log;
+import android.util.Slog;
+import android.util.StringBuilderPrinter;
+import com.android.server.AppWidgetBackupBridge;
+import com.android.server.backup.BackupRestoreTask;
+import com.android.server.backup.RefactoredBackupManagerService;
+import java.io.BufferedOutputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class FullBackupEngine {
+
+    private RefactoredBackupManagerService backupManagerService;
+    OutputStream mOutput;
+    FullBackupPreflight mPreflightHook;
+    BackupRestoreTask mTimeoutMonitor;
+    IBackupAgent mAgent;
+    File mFilesDir;
+    File mManifestFile;
+    File mMetadataFile;
+    boolean mIncludeApks;
+    PackageInfo mPkg;
+    private final long mQuota;
+    private final int mOpToken;
+
+    class FullBackupRunner implements Runnable {
+
+      PackageInfo mPackage;
+        byte[] mWidgetData;
+        IBackupAgent mAgent;
+        ParcelFileDescriptor mPipe;
+        int mToken;
+        boolean mSendApk;
+        boolean mWriteManifest;
+
+        FullBackupRunner(PackageInfo pack, IBackupAgent agent, ParcelFileDescriptor pipe,
+            int token, boolean sendApk, boolean writeManifest, byte[] widgetData)
+            throws IOException {
+            mPackage = pack;
+            mWidgetData = widgetData;
+            mAgent = agent;
+            mPipe = ParcelFileDescriptor.dup(pipe.getFileDescriptor());
+            mToken = token;
+            mSendApk = sendApk;
+            mWriteManifest = writeManifest;
+        }
+
+        @Override
+        public void run() {
+            try {
+                FullBackupDataOutput output = new FullBackupDataOutput(mPipe);
+
+                if (mWriteManifest) {
+                    final boolean writeWidgetData = mWidgetData != null;
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                      Slog.d(RefactoredBackupManagerService.TAG, "Writing manifest for " + mPackage.packageName);
+                    }
+                    RefactoredBackupManagerService
+                        .writeAppManifest(mPackage, backupManagerService.mPackageManager, mManifestFile, mSendApk,
+                            writeWidgetData);
+                    FullBackup.backupToTar(mPackage.packageName, null, null,
+                        mFilesDir.getAbsolutePath(),
+                        mManifestFile.getAbsolutePath(),
+                        output);
+                    mManifestFile.delete();
+
+                    // We only need to write a metadata file if we have widget data to stash
+                    if (writeWidgetData) {
+                        writeMetadata(mPackage, mMetadataFile, mWidgetData);
+                        FullBackup.backupToTar(mPackage.packageName, null, null,
+                            mFilesDir.getAbsolutePath(),
+                            mMetadataFile.getAbsolutePath(),
+                            output);
+                        mMetadataFile.delete();
+                    }
+                }
+
+                if (mSendApk) {
+                    writeApkToBackup(mPackage, output);
+                }
+
+                if (RefactoredBackupManagerService.DEBUG) {
+                  Slog.d(RefactoredBackupManagerService.TAG, "Calling doFullBackup() on " + mPackage.packageName);
+                }
+                backupManagerService
+                    .prepareOperationTimeout(mToken, RefactoredBackupManagerService.TIMEOUT_FULL_BACKUP_INTERVAL,
+                        mTimeoutMonitor /* in parent class */, RefactoredBackupManagerService.OP_TYPE_BACKUP_WAIT);
+                mAgent.doFullBackup(mPipe, mQuota, mToken, backupManagerService.mBackupManagerBinder);
+            } catch (IOException e) {
+                Slog.e(RefactoredBackupManagerService.TAG, "Error running full backup for " + mPackage.packageName);
+            } catch (RemoteException e) {
+                Slog.e(RefactoredBackupManagerService.TAG, "Remote agent vanished during full backup of "
+                    + mPackage.packageName);
+            } finally {
+                try {
+                    mPipe.close();
+                } catch (IOException e) {
+                }
+            }
+        }
+    }
+
+    public FullBackupEngine(RefactoredBackupManagerService backupManagerService,
+        OutputStream output,
+        FullBackupPreflight preflightHook, PackageInfo pkg,
+        boolean alsoApks, BackupRestoreTask timeoutMonitor, long quota, int opToken) {
+        this.backupManagerService = backupManagerService;
+        mOutput = output;
+        mPreflightHook = preflightHook;
+        mPkg = pkg;
+        mIncludeApks = alsoApks;
+        mTimeoutMonitor = timeoutMonitor;
+        mFilesDir = new File("/data/system");
+        mManifestFile = new File(mFilesDir, RefactoredBackupManagerService.BACKUP_MANIFEST_FILENAME);
+        mMetadataFile = new File(mFilesDir, RefactoredBackupManagerService.BACKUP_METADATA_FILENAME);
+        mQuota = quota;
+        mOpToken = opToken;
+    }
+
+    public int preflightCheck() throws RemoteException {
+        if (mPreflightHook == null) {
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.v(RefactoredBackupManagerService.TAG, "No preflight check");
+            }
+            return BackupTransport.TRANSPORT_OK;
+        }
+        if (initializeAgent()) {
+            int result = mPreflightHook.preflightFullBackup(mPkg, mAgent);
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.v(RefactoredBackupManagerService.TAG, "preflight returned " + result);
+            }
+            return result;
+        } else {
+            Slog.w(RefactoredBackupManagerService.TAG, "Unable to bind to full agent for " + mPkg.packageName);
+            return BackupTransport.AGENT_ERROR;
+        }
+    }
+
+    public int backupOnePackage() throws RemoteException {
+        int result = BackupTransport.AGENT_ERROR;
+
+        if (initializeAgent()) {
+            ParcelFileDescriptor[] pipes = null;
+            try {
+                pipes = ParcelFileDescriptor.createPipe();
+
+                ApplicationInfo app = mPkg.applicationInfo;
+                final boolean isSharedStorage =
+                    mPkg.packageName.equals(
+                        RefactoredBackupManagerService.SHARED_BACKUP_AGENT_PACKAGE);
+                final boolean sendApk = mIncludeApks
+                    && !isSharedStorage
+                    && ((app.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) == 0)
+                    && ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0 ||
+                    (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0);
+
+                // TODO: http://b/22388012
+                byte[] widgetBlob = AppWidgetBackupBridge.getWidgetState(mPkg.packageName,
+                    UserHandle.USER_SYSTEM);
+
+                FullBackupRunner runner = new FullBackupRunner(mPkg, mAgent, pipes[1],
+                    mOpToken, sendApk, !isSharedStorage, widgetBlob);
+                pipes[1].close();   // the runner has dup'd it
+                pipes[1] = null;
+                Thread t = new Thread(runner, "app-data-runner");
+                t.start();
+
+                // Now pull data from the app and stuff it into the output
+                RefactoredBackupManagerService.routeSocketDataToOutput(pipes[0], mOutput);
+
+                if (!backupManagerService.waitUntilOperationComplete(mOpToken)) {
+                    Slog.e(RefactoredBackupManagerService.TAG, "Full backup failed on package " + mPkg.packageName);
+                } else {
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                        Slog.d(RefactoredBackupManagerService.TAG, "Full package backup success: " + mPkg.packageName);
+                    }
+                    result = BackupTransport.TRANSPORT_OK;
+                }
+            } catch (IOException e) {
+                Slog.e(RefactoredBackupManagerService.TAG, "Error backing up " + mPkg.packageName + ": " + e.getMessage());
+                result = BackupTransport.AGENT_ERROR;
+            } finally {
+                try {
+                    // flush after every package
+                    mOutput.flush();
+                    if (pipes != null) {
+                        if (pipes[0] != null) {
+                          pipes[0].close();
+                        }
+                        if (pipes[1] != null) {
+                          pipes[1].close();
+                        }
+                    }
+                } catch (IOException e) {
+                    Slog.w(RefactoredBackupManagerService.TAG, "Error bringing down backup stack");
+                    result = BackupTransport.TRANSPORT_ERROR;
+                }
+            }
+        } else {
+            Slog.w(RefactoredBackupManagerService.TAG, "Unable to bind to full agent for " + mPkg.packageName);
+        }
+        tearDown();
+        return result;
+    }
+
+    public void sendQuotaExceeded(final long backupDataBytes, final long quotaBytes) {
+        if (initializeAgent()) {
+            try {
+                mAgent.doQuotaExceeded(backupDataBytes, quotaBytes);
+            } catch (RemoteException e) {
+                Slog.e(RefactoredBackupManagerService.TAG, "Remote exception while telling agent about quota exceeded");
+            }
+        }
+    }
+
+    private boolean initializeAgent() {
+        if (mAgent == null) {
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.d(RefactoredBackupManagerService.TAG, "Binding to full backup agent : " + mPkg.packageName);
+            }
+            mAgent = backupManagerService.bindToAgentSynchronous(mPkg.applicationInfo,
+                ApplicationThreadConstants.BACKUP_MODE_FULL);
+        }
+        return mAgent != null;
+    }
+
+    private void writeApkToBackup(PackageInfo pkg, FullBackupDataOutput output) {
+        // Forward-locked apps, system-bundled .apks, etc are filtered out before we get here
+        // TODO: handle backing up split APKs
+        final String appSourceDir = pkg.applicationInfo.getBaseCodePath();
+        final String apkDir = new File(appSourceDir).getParent();
+        FullBackup.backupToTar(pkg.packageName, FullBackup.APK_TREE_TOKEN, null,
+            apkDir, appSourceDir, output);
+
+        // TODO: migrate this to SharedStorageBackup, since AID_SYSTEM
+        // doesn't have access to external storage.
+
+        // Save associated .obb content if it exists and we did save the apk
+        // check for .obb and save those too
+        // TODO: http://b/22388012
+        final UserEnvironment userEnv = new UserEnvironment(UserHandle.USER_SYSTEM);
+        final File obbDir = userEnv.buildExternalStorageAppObbDirs(pkg.packageName)[0];
+        if (obbDir != null) {
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+              Log.i(RefactoredBackupManagerService.TAG, "obb dir: " + obbDir.getAbsolutePath());
+            }
+            File[] obbFiles = obbDir.listFiles();
+            if (obbFiles != null) {
+                final String obbDirName = obbDir.getAbsolutePath();
+                for (File obb : obbFiles) {
+                    FullBackup.backupToTar(pkg.packageName, FullBackup.OBB_TREE_TOKEN, null,
+                        obbDirName, obb.getAbsolutePath(), output);
+                }
+            }
+        }
+    }
+
+    // Widget metadata format. All header entries are strings ending in LF:
+    //
+    // Version 1 header:
+    //     BACKUP_METADATA_VERSION, currently "1"
+    //     package name
+    //
+    // File data (all integers are binary in network byte order)
+    // *N: 4 : integer token identifying which metadata blob
+    //     4 : integer size of this blob = N
+    //     N : raw bytes of this metadata blob
+    //
+    // Currently understood blobs (always in network byte order):
+    //
+    //     widgets : metadata token = 0x01FFED01 (BACKUP_WIDGET_METADATA_TOKEN)
+    //
+    // Unrecognized blobs are *ignored*, not errors.
+    private void writeMetadata(PackageInfo pkg, File destination, byte[] widgetData)
+        throws IOException {
+        StringBuilder b = new StringBuilder(512);
+        StringBuilderPrinter printer = new StringBuilderPrinter(b);
+        printer.println(Integer.toString(RefactoredBackupManagerService.BACKUP_METADATA_VERSION));
+        printer.println(pkg.packageName);
+
+        FileOutputStream fout = new FileOutputStream(destination);
+        BufferedOutputStream bout = new BufferedOutputStream(fout);
+        DataOutputStream out = new DataOutputStream(bout);
+        bout.write(b.toString().getBytes());    // bypassing DataOutputStream
+
+        if (widgetData != null && widgetData.length > 0) {
+            out.writeInt(RefactoredBackupManagerService.BACKUP_WIDGET_METADATA_TOKEN);
+            out.writeInt(widgetData.length);
+            out.write(widgetData);
+        }
+        bout.flush();
+        out.close();
+
+        // As with the manifest file, guarantee idempotence of the archive metadata
+        // for the widget block by using a fixed mtime on the transient file.
+        destination.setLastModified(0);
+    }
+
+    private void tearDown() {
+        if (mPkg != null) {
+            backupManagerService.tearDownAgentAndKill(mPkg.applicationInfo);
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/fullbackup/FullBackupEntry.java b/services/backup/java/com/android/server/backup/fullbackup/FullBackupEntry.java
new file mode 100644
index 0000000..a62f1c0
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/fullbackup/FullBackupEntry.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2017 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.server.backup.fullbackup;
+
+public class FullBackupEntry implements Comparable<FullBackupEntry> {
+
+    public String packageName;
+    public long lastBackup;
+
+    public FullBackupEntry(String pkg, long when) {
+        packageName = pkg;
+        lastBackup = when;
+    }
+
+    @Override
+    public int compareTo(FullBackupEntry other) {
+        if (lastBackup < other.lastBackup) {
+            return -1;
+        } else if (lastBackup > other.lastBackup) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/fullbackup/FullBackupObbConnection.java b/services/backup/java/com/android/server/backup/fullbackup/FullBackupObbConnection.java
new file mode 100644
index 0000000..8ca27b5
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/fullbackup/FullBackupObbConnection.java
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2017 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.server.backup.fullbackup;
+
+import android.app.backup.IBackupManager;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.ServiceConnection;
+import android.content.pm.PackageInfo;
+import android.os.IBinder;
+import android.os.ParcelFileDescriptor;
+import android.os.UserHandle;
+import android.util.Slog;
+import com.android.internal.backup.IObbBackupService;
+import com.android.server.backup.RefactoredBackupManagerService;
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class FullBackupObbConnection implements ServiceConnection {
+
+    private RefactoredBackupManagerService backupManagerService;
+    volatile IObbBackupService mService;
+
+    public FullBackupObbConnection(RefactoredBackupManagerService backupManagerService) {
+        this.backupManagerService = backupManagerService;
+        mService = null;
+    }
+
+    public void establish() {
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+          Slog.i(RefactoredBackupManagerService.TAG, "Initiating bind of OBB service on " + this);
+        }
+        Intent obbIntent = new Intent().setComponent(new ComponentName(
+            "com.android.sharedstoragebackup",
+            "com.android.sharedstoragebackup.ObbBackupService"));
+        backupManagerService.mContext.bindServiceAsUser(
+            obbIntent, this, Context.BIND_AUTO_CREATE, UserHandle.SYSTEM);
+    }
+
+    public void tearDown() {
+        backupManagerService.mContext.unbindService(this);
+    }
+
+    public boolean backupObbs(PackageInfo pkg, OutputStream out) {
+        boolean success = false;
+        waitForConnection();
+
+        ParcelFileDescriptor[] pipes = null;
+        try {
+            pipes = ParcelFileDescriptor.createPipe();
+            int token = backupManagerService.generateRandomIntegerToken();
+            backupManagerService
+                .prepareOperationTimeout(token, RefactoredBackupManagerService.TIMEOUT_FULL_BACKUP_INTERVAL,
+                    null, RefactoredBackupManagerService.OP_TYPE_BACKUP_WAIT);
+            mService.backupObbs(pkg.packageName, pipes[1], token, backupManagerService.mBackupManagerBinder);
+            RefactoredBackupManagerService.routeSocketDataToOutput(pipes[0], out);
+            success = backupManagerService.waitUntilOperationComplete(token);
+        } catch (Exception e) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Unable to back up OBBs for " + pkg, e);
+        } finally {
+            try {
+                out.flush();
+                if (pipes != null) {
+                    if (pipes[0] != null) {
+                      pipes[0].close();
+                    }
+                    if (pipes[1] != null) {
+                      pipes[1].close();
+                    }
+                }
+            } catch (IOException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "I/O error closing down OBB backup", e);
+            }
+        }
+        return success;
+    }
+
+    public void restoreObbFile(String pkgName, ParcelFileDescriptor data,
+        long fileSize, int type, String path, long mode, long mtime,
+        int token, IBackupManager callbackBinder) {
+        waitForConnection();
+
+        try {
+            mService.restoreObbFile(pkgName, data, fileSize, type, path, mode, mtime,
+                token, callbackBinder);
+        } catch (Exception e) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Unable to restore OBBs for " + pkgName, e);
+        }
+    }
+
+    private void waitForConnection() {
+        synchronized (this) {
+            while (mService == null) {
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                  Slog.i(RefactoredBackupManagerService.TAG, "...waiting for OBB service binding...");
+                }
+                try {
+                    this.wait();
+                } catch (InterruptedException e) { /* never interrupted */ }
+            }
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+              Slog.i(RefactoredBackupManagerService.TAG, "Connected to OBB service; continuing");
+            }
+        }
+    }
+
+    @Override
+    public void onServiceConnected(ComponentName name, IBinder service) {
+        synchronized (this) {
+            mService = IObbBackupService.Stub.asInterface(service);
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+              Slog.i(RefactoredBackupManagerService.TAG, "OBB service connection " + mService
+                  + " connected on " + this);
+            }
+            this.notifyAll();
+        }
+    }
+
+    @Override
+    public void onServiceDisconnected(ComponentName name) {
+        synchronized (this) {
+            mService = null;
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+              Slog.i(RefactoredBackupManagerService.TAG, "OBB service connection disconnected on " + this);
+            }
+            this.notifyAll();
+        }
+    }
+
+}
diff --git a/services/backup/java/com/android/server/backup/fullbackup/FullBackupPreflight.java b/services/backup/java/com/android/server/backup/fullbackup/FullBackupPreflight.java
new file mode 100644
index 0000000..1ed57d5
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/fullbackup/FullBackupPreflight.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2017 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.server.backup.fullbackup;
+
+import android.app.IBackupAgent;
+import android.content.pm.PackageInfo;
+
+/**
+ * Callout from the engine to an interested participant that might need to communicate with the
+ * agent prior to asking it to move data.
+ */
+public interface FullBackupPreflight {
+
+  /**
+   * Perform the preflight operation necessary for the given package.
+   *
+   * @param pkg The name of the package being proposed for full-data backup
+   * @param agent Live BackupAgent binding to the target app's agent
+   * @return BackupTransport.TRANSPORT_OK to proceed with the backup operation, or one of the
+   * other BackupTransport.* error codes as appropriate
+   */
+    int preflightFullBackup(PackageInfo pkg, IBackupAgent agent);
+
+    long getExpectedSizeOrErrorCode();
+}
diff --git a/services/backup/java/com/android/server/backup/fullbackup/FullBackupTask.java b/services/backup/java/com/android/server/backup/fullbackup/FullBackupTask.java
new file mode 100644
index 0000000..cba838b
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/fullbackup/FullBackupTask.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2017 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.server.backup.fullbackup;
+
+import android.app.backup.IFullBackupRestoreObserver;
+import android.os.RemoteException;
+import android.util.Slog;
+import com.android.server.backup.RefactoredBackupManagerService;
+
+/**
+ * Generic driver skeleton for full backup operations.
+ */
+public abstract class FullBackupTask implements Runnable {
+
+    IFullBackupRestoreObserver mObserver;
+
+    FullBackupTask(IFullBackupRestoreObserver observer) {
+        mObserver = observer;
+    }
+
+    // wrappers for observer use
+    final void sendStartBackup() {
+        if (mObserver != null) {
+            try {
+                mObserver.onStartBackup();
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "full backup observer went away: startBackup");
+                mObserver = null;
+            }
+        }
+    }
+
+    final void sendOnBackupPackage(String name) {
+        if (mObserver != null) {
+            try {
+                // TODO: use a more user-friendly name string
+                mObserver.onBackupPackage(name);
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "full backup observer went away: backupPackage");
+                mObserver = null;
+            }
+        }
+    }
+
+    final void sendEndBackup() {
+        if (mObserver != null) {
+            try {
+                mObserver.onEndBackup();
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "full backup observer went away: endBackup");
+                mObserver = null;
+            }
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java b/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java
new file mode 100644
index 0000000..d2c5963
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/fullbackup/PerformAdbBackupTask.java
@@ -0,0 +1,481 @@
+/*
+ * Copyright (C) 2017 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.server.backup.fullbackup;
+
+import android.app.backup.IFullBackupRestoreObserver;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.os.ParcelFileDescriptor;
+import android.os.RemoteException;
+import android.os.UserHandle;
+import android.util.Slog;
+import com.android.server.AppWidgetBackupBridge;
+import com.android.server.backup.BackupRestoreTask;
+import com.android.server.backup.KeyValueAdbBackupEngine;
+import com.android.server.backup.RefactoredBackupManagerService;
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.TreeMap;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.zip.Deflater;
+import java.util.zip.DeflaterOutputStream;
+import javax.crypto.Cipher;
+import javax.crypto.CipherOutputStream;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+
+/**
+ * Full backup task variant used for adb backup.
+ */
+public class PerformAdbBackupTask extends FullBackupTask implements BackupRestoreTask {
+
+    private RefactoredBackupManagerService backupManagerService;
+    FullBackupEngine mBackupEngine;
+    final AtomicBoolean mLatch;
+
+    ParcelFileDescriptor mOutputFile;
+    DeflaterOutputStream mDeflater;
+    boolean mIncludeApks;
+    boolean mIncludeObbs;
+    boolean mIncludeShared;
+    boolean mDoWidgets;
+    boolean mAllApps;
+    boolean mIncludeSystem;
+    boolean mCompress;
+    boolean mKeyValue;
+    ArrayList<String> mPackages;
+    PackageInfo mCurrentTarget;
+    String mCurrentPassword;
+    String mEncryptPassword;
+    private final int mCurrentOpToken;
+
+    public PerformAdbBackupTask(RefactoredBackupManagerService backupManagerService,
+        ParcelFileDescriptor fd, IFullBackupRestoreObserver observer,
+        boolean includeApks, boolean includeObbs, boolean includeShared, boolean doWidgets,
+        String curPassword, String encryptPassword, boolean doAllApps, boolean doSystem,
+        boolean doCompress, boolean doKeyValue, String[] packages, AtomicBoolean latch) {
+        super(observer);
+        this.backupManagerService = backupManagerService;
+        mCurrentOpToken = backupManagerService.generateRandomIntegerToken();
+        mLatch = latch;
+
+        mOutputFile = fd;
+        mIncludeApks = includeApks;
+        mIncludeObbs = includeObbs;
+        mIncludeShared = includeShared;
+        mDoWidgets = doWidgets;
+        mAllApps = doAllApps;
+        mIncludeSystem = doSystem;
+        mPackages = (packages == null)
+            ? new ArrayList<String>()
+            : new ArrayList<>(Arrays.asList(packages));
+        mCurrentPassword = curPassword;
+        // when backing up, if there is a current backup password, we require that
+        // the user use a nonempty encryption password as well.  if one is supplied
+        // in the UI we use that, but if the UI was left empty we fall back to the
+        // current backup password (which was supplied by the user as well).
+        if (encryptPassword == null || "".equals(encryptPassword)) {
+            mEncryptPassword = curPassword;
+        } else {
+            mEncryptPassword = encryptPassword;
+        }
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Encrypting backup with passphrase=" + mEncryptPassword);
+        }
+        mCompress = doCompress;
+        mKeyValue = doKeyValue;
+    }
+
+    void addPackagesToSet(TreeMap<String, PackageInfo> set, List<String> pkgNames) {
+        for (String pkgName : pkgNames) {
+            if (!set.containsKey(pkgName)) {
+                try {
+                    PackageInfo info = backupManagerService.mPackageManager.getPackageInfo(pkgName,
+                        PackageManager.GET_SIGNATURES);
+                    set.put(pkgName, info);
+                } catch (NameNotFoundException e) {
+                    Slog.w(RefactoredBackupManagerService.TAG, "Unknown package " + pkgName + ", skipping");
+                }
+            }
+        }
+    }
+
+    private OutputStream emitAesBackupHeader(StringBuilder headerbuf,
+        OutputStream ofstream) throws Exception {
+        // User key will be used to encrypt the master key.
+        byte[] newUserSalt = backupManagerService
+            .randomBytes(RefactoredBackupManagerService.PBKDF2_SALT_SIZE);
+        SecretKey userKey = backupManagerService
+            .buildPasswordKey(RefactoredBackupManagerService.PBKDF_CURRENT, mEncryptPassword, newUserSalt,
+                RefactoredBackupManagerService.PBKDF2_HASH_ROUNDS);
+
+        // the master key is random for each backup
+        byte[] masterPw = new byte[256 / 8];
+        backupManagerService.mRng.nextBytes(masterPw);
+        byte[] checksumSalt = backupManagerService
+            .randomBytes(RefactoredBackupManagerService.PBKDF2_SALT_SIZE);
+
+        // primary encryption of the datastream with the random key
+        Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
+        SecretKeySpec masterKeySpec = new SecretKeySpec(masterPw, "AES");
+        c.init(Cipher.ENCRYPT_MODE, masterKeySpec);
+        OutputStream finalOutput = new CipherOutputStream(ofstream, c);
+
+        // line 4: name of encryption algorithm
+        headerbuf.append(RefactoredBackupManagerService.ENCRYPTION_ALGORITHM_NAME);
+        headerbuf.append('\n');
+        // line 5: user password salt [hex]
+        headerbuf.append(backupManagerService.byteArrayToHex(newUserSalt));
+        headerbuf.append('\n');
+        // line 6: master key checksum salt [hex]
+        headerbuf.append(backupManagerService.byteArrayToHex(checksumSalt));
+        headerbuf.append('\n');
+        // line 7: number of PBKDF2 rounds used [decimal]
+        headerbuf.append(RefactoredBackupManagerService.PBKDF2_HASH_ROUNDS);
+        headerbuf.append('\n');
+
+        // line 8: IV of the user key [hex]
+        Cipher mkC = Cipher.getInstance("AES/CBC/PKCS5Padding");
+        mkC.init(Cipher.ENCRYPT_MODE, userKey);
+
+        byte[] IV = mkC.getIV();
+        headerbuf.append(backupManagerService.byteArrayToHex(IV));
+        headerbuf.append('\n');
+
+        // line 9: master IV + key blob, encrypted by the user key [hex].  Blob format:
+        //    [byte] IV length = Niv
+        //    [array of Niv bytes] IV itself
+        //    [byte] master key length = Nmk
+        //    [array of Nmk bytes] master key itself
+        //    [byte] MK checksum hash length = Nck
+        //    [array of Nck bytes] master key checksum hash
+        //
+        // The checksum is the (master key + checksum salt), run through the
+        // stated number of PBKDF2 rounds
+        IV = c.getIV();
+        byte[] mk = masterKeySpec.getEncoded();
+        byte[] checksum = backupManagerService
+            .makeKeyChecksum(RefactoredBackupManagerService.PBKDF_CURRENT, masterKeySpec.getEncoded(),
+                checksumSalt, RefactoredBackupManagerService.PBKDF2_HASH_ROUNDS);
+
+        ByteArrayOutputStream blob = new ByteArrayOutputStream(IV.length + mk.length
+            + checksum.length + 3);
+        DataOutputStream mkOut = new DataOutputStream(blob);
+        mkOut.writeByte(IV.length);
+        mkOut.write(IV);
+        mkOut.writeByte(mk.length);
+        mkOut.write(mk);
+        mkOut.writeByte(checksum.length);
+        mkOut.write(checksum);
+        mkOut.flush();
+        byte[] encryptedMk = mkC.doFinal(blob.toByteArray());
+        headerbuf.append(backupManagerService.byteArrayToHex(encryptedMk));
+        headerbuf.append('\n');
+
+        return finalOutput;
+    }
+
+    private void finalizeBackup(OutputStream out) {
+        try {
+            // A standard 'tar' EOF sequence: two 512-byte blocks of all zeroes.
+            byte[] eof = new byte[512 * 2]; // newly allocated == zero filled
+            out.write(eof);
+        } catch (IOException e) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Error attempting to finalize backup stream");
+        }
+    }
+
+    @Override
+    public void run() {
+        String includeKeyValue = mKeyValue ? ", including key-value backups" : "";
+        Slog.i(RefactoredBackupManagerService.TAG, "--- Performing adb backup" + includeKeyValue + " ---");
+
+        TreeMap<String, PackageInfo> packagesToBackup = new TreeMap<>();
+        FullBackupObbConnection obbConnection = new FullBackupObbConnection(
+            backupManagerService);
+        obbConnection.establish();  // we'll want this later
+
+        sendStartBackup();
+
+        // doAllApps supersedes the package set if any
+        if (mAllApps) {
+            List<PackageInfo> allPackages = backupManagerService.mPackageManager.getInstalledPackages(
+                PackageManager.GET_SIGNATURES);
+            for (int i = 0; i < allPackages.size(); i++) {
+                PackageInfo pkg = allPackages.get(i);
+                // Exclude system apps if we've been asked to do so
+                if (mIncludeSystem == true
+                    || ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)) {
+                    packagesToBackup.put(pkg.packageName, pkg);
+                }
+            }
+        }
+
+        // If we're doing widget state as well, ensure that we have all the involved
+        // host & provider packages in the set
+        if (mDoWidgets) {
+            // TODO: http://b/22388012
+            List<String> pkgs =
+                AppWidgetBackupBridge.getWidgetParticipants(UserHandle.USER_SYSTEM);
+            if (pkgs != null) {
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    Slog.i(RefactoredBackupManagerService.TAG, "Adding widget participants to backup set:");
+                    StringBuilder sb = new StringBuilder(128);
+                    sb.append("   ");
+                    for (String s : pkgs) {
+                        sb.append(' ');
+                        sb.append(s);
+                    }
+                    Slog.i(RefactoredBackupManagerService.TAG, sb.toString());
+                }
+                addPackagesToSet(packagesToBackup, pkgs);
+            }
+        }
+
+        // Now process the command line argument packages, if any. Note that explicitly-
+        // named system-partition packages will be included even if includeSystem was
+        // set to false.
+        if (mPackages != null) {
+            addPackagesToSet(packagesToBackup, mPackages);
+        }
+
+        // Now we cull any inapplicable / inappropriate packages from the set.  This
+        // includes the special shared-storage agent package; we handle that one
+        // explicitly at the end of the backup pass. Packages supporting key-value backup are
+        // added to their own queue, and handled after packages supporting fullbackup.
+        ArrayList<PackageInfo> keyValueBackupQueue = new ArrayList<>();
+        Iterator<Entry<String, PackageInfo>> iter = packagesToBackup.entrySet().iterator();
+        while (iter.hasNext()) {
+            PackageInfo pkg = iter.next().getValue();
+            if (!RefactoredBackupManagerService.appIsEligibleForBackup(pkg.applicationInfo)
+                || RefactoredBackupManagerService.appIsStopped(pkg.applicationInfo)) {
+                iter.remove();
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.i(RefactoredBackupManagerService.TAG, "Package " + pkg.packageName
+                        + " is not eligible for backup, removing.");
+                }
+            } else if (RefactoredBackupManagerService.appIsKeyValueOnly(pkg)) {
+                iter.remove();
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.i(RefactoredBackupManagerService.TAG, "Package " + pkg.packageName
+                        + " is key-value.");
+                }
+                keyValueBackupQueue.add(pkg);
+            }
+        }
+
+        // flatten the set of packages now so we can explicitly control the ordering
+        ArrayList<PackageInfo> backupQueue =
+            new ArrayList<>(packagesToBackup.values());
+        FileOutputStream ofstream = new FileOutputStream(mOutputFile.getFileDescriptor());
+        OutputStream out = null;
+
+        PackageInfo pkg = null;
+        try {
+            boolean encrypting = (mEncryptPassword != null && mEncryptPassword.length() > 0);
+
+            // Only allow encrypted backups of encrypted devices
+            if (backupManagerService.deviceIsEncrypted() && !encrypting) {
+                Slog.e(RefactoredBackupManagerService.TAG, "Unencrypted backup of encrypted device; aborting");
+                return;
+            }
+
+            OutputStream finalOutput = ofstream;
+
+            // Verify that the given password matches the currently-active
+            // backup password, if any
+            if (!backupManagerService.backupPasswordMatches(mCurrentPassword)) {
+                if (RefactoredBackupManagerService.DEBUG) {
+                  Slog.w(RefactoredBackupManagerService.TAG, "Backup password mismatch; aborting");
+                }
+                return;
+            }
+
+            // Write the global file header.  All strings are UTF-8 encoded; lines end
+            // with a '\n' byte.  Actual backup data begins immediately following the
+            // final '\n'.
+            //
+            // line 1: "ANDROID BACKUP"
+            // line 2: backup file format version, currently "5"
+            // line 3: compressed?  "0" if not compressed, "1" if compressed.
+            // line 4: name of encryption algorithm [currently only "none" or "AES-256"]
+            //
+            // When line 4 is not "none", then additional header data follows:
+            //
+            // line 5: user password salt [hex]
+            // line 6: master key checksum salt [hex]
+            // line 7: number of PBKDF2 rounds to use (same for user & master) [decimal]
+            // line 8: IV of the user key [hex]
+            // line 9: master key blob [hex]
+            //     IV of the master key, master key itself, master key checksum hash
+            //
+            // The master key checksum is the master key plus its checksum salt, run through
+            // 10k rounds of PBKDF2.  This is used to verify that the user has supplied the
+            // correct password for decrypting the archive:  the master key decrypted from
+            // the archive using the user-supplied password is also run through PBKDF2 in
+            // this way, and if the result does not match the checksum as stored in the
+            // archive, then we know that the user-supplied password does not match the
+            // archive's.
+            StringBuilder headerbuf = new StringBuilder(1024);
+
+            headerbuf.append(RefactoredBackupManagerService.BACKUP_FILE_HEADER_MAGIC);
+            headerbuf.append(RefactoredBackupManagerService.BACKUP_FILE_VERSION); // integer, no trailing \n
+            headerbuf.append(mCompress ? "\n1\n" : "\n0\n");
+
+            try {
+                // Set up the encryption stage if appropriate, and emit the correct header
+                if (encrypting) {
+                    finalOutput = emitAesBackupHeader(headerbuf, finalOutput);
+                } else {
+                    headerbuf.append("none\n");
+                }
+
+                byte[] header = headerbuf.toString().getBytes("UTF-8");
+                ofstream.write(header);
+
+                // Set up the compression stage feeding into the encryption stage (if any)
+                if (mCompress) {
+                    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
+                    finalOutput = new DeflaterOutputStream(finalOutput, deflater, true);
+                }
+
+                out = finalOutput;
+            } catch (Exception e) {
+                // Should never happen!
+                Slog.e(RefactoredBackupManagerService.TAG, "Unable to emit archive header", e);
+                return;
+            }
+
+            // Shared storage if requested
+            if (mIncludeShared) {
+                try {
+                    pkg = backupManagerService.mPackageManager.getPackageInfo(
+                        RefactoredBackupManagerService.SHARED_BACKUP_AGENT_PACKAGE, 0);
+                    backupQueue.add(pkg);
+                } catch (NameNotFoundException e) {
+                    Slog.e(RefactoredBackupManagerService.TAG, "Unable to find shared-storage backup handler");
+                }
+            }
+
+            // Now actually run the constructed backup sequence for full backup
+            int N = backupQueue.size();
+            for (int i = 0; i < N; i++) {
+                pkg = backupQueue.get(i);
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.i(RefactoredBackupManagerService.TAG,
+                        "--- Performing full backup for package " + pkg.packageName
+                            + " ---");
+                }
+                final boolean isSharedStorage =
+                    pkg.packageName.equals(
+                        RefactoredBackupManagerService.SHARED_BACKUP_AGENT_PACKAGE);
+
+                mBackupEngine = new FullBackupEngine(backupManagerService, out,
+                    null, pkg, mIncludeApks, this, Long.MAX_VALUE, mCurrentOpToken);
+                sendOnBackupPackage(isSharedStorage ? "Shared storage" : pkg.packageName);
+
+                // Don't need to check preflight result as there is no preflight hook.
+                mCurrentTarget = pkg;
+                mBackupEngine.backupOnePackage();
+
+                // after the app's agent runs to handle its private filesystem
+                // contents, back up any OBB content it has on its behalf.
+                if (mIncludeObbs) {
+                    boolean obbOkay = obbConnection.backupObbs(pkg, out);
+                    if (!obbOkay) {
+                        throw new RuntimeException("Failure writing OBB stack for " + pkg);
+                    }
+                }
+            }
+            // And for key-value backup if enabled
+            if (mKeyValue) {
+                for (PackageInfo keyValuePackage : keyValueBackupQueue) {
+                    if (RefactoredBackupManagerService.DEBUG) {
+                        Slog.i(RefactoredBackupManagerService.TAG, "--- Performing key-value backup for package "
+                            + keyValuePackage.packageName + " ---");
+                    }
+                    KeyValueAdbBackupEngine kvBackupEngine =
+                        new KeyValueAdbBackupEngine(out, keyValuePackage,
+                            backupManagerService,
+                            backupManagerService.mPackageManager, backupManagerService.mBaseStateDir, backupManagerService.mDataDir);
+                    sendOnBackupPackage(keyValuePackage.packageName);
+                    kvBackupEngine.backupOnePackage();
+                }
+            }
+
+            // Done!
+            finalizeBackup(out);
+        } catch (RemoteException e) {
+            Slog.e(RefactoredBackupManagerService.TAG, "App died during full backup");
+        } catch (Exception e) {
+            Slog.e(RefactoredBackupManagerService.TAG, "Internal exception during full backup", e);
+        } finally {
+            try {
+                if (out != null) {
+                    out.flush();
+                    out.close();
+                }
+                mOutputFile.close();
+            } catch (IOException e) {
+                /* nothing we can do about this */
+            }
+            synchronized (mLatch) {
+                mLatch.set(true);
+                mLatch.notifyAll();
+            }
+            sendEndBackup();
+            obbConnection.tearDown();
+            if (RefactoredBackupManagerService.DEBUG) {
+              Slog.d(RefactoredBackupManagerService.TAG, "Full backup pass complete.");
+            }
+            backupManagerService.mWakelock.release();
+        }
+    }
+
+    // BackupRestoreTask methods, used for timeout handling
+    @Override
+    public void execute() {
+        // Unused
+    }
+
+    @Override
+    public void operationComplete(long result) {
+        // Unused
+    }
+
+    @Override
+    public void handleCancel(boolean cancelAll) {
+        final PackageInfo target = mCurrentTarget;
+        if (RefactoredBackupManagerService.DEBUG) {
+            Slog.w(RefactoredBackupManagerService.TAG, "adb backup cancel of " + target);
+        }
+        if (target != null) {
+            backupManagerService.tearDownAgentAndKill(mCurrentTarget.applicationInfo);
+        }
+        backupManagerService.removeOperation(mCurrentOpToken);
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/fullbackup/PerformFullTransportBackupTask.java b/services/backup/java/com/android/server/backup/fullbackup/PerformFullTransportBackupTask.java
new file mode 100644
index 0000000..0849d04
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/fullbackup/PerformFullTransportBackupTask.java
@@ -0,0 +1,837 @@
+/*
+ * Copyright (C) 2017 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.server.backup.fullbackup;
+
+import android.app.IBackupAgent;
+import android.app.backup.BackupManager;
+import android.app.backup.BackupManagerMonitor;
+import android.app.backup.BackupProgress;
+import android.app.backup.BackupTransport;
+import android.app.backup.IBackupManagerMonitor;
+import android.app.backup.IBackupObserver;
+import android.app.backup.IFullBackupRestoreObserver;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.os.ParcelFileDescriptor;
+import android.os.RemoteException;
+import android.util.EventLog;
+import android.util.Log;
+import android.util.Slog;
+import com.android.internal.backup.IBackupTransport;
+import com.android.server.EventLogTags;
+import com.android.server.backup.BackupRestoreTask;
+import com.android.server.backup.FullBackupJob;
+import com.android.server.backup.RefactoredBackupManagerService;
+import com.android.server.backup.internal.Operation;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Full backup task extension used for transport-oriented operation.
+ *
+ * Flow:
+ * For each requested package:
+ *     - Spin off a new SinglePackageBackupRunner (mBackupRunner) for the current package.
+ *     - Wait until preflight is complete. (mBackupRunner.getPreflightResultBlocking())
+ *     - If preflight data size is within limit, start reading data from agent pipe and writing
+ *       to transport pipe. While there is data to send, call transport.sendBackupData(int) to
+ *       tell the transport how many bytes to expect on its pipe.
+ *     - After sending all data, call transport.finishBackup() if things went well. And
+ *       transport.cancelFullBackup() otherwise.
+ *
+ * Interactions with mCurrentOperations:
+ *     - An entry for this object is added to mCurrentOperations for the entire lifetime of this
+ *       object. Used to cancel the operation.
+ *     - SinglePackageBackupRunner and SinglePackageBackupPreflight will put ephemeral entries
+ *       to get timeouts or operation complete callbacks.
+ *
+ * Handling cancels:
+ *     - The contract we provide is that the task won't interact with the transport after
+ *       handleCancel() is done executing.
+ *     - This task blocks at 3 points: 1. Preflight result check 2. Reading on agent side pipe
+ *       and 3. Get backup result from mBackupRunner.
+ *     - Bubbling up handleCancel to mBackupRunner handles all 3: 1. Calls handleCancel on the
+ *       preflight operation which counts down on the preflight latch. 2. Tears down the agent,
+ *       so read() returns -1. 3. Notifies mCurrentOpLock which unblocks
+ *       mBackupRunner.getBackupResultBlocking().
+ */
+public class PerformFullTransportBackupTask extends FullBackupTask implements BackupRestoreTask {
+    private static final String TAG = "PFTBT";
+
+    private RefactoredBackupManagerService backupManagerService;
+    private final Object mCancelLock = new Object();
+
+    ArrayList<PackageInfo> mPackages;
+    PackageInfo mCurrentPackage;
+    boolean mUpdateSchedule;
+    CountDownLatch mLatch;
+    FullBackupJob mJob;             // if a scheduled job needs to be finished afterwards
+    IBackupObserver mBackupObserver;
+    IBackupManagerMonitor mMonitor;
+    boolean mUserInitiated;
+    private volatile IBackupTransport mTransport;
+    SinglePackageBackupRunner mBackupRunner;
+    private final int mBackupRunnerOpToken;
+
+    // This is true when a backup operation for some package is in progress.
+    private volatile boolean mIsDoingBackup;
+    private volatile boolean mCancelAll;
+    private final int mCurrentOpToken;
+
+    public PerformFullTransportBackupTask(RefactoredBackupManagerService backupManagerService,
+        IFullBackupRestoreObserver observer,
+        String[] whichPackages, boolean updateSchedule,
+        FullBackupJob runningJob, CountDownLatch latch, IBackupObserver backupObserver,
+        IBackupManagerMonitor monitor, boolean userInitiated) {
+        super(observer);
+        this.backupManagerService = backupManagerService;
+        mUpdateSchedule = updateSchedule;
+        mLatch = latch;
+        mJob = runningJob;
+        mPackages = new ArrayList<>(whichPackages.length);
+        mBackupObserver = backupObserver;
+        mMonitor = monitor;
+        mUserInitiated = userInitiated;
+        mCurrentOpToken = backupManagerService.generateRandomIntegerToken();
+        mBackupRunnerOpToken = backupManagerService.generateRandomIntegerToken();
+
+        if (backupManagerService.isBackupOperationInProgress()) {
+            if (RefactoredBackupManagerService.DEBUG) {
+                Slog.d(TAG, "Skipping full backup. A backup is already in progress.");
+            }
+            mCancelAll = true;
+            return;
+        }
+
+        registerTask();
+
+        for (String pkg : whichPackages) {
+            try {
+                PackageInfo info = backupManagerService.mPackageManager.getPackageInfo(pkg,
+                        PackageManager.GET_SIGNATURES);
+                mCurrentPackage = info;
+                if (!RefactoredBackupManagerService.appIsEligibleForBackup(info.applicationInfo)) {
+                    // Cull any packages that have indicated that backups are not permitted,
+                    // that run as system-domain uids but do not define their own backup agents,
+                    // as well as any explicit mention of the 'special' shared-storage agent
+                    // package (we handle that one at the end).
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                        Slog.d(TAG, "Ignoring ineligible package " + pkg);
+                    }
+                    mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                            BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_INELIGIBLE,
+                            mCurrentPackage,
+                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                            null);
+                    RefactoredBackupManagerService.sendBackupOnPackageResult(mBackupObserver, pkg,
+                        BackupManager.ERROR_BACKUP_NOT_ALLOWED);
+                    continue;
+                } else if (!RefactoredBackupManagerService.appGetsFullBackup(info)) {
+                    // Cull any packages that are found in the queue but now aren't supposed
+                    // to get full-data backup operations.
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                        Slog.d(TAG, "Ignoring full-data backup of key/value participant "
+                                + pkg);
+                    }
+                    mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                            BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_KEY_VALUE_PARTICIPANT,
+                            mCurrentPackage,
+                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                            null);
+                    RefactoredBackupManagerService.sendBackupOnPackageResult(mBackupObserver, pkg,
+                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
+                    continue;
+                } else if (RefactoredBackupManagerService.appIsStopped(info.applicationInfo)) {
+                    // Cull any packages in the 'stopped' state: they've either just been
+                    // installed or have explicitly been force-stopped by the user.  In both
+                    // cases we do not want to launch them for backup.
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                        Slog.d(TAG, "Ignoring stopped package " + pkg);
+                    }
+                    mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                            BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_STOPPED,
+                            mCurrentPackage,
+                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                            null);
+                    RefactoredBackupManagerService.sendBackupOnPackageResult(mBackupObserver, pkg,
+                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
+                    continue;
+                }
+                mPackages.add(info);
+            } catch (NameNotFoundException e) {
+                Slog.i(TAG, "Requested package " + pkg + " not found; ignoring");
+                mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                        BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_NOT_FOUND,
+                        mCurrentPackage,
+                        BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                        null);
+            }
+        }
+    }
+
+    private void registerTask() {
+        synchronized (backupManagerService.mCurrentOpLock) {
+            Slog.d(TAG, "backupmanager pftbt token=" + Integer.toHexString(mCurrentOpToken));
+            backupManagerService.mCurrentOperations.put(mCurrentOpToken, new Operation(
+                RefactoredBackupManagerService.OP_PENDING, this,
+                    RefactoredBackupManagerService.OP_TYPE_BACKUP));
+        }
+    }
+
+    public void unregisterTask() {
+        backupManagerService.removeOperation(mCurrentOpToken);
+    }
+
+    @Override
+    public void execute() {
+        // Nothing to do.
+    }
+
+    @Override
+    public void handleCancel(boolean cancelAll) {
+        synchronized (mCancelLock) {
+            // We only support 'cancelAll = true' case for this task. Cancelling of a single package
+
+            // due to timeout is handled by SinglePackageBackupRunner and SinglePackageBackupPreflight.
+
+            if (!cancelAll) {
+                Slog.wtf(TAG, "Expected cancelAll to be true.");
+            }
+
+            if (mCancelAll) {
+                Slog.d(TAG, "Ignoring duplicate cancel call.");
+                return;
+            }
+
+            mCancelAll = true;
+            if (mIsDoingBackup) {
+                backupManagerService.handleCancel(mBackupRunnerOpToken, cancelAll);
+                try {
+                    mTransport.cancelFullBackup();
+                } catch (RemoteException e) {
+                    Slog.w(TAG, "Error calling cancelFullBackup() on transport: " + e);
+                    // Can't do much.
+                }
+            }
+        }
+    }
+
+    @Override
+    public void operationComplete(long result) {
+        // Nothing to do.
+    }
+
+    @Override
+    public void run() {
+
+        // data from the app, passed to us for bridging to the transport
+        ParcelFileDescriptor[] enginePipes = null;
+
+        // Pipe through which we write data to the transport
+        ParcelFileDescriptor[] transportPipes = null;
+
+        long backoff = 0;
+        int backupRunStatus = BackupManager.SUCCESS;
+
+        try {
+            if (!backupManagerService.mEnabled || !backupManagerService.mProvisioned) {
+                // Backups are globally disabled, so don't proceed.
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.i(TAG, "full backup requested but enabled=" + backupManagerService.mEnabled
+                            + " provisioned=" + backupManagerService.mProvisioned + "; ignoring");
+                }
+                int monitoringEvent;
+                if (!backupManagerService.mEnabled) {
+                    monitoringEvent = BackupManagerMonitor.LOG_EVENT_ID_BACKUP_DISABLED;
+                } else {
+                    monitoringEvent = BackupManagerMonitor.LOG_EVENT_ID_DEVICE_NOT_PROVISIONED;
+                }
+                mMonitor = RefactoredBackupManagerService
+                    .monitorEvent(mMonitor, monitoringEvent, null,
+                        BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
+                mUpdateSchedule = false;
+                backupRunStatus = BackupManager.ERROR_BACKUP_NOT_ALLOWED;
+                return;
+            }
+
+            mTransport = backupManagerService.mTransportManager.getCurrentTransportBinder();
+            if (mTransport == null) {
+                Slog.w(TAG, "Transport not present; full data backup not performed");
+                backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
+                mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                        BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_TRANSPORT_NOT_PRESENT,
+                        mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT,
+                        null);
+                return;
+            }
+
+            // Set up to send data to the transport
+            final int N = mPackages.size();
+            final byte[] buffer = new byte[8192];
+            for (int i = 0; i < N; i++) {
+                PackageInfo currentPackage = mPackages.get(i);
+                String packageName = currentPackage.packageName;
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.i(TAG, "Initiating full-data transport backup of " + packageName
+                            + " token: " + mCurrentOpToken);
+                }
+                EventLog.writeEvent(EventLogTags.FULL_BACKUP_PACKAGE, packageName);
+
+                transportPipes = ParcelFileDescriptor.createPipe();
+
+                // Tell the transport the data's coming
+                int flags = mUserInitiated ? BackupTransport.FLAG_USER_INITIATED : 0;
+                int backupPackageStatus;
+                long quota = Long.MAX_VALUE;
+                synchronized (mCancelLock) {
+                    if (mCancelAll) {
+                        break;
+                    }
+                    backupPackageStatus = mTransport.performFullBackup(currentPackage,
+                            transportPipes[0], flags);
+
+                    if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
+                        quota = mTransport.getBackupQuota(currentPackage.packageName,
+                                true /* isFullBackup */);
+                        // Now set up the backup engine / data source end of things
+                        enginePipes = ParcelFileDescriptor.createPipe();
+                        mBackupRunner =
+                                new SinglePackageBackupRunner(enginePipes[1], currentPackage,
+                                        mTransport, quota, mBackupRunnerOpToken);
+                        // The runner dup'd the pipe half, so we close it here
+                        enginePipes[1].close();
+                        enginePipes[1] = null;
+
+                        mIsDoingBackup = true;
+                    }
+                }
+                if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
+
+                    // The transport has its own copy of the read end of the pipe,
+                    // so close ours now
+                    transportPipes[0].close();
+                    transportPipes[0] = null;
+
+                    // Spin off the runner to fetch the app's data and pipe it
+                    // into the engine pipes
+                    (new Thread(mBackupRunner, "package-backup-bridge")).start();
+
+                    // Read data off the engine pipe and pass it to the transport
+                    // pipe until we hit EOD on the input stream.  We do not take
+                    // close() responsibility for these FDs into these stream wrappers.
+                    FileInputStream in = new FileInputStream(
+                            enginePipes[0].getFileDescriptor());
+                    FileOutputStream out = new FileOutputStream(
+                            transportPipes[1].getFileDescriptor());
+                    long totalRead = 0;
+                    final long preflightResult = mBackupRunner.getPreflightResultBlocking();
+                    // Preflight result is negative if some error happened on preflight.
+                    if (preflightResult < 0) {
+                        if (RefactoredBackupManagerService.MORE_DEBUG) {
+                            Slog.d(TAG, "Backup error after preflight of package "
+                                    + packageName + ": " + preflightResult
+                                    + ", not running backup.");
+                        }
+                        mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                                BackupManagerMonitor.LOG_EVENT_ID_ERROR_PREFLIGHT,
+                                mCurrentPackage,
+                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                                backupManagerService.putMonitoringExtra(null,
+                                        BackupManagerMonitor.EXTRA_LOG_PREFLIGHT_ERROR,
+                                        preflightResult));
+                        backupPackageStatus = (int) preflightResult;
+                    } else {
+                        int nRead = 0;
+                        do {
+                            nRead = in.read(buffer);
+                            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                                Slog.v(TAG, "in.read(buffer) from app: " + nRead);
+                            }
+                            if (nRead > 0) {
+                                out.write(buffer, 0, nRead);
+                                synchronized (mCancelLock) {
+                                    if (!mCancelAll) {
+                                        backupPackageStatus = mTransport.sendBackupData(nRead);
+                                    }
+                                }
+                                totalRead += nRead;
+                                if (mBackupObserver != null && preflightResult > 0) {
+                                    RefactoredBackupManagerService
+                                        .sendBackupOnUpdate(mBackupObserver, packageName,
+                                            new BackupProgress(preflightResult, totalRead));
+                                }
+                            }
+                        } while (nRead > 0
+                                && backupPackageStatus == BackupTransport.TRANSPORT_OK);
+                        // Despite preflight succeeded, package still can hit quota on flight.
+                        if (backupPackageStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
+                            Slog.w(TAG, "Package hit quota limit in-flight " + packageName
+                                    + ": " + totalRead + " of " + quota);
+                            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                                    BackupManagerMonitor.LOG_EVENT_ID_QUOTA_HIT_PREFLIGHT,
+                                    mCurrentPackage,
+                                    BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT,
+                                    null);
+                            mBackupRunner.sendQuotaExceeded(totalRead, quota);
+                        }
+                    }
+
+                    final int backupRunnerResult = mBackupRunner.getBackupResultBlocking();
+
+                    synchronized (mCancelLock) {
+                        mIsDoingBackup = false;
+                        // If mCancelCurrent is true, we have already called cancelFullBackup().
+                        if (!mCancelAll) {
+                            if (backupRunnerResult == BackupTransport.TRANSPORT_OK) {
+                                // If we were otherwise in a good state, now interpret the final
+                                // result based on what finishBackup() returns.  If we're in a
+                                // failure case already, preserve that result and ignore whatever
+                                // finishBackup() reports.
+                                final int finishResult = mTransport.finishBackup();
+                                if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
+                                    backupPackageStatus = finishResult;
+                                }
+                            } else {
+                                mTransport.cancelFullBackup();
+                            }
+                        }
+                    }
+
+                    // A transport-originated error here means that we've hit an error that the
+                    // runner doesn't know about, so it's still moving data but we're pulling the
+                    // rug out from under it.  Don't ask for its result:  we already know better
+                    // and we'll hang if we block waiting for it, since it relies on us to
+                    // read back the data it's writing into the engine.  Just proceed with
+                    // a graceful failure.  The runner/engine mechanism will tear itself
+                    // down cleanly when we close the pipes from this end.  Transport-level
+                    // errors take precedence over agent/app-specific errors for purposes of
+                    // determining our course of action.
+                    if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
+                        // We still could fail in backup runner thread.
+                        if (backupRunnerResult != BackupTransport.TRANSPORT_OK) {
+                            // If there was an error in runner thread and
+                            // not TRANSPORT_ERROR here, overwrite it.
+                            backupPackageStatus = backupRunnerResult;
+                        }
+                    } else {
+                        if (RefactoredBackupManagerService.MORE_DEBUG) {
+                            Slog.i(TAG, "Transport-level failure; cancelling agent work");
+                        }
+                    }
+
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                        Slog.i(TAG, "Done delivering backup data: result="
+                                + backupPackageStatus);
+                    }
+
+                    if (backupPackageStatus != BackupTransport.TRANSPORT_OK) {
+                        Slog.e(TAG, "Error " + backupPackageStatus + " backing up "
+                                + packageName);
+                    }
+
+                    // Also ask the transport how long it wants us to wait before
+                    // moving on to the next package, if any.
+                    backoff = mTransport.requestFullBackupTime();
+                    if (RefactoredBackupManagerService.DEBUG_SCHEDULING) {
+                        Slog.i(TAG, "Transport suggested backoff=" + backoff);
+                    }
+
+                }
+
+                // Roll this package to the end of the backup queue if we're
+                // in a queue-driven mode (regardless of success/failure)
+                if (mUpdateSchedule) {
+                    backupManagerService.enqueueFullBackup(packageName, System.currentTimeMillis());
+                }
+
+                if (backupPackageStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
+                    RefactoredBackupManagerService
+                        .sendBackupOnPackageResult(mBackupObserver, packageName,
+                            BackupManager.ERROR_TRANSPORT_PACKAGE_REJECTED);
+                    if (RefactoredBackupManagerService.DEBUG) {
+                        Slog.i(TAG, "Transport rejected backup of " + packageName
+                                + ", skipping");
+                    }
+                    EventLog.writeEvent(EventLogTags.FULL_BACKUP_AGENT_FAILURE, packageName,
+                            "transport rejected");
+                    // Do nothing, clean up, and continue looping.
+                } else if (backupPackageStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
+                    RefactoredBackupManagerService
+                        .sendBackupOnPackageResult(mBackupObserver, packageName,
+                            BackupManager.ERROR_TRANSPORT_QUOTA_EXCEEDED);
+                    if (RefactoredBackupManagerService.DEBUG) {
+                        Slog.i(TAG, "Transport quota exceeded for package: " + packageName);
+                        EventLog.writeEvent(EventLogTags.FULL_BACKUP_QUOTA_EXCEEDED,
+                                packageName);
+                    }
+                    // Do nothing, clean up, and continue looping.
+                } else if (backupPackageStatus == BackupTransport.AGENT_ERROR) {
+                    RefactoredBackupManagerService
+                        .sendBackupOnPackageResult(mBackupObserver, packageName,
+                            BackupManager.ERROR_AGENT_FAILURE);
+                    Slog.w(TAG, "Application failure for package: " + packageName);
+                    EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName);
+                    backupManagerService.tearDownAgentAndKill(currentPackage.applicationInfo);
+                    // Do nothing, clean up, and continue looping.
+                } else if (backupPackageStatus == BackupManager.ERROR_BACKUP_CANCELLED) {
+                    RefactoredBackupManagerService
+                        .sendBackupOnPackageResult(mBackupObserver, packageName,
+                            BackupManager.ERROR_BACKUP_CANCELLED);
+                    Slog.w(TAG, "Backup cancelled. package=" + packageName +
+                            ", cancelAll=" + mCancelAll);
+                    EventLog.writeEvent(EventLogTags.FULL_BACKUP_CANCELLED, packageName);
+                    backupManagerService.tearDownAgentAndKill(currentPackage.applicationInfo);
+                    // Do nothing, clean up, and continue looping.
+                } else if (backupPackageStatus != BackupTransport.TRANSPORT_OK) {
+                    RefactoredBackupManagerService
+                        .sendBackupOnPackageResult(mBackupObserver, packageName,
+                        BackupManager.ERROR_TRANSPORT_ABORTED);
+                    Slog.w(TAG, "Transport failed; aborting backup: " + backupPackageStatus);
+                    EventLog.writeEvent(EventLogTags.FULL_BACKUP_TRANSPORT_FAILURE);
+                    // Abort entire backup pass.
+                    backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
+                    return;
+                } else {
+                    // Success!
+                    RefactoredBackupManagerService
+                        .sendBackupOnPackageResult(mBackupObserver, packageName,
+                            BackupManager.SUCCESS);
+                    EventLog.writeEvent(EventLogTags.FULL_BACKUP_SUCCESS, packageName);
+                    backupManagerService.logBackupComplete(packageName);
+                }
+                cleanUpPipes(transportPipes);
+                cleanUpPipes(enginePipes);
+                if (currentPackage.applicationInfo != null) {
+                    Slog.i(TAG, "Unbinding agent in " + packageName);
+                    backupManagerService.addBackupTrace("unbinding " + packageName);
+                    try {
+                        backupManagerService.mActivityManager.unbindBackupAgent(currentPackage.applicationInfo);
+                    } catch (RemoteException e) { /* can't happen; activity manager is local */ }
+                }
+            }
+        } catch (Exception e) {
+            backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
+            Slog.w(TAG, "Exception trying full transport backup", e);
+            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                    BackupManagerMonitor.LOG_EVENT_ID_EXCEPTION_FULL_BACKUP,
+                    mCurrentPackage,
+                    BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                    backupManagerService.putMonitoringExtra(null,
+                            BackupManagerMonitor.EXTRA_LOG_EXCEPTION_FULL_BACKUP,
+                            Log.getStackTraceString(e)));
+
+        } finally {
+
+            if (mCancelAll) {
+                backupRunStatus = BackupManager.ERROR_BACKUP_CANCELLED;
+            }
+
+            if (RefactoredBackupManagerService.DEBUG) {
+                Slog.i(TAG, "Full backup completed with status: " + backupRunStatus);
+            }
+            RefactoredBackupManagerService.sendBackupFinished(mBackupObserver, backupRunStatus);
+
+            cleanUpPipes(transportPipes);
+            cleanUpPipes(enginePipes);
+
+            unregisterTask();
+
+            if (mJob != null) {
+                mJob.finishBackupPass();
+            }
+
+            synchronized (backupManagerService.mQueueLock) {
+                backupManagerService.mRunningFullBackupTask = null;
+            }
+
+            mLatch.countDown();
+
+            // Now that we're actually done with schedule-driven work, reschedule
+            // the next pass based on the new queue state.
+            if (mUpdateSchedule) {
+                backupManagerService.scheduleNextFullBackupJob(backoff);
+            }
+
+            Slog.i(RefactoredBackupManagerService.TAG, "Full data backup pass finished.");
+            backupManagerService.mWakelock.release();
+        }
+    }
+
+    void cleanUpPipes(ParcelFileDescriptor[] pipes) {
+        if (pipes != null) {
+            if (pipes[0] != null) {
+                ParcelFileDescriptor fd = pipes[0];
+                pipes[0] = null;
+                try {
+                    fd.close();
+                } catch (IOException e) {
+                    Slog.w(TAG, "Unable to close pipe!");
+                }
+            }
+            if (pipes[1] != null) {
+                ParcelFileDescriptor fd = pipes[1];
+                pipes[1] = null;
+                try {
+                    fd.close();
+                } catch (IOException e) {
+                    Slog.w(TAG, "Unable to close pipe!");
+                }
+            }
+        }
+    }
+
+    // Run the backup and pipe it back to the given socket -- expects to run on
+    // a standalone thread.  The  runner owns this half of the pipe, and closes
+    // it to indicate EOD to the other end.
+    class SinglePackageBackupPreflight implements BackupRestoreTask, FullBackupPreflight {
+        final AtomicLong mResult = new AtomicLong(BackupTransport.AGENT_ERROR);
+        final CountDownLatch mLatch = new CountDownLatch(1);
+        final IBackupTransport mTransport;
+        final long mQuota;
+        private final int mCurrentOpToken;
+
+        SinglePackageBackupPreflight(IBackupTransport transport, long quota, int currentOpToken) {
+            mTransport = transport;
+            mQuota = quota;
+            mCurrentOpToken = currentOpToken;
+        }
+
+        @Override
+        public int preflightFullBackup(PackageInfo pkg, IBackupAgent agent) {
+            int result;
+            try {
+                backupManagerService
+                    .prepareOperationTimeout(mCurrentOpToken, RefactoredBackupManagerService.TIMEOUT_FULL_BACKUP_INTERVAL,
+                        this, RefactoredBackupManagerService.OP_TYPE_BACKUP_WAIT);
+                backupManagerService.addBackupTrace("preflighting");
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    Slog.d(TAG, "Preflighting full payload of " + pkg.packageName);
+                }
+                agent.doMeasureFullBackup(mQuota, mCurrentOpToken, backupManagerService.mBackupManagerBinder);
+
+                // Now wait to get our result back.  If this backstop timeout is reached without
+                // the latch being thrown, flow will continue as though a result or "normal"
+                // timeout had been produced.  In case of a real backstop timeout, mResult
+                // will still contain the value it was constructed with, AGENT_ERROR, which
+                // intentionaly falls into the "just report failure" code.
+                mLatch.await(RefactoredBackupManagerService.TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
+
+                long totalSize = mResult.get();
+                // If preflight timed out, mResult will contain error code as int.
+                if (totalSize < 0) {
+                    return (int) totalSize;
+                }
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    Slog.v(TAG, "Got preflight response; size=" + totalSize);
+                }
+
+                result = mTransport.checkFullBackupSize(totalSize);
+                if (result == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                        Slog.d(TAG, "Package hit quota limit on preflight " +
+                                pkg.packageName + ": " + totalSize + " of " + mQuota);
+                    }
+                    agent.doQuotaExceeded(totalSize, mQuota);
+                }
+            } catch (Exception e) {
+                Slog.w(TAG, "Exception preflighting " + pkg.packageName + ": " + e.getMessage());
+                result = BackupTransport.AGENT_ERROR;
+            }
+            return result;
+        }
+
+        @Override
+        public void execute() {
+            // Unused.
+        }
+
+        @Override
+        public void operationComplete(long result) {
+            // got the callback, and our preflightFullBackup() method is waiting for the result
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.i(TAG, "Preflight op complete, result=" + result);
+            }
+            mResult.set(result);
+            mLatch.countDown();
+            backupManagerService.removeOperation(mCurrentOpToken);
+        }
+
+        @Override
+        public void handleCancel(boolean cancelAll) {
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.i(TAG, "Preflight cancelled; failing");
+            }
+            mResult.set(BackupTransport.AGENT_ERROR);
+            mLatch.countDown();
+            backupManagerService.removeOperation(mCurrentOpToken);
+        }
+
+        @Override
+        public long getExpectedSizeOrErrorCode() {
+            try {
+                mLatch.await(RefactoredBackupManagerService.TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
+                return mResult.get();
+            } catch (InterruptedException e) {
+                return BackupTransport.NO_MORE_DATA;
+            }
+        }
+    }
+
+    class SinglePackageBackupRunner implements Runnable, BackupRestoreTask {
+        final ParcelFileDescriptor mOutput;
+        final PackageInfo mTarget;
+        final SinglePackageBackupPreflight mPreflight;
+        final CountDownLatch mPreflightLatch;
+        final CountDownLatch mBackupLatch;
+        private final int mCurrentOpToken;
+        private final int mEphemeralToken;
+        private FullBackupEngine mEngine;
+        private volatile int mPreflightResult;
+        private volatile int mBackupResult;
+        private final long mQuota;
+        private volatile boolean mIsCancelled;
+
+        SinglePackageBackupRunner(ParcelFileDescriptor output, PackageInfo target,
+                IBackupTransport transport, long quota, int currentOpToken) throws IOException {
+            mOutput = ParcelFileDescriptor.dup(output.getFileDescriptor());
+            mTarget = target;
+            mCurrentOpToken = currentOpToken;
+            mEphemeralToken = backupManagerService.generateRandomIntegerToken();
+            mPreflight = new SinglePackageBackupPreflight(transport, quota, mEphemeralToken);
+            mPreflightLatch = new CountDownLatch(1);
+            mBackupLatch = new CountDownLatch(1);
+            mPreflightResult = BackupTransport.AGENT_ERROR;
+            mBackupResult = BackupTransport.AGENT_ERROR;
+            mQuota = quota;
+            registerTask();
+        }
+
+        void registerTask() {
+            synchronized (backupManagerService.mCurrentOpLock) {
+                backupManagerService.mCurrentOperations.put(mCurrentOpToken, new Operation(
+                    RefactoredBackupManagerService.OP_PENDING, this,
+                        RefactoredBackupManagerService.OP_TYPE_BACKUP_WAIT));
+            }
+        }
+
+        void unregisterTask() {
+            synchronized (backupManagerService.mCurrentOpLock) {
+                backupManagerService.mCurrentOperations.remove(mCurrentOpToken);
+            }
+        }
+
+        @Override
+        public void run() {
+            FileOutputStream out = new FileOutputStream(mOutput.getFileDescriptor());
+            mEngine = new FullBackupEngine(backupManagerService, out, mPreflight, mTarget, false, this, mQuota, mCurrentOpToken);
+            try {
+                try {
+                    if (!mIsCancelled) {
+                        mPreflightResult = mEngine.preflightCheck();
+                    }
+                } finally {
+                    mPreflightLatch.countDown();
+                }
+                // If there is no error on preflight, continue backup.
+                if (mPreflightResult == BackupTransport.TRANSPORT_OK) {
+                    if (!mIsCancelled) {
+                        mBackupResult = mEngine.backupOnePackage();
+                    }
+                }
+            } catch (Exception e) {
+                Slog.e(TAG, "Exception during full package backup of " + mTarget.packageName);
+            } finally {
+                unregisterTask();
+                mBackupLatch.countDown();
+                try {
+                    mOutput.close();
+                } catch (IOException e) {
+                    Slog.w(TAG, "Error closing transport pipe in runner");
+                }
+            }
+        }
+
+        public void sendQuotaExceeded(final long backupDataBytes, final long quotaBytes) {
+            mEngine.sendQuotaExceeded(backupDataBytes, quotaBytes);
+        }
+
+        // If preflight succeeded, returns positive number - preflight size,
+        // otherwise return negative error code.
+        long getPreflightResultBlocking() {
+            try {
+                mPreflightLatch.await(RefactoredBackupManagerService.TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
+                if (mIsCancelled) {
+                    return BackupManager.ERROR_BACKUP_CANCELLED;
+                }
+                if (mPreflightResult == BackupTransport.TRANSPORT_OK) {
+                    return mPreflight.getExpectedSizeOrErrorCode();
+                } else {
+                    return mPreflightResult;
+                }
+            } catch (InterruptedException e) {
+                return BackupTransport.AGENT_ERROR;
+            }
+        }
+
+        int getBackupResultBlocking() {
+            try {
+                mBackupLatch.await(RefactoredBackupManagerService.TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
+                if (mIsCancelled) {
+                    return BackupManager.ERROR_BACKUP_CANCELLED;
+                }
+                return mBackupResult;
+            } catch (InterruptedException e) {
+                return BackupTransport.AGENT_ERROR;
+            }
+        }
+
+
+        // BackupRestoreTask interface: specifically, timeout detection
+
+        @Override
+        public void execute() { /* intentionally empty */ }
+
+        @Override
+        public void operationComplete(long result) { /* intentionally empty */ }
+
+        @Override
+        public void handleCancel(boolean cancelAll) {
+            if (RefactoredBackupManagerService.DEBUG) {
+                Slog.w(TAG, "Full backup cancel of " + mTarget.packageName);
+            }
+
+            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                    BackupManagerMonitor.LOG_EVENT_ID_FULL_BACKUP_CANCEL,
+                    mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
+            mIsCancelled = true;
+            // Cancel tasks spun off by this task.
+            backupManagerService.handleCancel(mEphemeralToken, cancelAll);
+            backupManagerService.tearDownAgentAndKill(mTarget.applicationInfo);
+            // Free up everyone waiting on this task and its children.
+            mPreflightLatch.countDown();
+            mBackupLatch.countDown();
+            // We are done with this operation.
+            backupManagerService.removeOperation(mCurrentOpToken);
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/internal/BackupHandler.java b/services/backup/java/com/android/server/backup/internal/BackupHandler.java
new file mode 100644
index 0000000..f6914fc
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/internal/BackupHandler.java
@@ -0,0 +1,386 @@
+/*
+ * Copyright (C) 2017 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.server.backup.internal;
+
+import android.app.AlarmManager;
+import android.app.backup.RestoreSet;
+import android.content.Intent;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.os.RemoteException;
+import android.os.UserHandle;
+import android.util.EventLog;
+import android.util.Pair;
+import android.util.Slog;
+import com.android.internal.backup.IBackupTransport;
+import com.android.server.EventLogTags;
+import com.android.server.backup.BackupRestoreTask;
+import com.android.server.backup.RefactoredBackupManagerService;
+import com.android.server.backup.fullbackup.PerformAdbBackupTask;
+import com.android.server.backup.fullbackup.PerformFullTransportBackupTask;
+import com.android.server.backup.params.AdbBackupParams;
+import com.android.server.backup.params.AdbParams;
+import com.android.server.backup.params.AdbRestoreParams;
+import com.android.server.backup.params.BackupParams;
+import com.android.server.backup.params.ClearParams;
+import com.android.server.backup.params.ClearRetryParams;
+import com.android.server.backup.params.RestoreGetSetsParams;
+import com.android.server.backup.params.RestoreParams;
+import com.android.server.backup.restore.PerformAdbRestoreTask;
+import com.android.server.backup.restore.PerformUnifiedRestoreTask;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+
+/**
+ * Asynchronous backup/restore handler thread.
+ */
+public class BackupHandler extends Handler {
+
+    private RefactoredBackupManagerService backupManagerService;
+
+    public BackupHandler(
+        RefactoredBackupManagerService backupManagerService, Looper looper) {
+        super(looper);
+        this.backupManagerService = backupManagerService;
+    }
+
+    public void handleMessage(Message msg) {
+
+        switch (msg.what) {
+          case RefactoredBackupManagerService.MSG_RUN_BACKUP: {
+            backupManagerService.mLastBackupPass = System.currentTimeMillis();
+
+            IBackupTransport transport = backupManagerService.mTransportManager.getCurrentTransportBinder();
+            if (transport == null) {
+              Slog.v(RefactoredBackupManagerService.TAG, "Backup requested but no transport available");
+              synchronized (backupManagerService.mQueueLock) {
+                backupManagerService.mBackupRunning = false;
+              }
+              backupManagerService.mWakelock.release();
+              break;
+            }
+
+            // snapshot the pending-backup set and work on that
+            ArrayList<BackupRequest> queue = new ArrayList<>();
+            File oldJournal = backupManagerService.mJournal;
+            synchronized (backupManagerService.mQueueLock) {
+              // Do we have any work to do?  Construct the work queue
+              // then release the synchronization lock to actually run
+              // the backup.
+              if (backupManagerService.mPendingBackups.size() > 0) {
+                for (BackupRequest b : backupManagerService.mPendingBackups.values()) {
+                  queue.add(b);
+                }
+                if (RefactoredBackupManagerService.DEBUG) {
+                  Slog.v(RefactoredBackupManagerService.TAG, "clearing pending backups");
+                }
+                backupManagerService.mPendingBackups.clear();
+
+                // Start a new backup-queue journal file too
+                backupManagerService.mJournal = null;
+
+              }
+            }
+
+            // At this point, we have started a new journal file, and the old
+            // file identity is being passed to the backup processing task.
+            // When it completes successfully, that old journal file will be
+            // deleted.  If we crash prior to that, the old journal is parsed
+            // at next boot and the journaled requests fulfilled.
+            boolean staged = true;
+            if (queue.size() > 0) {
+              // Spin up a backup state sequence and set it running
+              try {
+                String dirName = transport.transportDirName();
+                PerformBackupTask pbt = new PerformBackupTask(
+                    backupManagerService, transport, dirName, queue,
+                            oldJournal, null, null, Collections.<String>emptyList(), false,
+                            false /* nonIncremental */);
+                Message pbtMessage = obtainMessage(
+                    RefactoredBackupManagerService.MSG_BACKUP_RESTORE_STEP, pbt);
+                sendMessage(pbtMessage);
+              } catch (Exception e) {
+                // unable to ask the transport its dir name -- transient failure, since
+                // the above check succeeded.  Try again next time.
+                Slog.e(RefactoredBackupManagerService.TAG, "Transport became unavailable attempting backup"
+                            + " or error initializing backup task", e);
+                staged = false;
+              }
+            } else {
+              Slog.v(RefactoredBackupManagerService.TAG, "Backup requested but nothing pending");
+              staged = false;
+            }
+
+            if (!staged) {
+              // if we didn't actually hand off the wakelock, rewind until next time
+              synchronized (backupManagerService.mQueueLock) {
+                backupManagerService.mBackupRunning = false;
+              }
+              backupManagerService.mWakelock.release();
+            }
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_BACKUP_RESTORE_STEP: {
+            try {
+              BackupRestoreTask task = (BackupRestoreTask) msg.obj;
+              if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.v(RefactoredBackupManagerService.TAG, "Got next step for " + task + ", executing");
+              }
+              task.execute();
+            } catch (ClassCastException e) {
+              Slog.e(RefactoredBackupManagerService.TAG, "Invalid backup task in flight, obj=" + msg.obj);
+            }
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_OP_COMPLETE: {
+            try {
+              Pair<BackupRestoreTask, Long> taskWithResult =
+                        (Pair<BackupRestoreTask, Long>) msg.obj;
+              taskWithResult.first.operationComplete(taskWithResult.second);
+            } catch (ClassCastException e) {
+              Slog.e(RefactoredBackupManagerService.TAG, "Invalid completion in flight, obj=" + msg.obj);
+            }
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_RUN_ADB_BACKUP: {
+            // TODO: refactor full backup to be a looper-based state machine
+            // similar to normal backup/restore.
+            AdbBackupParams params = (AdbBackupParams) msg.obj;
+            PerformAdbBackupTask task = new PerformAdbBackupTask(backupManagerService, params.fd,
+                    params.observer, params.includeApks, params.includeObbs,
+                    params.includeShared, params.doWidgets, params.curPassword,
+                    params.encryptPassword, params.allApps, params.includeSystem,
+                    params.doCompress, params.includeKeyValue, params.packages, params.latch);
+            (new Thread(task, "adb-backup")).start();
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_RUN_FULL_TRANSPORT_BACKUP: {
+            PerformFullTransportBackupTask task = (PerformFullTransportBackupTask) msg.obj;
+            (new Thread(task, "transport-backup")).start();
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_RUN_RESTORE: {
+            RestoreParams params = (RestoreParams) msg.obj;
+            Slog.d(RefactoredBackupManagerService.TAG, "MSG_RUN_RESTORE observer=" + params.observer);
+
+            PerformUnifiedRestoreTask task = new PerformUnifiedRestoreTask(backupManagerService, params.transport,
+                    params.observer, params.monitor, params.token, params.pkgInfo,
+                    params.pmToken, params.isSystemRestore, params.filterSet);
+
+            synchronized (backupManagerService.mPendingRestores) {
+              if (backupManagerService.mIsRestoreInProgress) {
+                if (RefactoredBackupManagerService.DEBUG) {
+                  Slog.d(RefactoredBackupManagerService.TAG, "Restore in progress, queueing.");
+                }
+                backupManagerService.mPendingRestores.add(task);
+                // This task will be picked up and executed when the the currently running
+                // restore task finishes.
+              } else {
+                if (RefactoredBackupManagerService.DEBUG) {
+                  Slog.d(RefactoredBackupManagerService.TAG, "Starting restore.");
+                }
+                backupManagerService.mIsRestoreInProgress = true;
+                Message restoreMsg = obtainMessage(
+                    RefactoredBackupManagerService.MSG_BACKUP_RESTORE_STEP, task);
+                sendMessage(restoreMsg);
+              }
+            }
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_RUN_ADB_RESTORE: {
+            // TODO: refactor full restore to be a looper-based state machine
+            // similar to normal backup/restore.
+            AdbRestoreParams params = (AdbRestoreParams) msg.obj;
+            PerformAdbRestoreTask task = new PerformAdbRestoreTask(backupManagerService, params.fd,
+                    params.curPassword, params.encryptPassword,
+                    params.observer, params.latch);
+            (new Thread(task, "adb-restore")).start();
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_RUN_CLEAR: {
+            ClearParams params = (ClearParams) msg.obj;
+            (new PerformClearTask(backupManagerService, params.transport, params.packageInfo)).run();
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_RETRY_CLEAR: {
+            // reenqueues if the transport remains unavailable
+            ClearRetryParams params = (ClearRetryParams) msg.obj;
+            backupManagerService.clearBackupData(params.transportName, params.packageName);
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_RUN_INITIALIZE: {
+            HashSet<String> queue;
+
+            // Snapshot the pending-init queue and work on that
+            synchronized (backupManagerService.mQueueLock) {
+              queue = new HashSet<>(backupManagerService.mPendingInits);
+              backupManagerService.mPendingInits.clear();
+            }
+
+            (new PerformInitializeTask(backupManagerService, queue)).run();
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_RETRY_INIT: {
+            synchronized (backupManagerService.mQueueLock) {
+              backupManagerService.recordInitPendingLocked(msg.arg1 != 0, (String) msg.obj);
+              backupManagerService.mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
+                        backupManagerService.mRunInitIntent);
+            }
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_RUN_GET_RESTORE_SETS: {
+            // Like other async operations, this is entered with the wakelock held
+            RestoreSet[] sets = null;
+            RestoreGetSetsParams params = (RestoreGetSetsParams) msg.obj;
+            try {
+              sets = params.transport.getAvailableRestoreSets();
+              // cache the result in the active session
+              synchronized (params.session) {
+                params.session.mRestoreSets = sets;
+              }
+              if (sets == null) {
+                EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
+              }
+            } catch (Exception e) {
+              Slog.e(RefactoredBackupManagerService.TAG, "Error from transport getting set list: " + e.getMessage());
+            } finally {
+              if (params.observer != null) {
+                try {
+                  params.observer.restoreSetsAvailable(sets);
+                } catch (RemoteException re) {
+                  Slog.e(RefactoredBackupManagerService.TAG, "Unable to report listing to observer");
+                } catch (Exception e) {
+                  Slog.e(RefactoredBackupManagerService.TAG, "Restore observer threw: " + e.getMessage());
+                }
+              }
+
+              // Done: reset the session timeout clock
+              removeMessages(RefactoredBackupManagerService.MSG_RESTORE_SESSION_TIMEOUT);
+              sendEmptyMessageDelayed(RefactoredBackupManagerService.MSG_RESTORE_SESSION_TIMEOUT,
+                  RefactoredBackupManagerService.TIMEOUT_RESTORE_INTERVAL);
+
+              backupManagerService.mWakelock.release();
+            }
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_BACKUP_OPERATION_TIMEOUT:
+          case RefactoredBackupManagerService.MSG_RESTORE_OPERATION_TIMEOUT: {
+            Slog.d(RefactoredBackupManagerService.TAG,
+                "Timeout message received for token=" + Integer.toHexString(msg.arg1));
+            backupManagerService.handleCancel(msg.arg1, false);
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_RESTORE_SESSION_TIMEOUT: {
+            synchronized (backupManagerService) {
+              if (backupManagerService.mActiveRestoreSession != null) {
+                // Client app left the restore session dangling.  We know that it
+                // can't be in the middle of an actual restore operation because
+                // the timeout is suspended while a restore is in progress.  Clean
+                // up now.
+                Slog.w(RefactoredBackupManagerService.TAG, "Restore session timed out; aborting");
+                backupManagerService.mActiveRestoreSession.markTimedOut();
+                post(backupManagerService.mActiveRestoreSession.new EndRestoreRunnable(
+                    backupManagerService, backupManagerService.mActiveRestoreSession));
+              }
+            }
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_FULL_CONFIRMATION_TIMEOUT: {
+            synchronized (backupManagerService.mAdbBackupRestoreConfirmations) {
+              AdbParams params = backupManagerService.mAdbBackupRestoreConfirmations.get(msg.arg1);
+              if (params != null) {
+                Slog.i(RefactoredBackupManagerService.TAG,
+                    "Full backup/restore timed out waiting for user confirmation");
+
+                // Release the waiter; timeout == completion
+                backupManagerService.signalAdbBackupRestoreCompletion(params);
+
+                // Remove the token from the set
+                backupManagerService.mAdbBackupRestoreConfirmations.delete(msg.arg1);
+
+                // Report a timeout to the observer, if any
+                if (params.observer != null) {
+                  try {
+                    params.observer.onTimeout();
+                  } catch (RemoteException e) {
+                            /* don't care if the app has gone away */
+                  }
+                }
+              } else {
+                Slog.d(RefactoredBackupManagerService.TAG, "couldn't find params for token " + msg.arg1);
+              }
+            }
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_WIDGET_BROADCAST: {
+            final Intent intent = (Intent) msg.obj;
+            backupManagerService.mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM);
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_REQUEST_BACKUP: {
+            BackupParams params = (BackupParams) msg.obj;
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+              Slog.d(RefactoredBackupManagerService.TAG, "MSG_REQUEST_BACKUP observer=" + params.observer);
+            }
+            ArrayList<BackupRequest> kvQueue = new ArrayList<>();
+            for (String packageName : params.kvPackages) {
+              kvQueue.add(new BackupRequest(packageName));
+            }
+            backupManagerService.mBackupRunning = true;
+            backupManagerService.mWakelock.acquire();
+
+            PerformBackupTask pbt = new PerformBackupTask(
+                backupManagerService,
+                params.transport, params.dirName,
+                    kvQueue, null, params.observer, params.monitor, params.fullPackages, true,
+                    params.nonIncrementalBackup);
+            Message pbtMessage = obtainMessage(
+                RefactoredBackupManagerService.MSG_BACKUP_RESTORE_STEP, pbt);
+            sendMessage(pbtMessage);
+            break;
+          }
+
+          case RefactoredBackupManagerService.MSG_SCHEDULE_BACKUP_PACKAGE: {
+            String pkgName = (String) msg.obj;
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+              Slog.d(RefactoredBackupManagerService.TAG, "MSG_SCHEDULE_BACKUP_PACKAGE " + pkgName);
+            }
+            backupManagerService.dataChangedImpl(pkgName);
+            break;
+          }
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/internal/BackupRequest.java b/services/backup/java/com/android/server/backup/internal/BackupRequest.java
new file mode 100644
index 0000000..20c3cc4
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/internal/BackupRequest.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2017 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.server.backup.internal;
+
+/**
+ * Set of backup services that have pending changes.
+ */
+public class BackupRequest {
+
+    public String packageName;
+
+    public BackupRequest(String pkgName) {
+        packageName = pkgName;
+    }
+
+    public String toString() {
+        return "BackupRequest{pkg=" + packageName + "}";
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/internal/ClearDataObserver.java b/services/backup/java/com/android/server/backup/internal/ClearDataObserver.java
new file mode 100644
index 0000000..e24dab7
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/internal/ClearDataObserver.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2017 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.server.backup.internal;
+
+import android.content.pm.IPackageDataObserver;
+import com.android.server.backup.RefactoredBackupManagerService;
+
+public class ClearDataObserver extends IPackageDataObserver.Stub {
+
+    private RefactoredBackupManagerService backupManagerService;
+
+    public ClearDataObserver(RefactoredBackupManagerService backupManagerService) {
+        this.backupManagerService = backupManagerService;
+    }
+
+    public void onRemoveCompleted(String packageName, boolean succeeded) {
+        synchronized (backupManagerService.mClearDataLock) {
+            backupManagerService.mClearingData = false;
+            backupManagerService.mClearDataLock.notifyAll();
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/internal/Operation.java b/services/backup/java/com/android/server/backup/internal/Operation.java
new file mode 100644
index 0000000..fd5ad92
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/internal/Operation.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2017 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.server.backup.internal;
+
+import com.android.server.backup.BackupRestoreTask;
+
+public class Operation {
+
+    public int state;
+    public final BackupRestoreTask callback;
+    public final int type;
+
+    public Operation(int initialState, BackupRestoreTask callbackObj, int type) {
+        state = initialState;
+        callback = callbackObj;
+        this.type = type;
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/internal/PerformBackupTask.java b/services/backup/java/com/android/server/backup/internal/PerformBackupTask.java
new file mode 100644
index 0000000..f3576e6
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/internal/PerformBackupTask.java
@@ -0,0 +1,1060 @@
+/*
+ * Copyright (C) 2017 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.server.backup.internal;
+
+import android.app.ApplicationThreadConstants;
+import android.app.IBackupAgent;
+import android.app.backup.BackupDataInput;
+import android.app.backup.BackupDataOutput;
+import android.app.backup.BackupManager;
+import android.app.backup.BackupManagerMonitor;
+import android.app.backup.BackupTransport;
+import android.app.backup.IBackupManagerMonitor;
+import android.app.backup.IBackupObserver;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.os.Message;
+import android.os.ParcelFileDescriptor;
+import android.os.RemoteException;
+import android.os.SELinux;
+import android.os.UserHandle;
+import android.os.WorkSource;
+import android.system.ErrnoException;
+import android.system.Os;
+import android.util.EventLog;
+import android.util.Slog;
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.backup.IBackupTransport;
+import com.android.server.AppWidgetBackupBridge;
+import com.android.server.EventLogTags;
+import com.android.server.backup.BackupRestoreTask;
+import com.android.server.backup.KeyValueBackupJob;
+import com.android.server.backup.PackageManagerBackupAgent;
+import com.android.server.backup.RefactoredBackupManagerService;
+import com.android.server.backup.RefactoredBackupManagerService.BackupState;
+import com.android.server.backup.fullbackup.PerformFullTransportBackupTask;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.CountDownLatch;
+
+/**
+ * This class handles the process of backing up a given list of key/value backup packages.
+ * Also takes in a list of pending dolly backups and kicks them off when key/value backups
+ * are done.
+ *
+ * Flow:
+ * If required, backup @pm@.
+ * For each pending key/value backup package:
+ *     - Bind to agent.
+ *     - Call agent.doBackup()
+ *     - Wait either for cancel/timeout or operationComplete() callback from the agent.
+ * Start task to perform dolly backups.
+ *
+ * There are three entry points into this class:
+ *     - execute() [Called from the handler thread]
+ *     - operationComplete(long result) [Called from the handler thread]
+ *     - handleCancel(boolean cancelAll) [Can be called from any thread]
+ * These methods synchronize on mCancelLock.
+ *
+ * Interaction with mCurrentOperations:
+ *     - An entry for this task is put into mCurrentOperations for the entire lifetime of the
+ *       task. This is useful to cancel the task if required.
+ *     - An ephemeral entry is put into mCurrentOperations each time we are waiting on for
+ *       response from a backup agent. This is used to plumb timeouts and completion callbacks.
+ */
+public class PerformBackupTask implements BackupRestoreTask {
+    private static final String TAG = "PerformBackupTask";
+
+    private RefactoredBackupManagerService backupManagerService;
+    private final Object mCancelLock = new Object();
+
+    IBackupTransport mTransport;
+    ArrayList<BackupRequest> mQueue;
+    ArrayList<BackupRequest> mOriginalQueue;
+    File mStateDir;
+    File mJournal;
+    BackupState mCurrentState;
+    List<String> mPendingFullBackups;
+    IBackupObserver mObserver;
+    IBackupManagerMonitor mMonitor;
+
+    private final PerformFullTransportBackupTask mFullBackupTask;
+    private final int mCurrentOpToken;
+    private volatile int mEphemeralOpToken;
+
+    // carried information about the current in-flight operation
+    IBackupAgent mAgentBinder;
+    PackageInfo mCurrentPackage;
+    File mSavedStateName;
+    File mBackupDataName;
+    File mNewStateName;
+    ParcelFileDescriptor mSavedState;
+    ParcelFileDescriptor mBackupData;
+    ParcelFileDescriptor mNewState;
+    int mStatus;
+    boolean mFinished;
+    final boolean mUserInitiated;
+    final boolean mNonIncremental;
+
+    private volatile boolean mCancelAll;
+
+    public PerformBackupTask(RefactoredBackupManagerService backupManagerService,
+        IBackupTransport transport, String dirName,
+        ArrayList<BackupRequest> queue, File journal, IBackupObserver observer,
+        IBackupManagerMonitor monitor, List<String> pendingFullBackups,
+        boolean userInitiated, boolean nonIncremental) {
+        this.backupManagerService = backupManagerService;
+        mTransport = transport;
+        mOriginalQueue = queue;
+        mQueue = new ArrayList<>();
+        mJournal = journal;
+        mObserver = observer;
+        mMonitor = monitor;
+        mPendingFullBackups = pendingFullBackups;
+        mUserInitiated = userInitiated;
+        mNonIncremental = nonIncremental;
+
+        mStateDir = new File(backupManagerService.mBaseStateDir, dirName);
+        mCurrentOpToken = backupManagerService.generateRandomIntegerToken();
+
+        mFinished = false;
+
+        synchronized (backupManagerService.mCurrentOpLock) {
+            if (backupManagerService.isBackupOperationInProgress()) {
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.d(TAG, "Skipping backup since one is already in progress.");
+                }
+                mCancelAll = true;
+                mFullBackupTask = null;
+                mCurrentState = BackupState.FINAL;
+                backupManagerService.addBackupTrace("Skipped. Backup already in progress.");
+            } else {
+                mCurrentState = BackupState.INITIAL;
+                CountDownLatch latch = new CountDownLatch(1);
+                String[] fullBackups =
+                        mPendingFullBackups.toArray(new String[mPendingFullBackups.size()]);
+                mFullBackupTask =
+                        new PerformFullTransportBackupTask(backupManagerService, /*fullBackupRestoreObserver*/ null,
+                                fullBackups, /*updateSchedule*/ false, /*runningJob*/ null,
+                                latch,
+                                mObserver, mMonitor, mUserInitiated);
+
+                registerTask();
+                backupManagerService.addBackupTrace("STATE => INITIAL");
+            }
+        }
+    }
+
+    /**
+     * Put this task in the repository of running tasks.
+     */
+    private void registerTask() {
+        synchronized (backupManagerService.mCurrentOpLock) {
+            backupManagerService.mCurrentOperations.put(mCurrentOpToken, new Operation(
+                RefactoredBackupManagerService.OP_PENDING, this,
+                    RefactoredBackupManagerService.OP_TYPE_BACKUP));
+        }
+    }
+
+    /**
+     * Remove this task from repository of running tasks.
+     */
+    private void unregisterTask() {
+        backupManagerService.removeOperation(mCurrentOpToken);
+    }
+
+    // Main entry point: perform one chunk of work, updating the state as appropriate
+    // and reposting the next chunk to the primary backup handler thread.
+    @Override
+    @GuardedBy("mCancelLock")
+    public void execute() {
+        synchronized (mCancelLock) {
+            switch (mCurrentState) {
+                case INITIAL:
+                    beginBackup();
+                    break;
+
+                case RUNNING_QUEUE:
+                    invokeNextAgent();
+                    break;
+
+                case FINAL:
+                    if (!mFinished) finalizeBackup();
+                    else {
+                        Slog.e(TAG, "Duplicate finish");
+                    }
+                    mFinished = true;
+                    break;
+            }
+        }
+    }
+
+    // We're starting a backup pass.  Initialize the transport and send
+    // the PM metadata blob if we haven't already.
+    void beginBackup() {
+        if (RefactoredBackupManagerService.DEBUG_BACKUP_TRACE) {
+            backupManagerService.clearBackupTrace();
+            StringBuilder b = new StringBuilder(256);
+            b.append("beginBackup: [");
+            for (BackupRequest req : mOriginalQueue) {
+                b.append(' ');
+                b.append(req.packageName);
+            }
+            b.append(" ]");
+            backupManagerService.addBackupTrace(b.toString());
+        }
+
+        mAgentBinder = null;
+        mStatus = BackupTransport.TRANSPORT_OK;
+
+        // Sanity check: if the queue is empty we have no work to do.
+        if (mOriginalQueue.isEmpty() && mPendingFullBackups.isEmpty()) {
+            Slog.w(TAG, "Backup begun with an empty queue - nothing to do.");
+            backupManagerService.addBackupTrace("queue empty at begin");
+            RefactoredBackupManagerService.sendBackupFinished(mObserver, BackupManager.SUCCESS);
+            executeNextState(BackupState.FINAL);
+            return;
+        }
+
+        // We need to retain the original queue contents in case of transport
+        // failure, but we want a working copy that we can manipulate along
+        // the way.
+        mQueue = (ArrayList<BackupRequest>) mOriginalQueue.clone();
+
+        // When the transport is forcing non-incremental key/value payloads, we send the
+        // metadata only if it explicitly asks for it.
+        boolean skipPm = mNonIncremental;
+
+        // The app metadata pseudopackage might also be represented in the
+        // backup queue if apps have been added/removed since the last time
+        // we performed a backup.  Drop it from the working queue now that
+        // we're committed to evaluating it for backup regardless.
+        for (int i = 0; i < mQueue.size(); i++) {
+            if (RefactoredBackupManagerService.PACKAGE_MANAGER_SENTINEL.equals(mQueue.get(i).packageName)) {
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    Slog.i(TAG, "Metadata in queue; eliding");
+                }
+                mQueue.remove(i);
+                skipPm = false;
+                break;
+            }
+        }
+
+        if (RefactoredBackupManagerService.DEBUG) Slog.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
+
+        File pmState = new File(mStateDir, RefactoredBackupManagerService.PACKAGE_MANAGER_SENTINEL);
+        try {
+            final String transportName = mTransport.transportDirName();
+            EventLog.writeEvent(EventLogTags.BACKUP_START, transportName);
+
+            // If we haven't stored package manager metadata yet, we must init the transport.
+            if (mStatus == BackupTransport.TRANSPORT_OK && pmState.length() <= 0) {
+                Slog.i(TAG, "Initializing (wiping) backup state and transport storage");
+                backupManagerService.addBackupTrace("initializing transport " + transportName);
+                backupManagerService.resetBackupState(mStateDir);  // Just to make sure.
+                mStatus = mTransport.initializeDevice();
+
+                backupManagerService.addBackupTrace("transport.initializeDevice() == " + mStatus);
+                if (mStatus == BackupTransport.TRANSPORT_OK) {
+                    EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE);
+                } else {
+                    EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)");
+                    Slog.e(TAG, "Transport error in initializeDevice()");
+                }
+            }
+
+            if (skipPm) {
+                Slog.d(TAG, "Skipping backup of package metadata.");
+                executeNextState(BackupState.RUNNING_QUEUE);
+            } else {
+                // The package manager doesn't have a proper <application> etc, but since
+                // it's running here in the system process we can just set up its agent
+                // directly and use a synthetic BackupRequest.  We always run this pass
+                // because it's cheap and this way we guarantee that we don't get out of
+                // step even if we're selecting among various transports at run time.
+                if (mStatus == BackupTransport.TRANSPORT_OK) {
+                    PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
+                            backupManagerService.mPackageManager);
+                    mStatus = invokeAgentForBackup(
+                        RefactoredBackupManagerService.PACKAGE_MANAGER_SENTINEL,
+                            IBackupAgent.Stub.asInterface(pmAgent.onBind()), mTransport);
+                    backupManagerService.addBackupTrace("PMBA invoke: " + mStatus);
+
+                    // Because the PMBA is a local instance, it has already executed its
+                    // backup callback and returned.  Blow away the lingering (spurious)
+                    // pending timeout message for it.
+                    backupManagerService.mBackupHandler.removeMessages(
+                        RefactoredBackupManagerService.MSG_BACKUP_OPERATION_TIMEOUT);
+                }
+            }
+
+            if (mStatus == BackupTransport.TRANSPORT_NOT_INITIALIZED) {
+                // The backend reports that our dataset has been wiped.  Note this in
+                // the event log; the no-success code below will reset the backup
+                // state as well.
+                EventLog.writeEvent(EventLogTags.BACKUP_RESET, mTransport.transportDirName());
+            }
+        } catch (Exception e) {
+            Slog.e(TAG, "Error in backup thread", e);
+            backupManagerService.addBackupTrace("Exception in backup thread: " + e);
+            mStatus = BackupTransport.TRANSPORT_ERROR;
+        } finally {
+            // If we've succeeded so far, invokeAgentForBackup() will have run the PM
+            // metadata and its completion/timeout callback will continue the state
+            // machine chain.  If it failed that won't happen; we handle that now.
+            backupManagerService.addBackupTrace("exiting prelim: " + mStatus);
+            if (mStatus != BackupTransport.TRANSPORT_OK) {
+                // if things went wrong at this point, we need to
+                // restage everything and try again later.
+                backupManagerService.resetBackupState(mStateDir);  // Just to make sure.
+                // In case of any other error, it's backup transport error.
+                RefactoredBackupManagerService.sendBackupFinished(mObserver, BackupManager.ERROR_TRANSPORT_ABORTED);
+                executeNextState(BackupState.FINAL);
+            }
+        }
+    }
+
+    // Transport has been initialized and the PM metadata submitted successfully
+    // if that was warranted.  Now we process the single next thing in the queue.
+    void invokeNextAgent() {
+        mStatus = BackupTransport.TRANSPORT_OK;
+        backupManagerService.addBackupTrace("invoke q=" + mQueue.size());
+
+        // Sanity check that we have work to do.  If not, skip to the end where
+        // we reestablish the wakelock invariants etc.
+        if (mQueue.isEmpty()) {
+            if (RefactoredBackupManagerService.MORE_DEBUG) Slog.i(TAG, "queue now empty");
+            executeNextState(BackupState.FINAL);
+            return;
+        }
+
+        // pop the entry we're going to process on this step
+        BackupRequest request = mQueue.get(0);
+        mQueue.remove(0);
+
+        Slog.d(TAG, "starting key/value backup of " + request);
+        backupManagerService.addBackupTrace("launch agent for " + request.packageName);
+
+        // Verify that the requested app exists; it might be something that
+        // requested a backup but was then uninstalled.  The request was
+        // journalled and rather than tamper with the journal it's safer
+        // to sanity-check here.  This also gives us the classname of the
+        // package's backup agent.
+        try {
+            mCurrentPackage = backupManagerService.mPackageManager.getPackageInfo(request.packageName,
+                    PackageManager.GET_SIGNATURES);
+            if (!RefactoredBackupManagerService.appIsEligibleForBackup(mCurrentPackage.applicationInfo)) {
+                // The manifest has changed but we had a stale backup request pending.
+                // This won't happen again because the app won't be requesting further
+                // backups.
+                Slog.i(TAG, "Package " + request.packageName
+                        + " no longer supports backup; skipping");
+                backupManagerService.addBackupTrace("skipping - not eligible, completion is noop");
+                // Shouldn't happen in case of requested backup, as pre-check was done in
+                // #requestBackup(), except to app update done concurrently
+                RefactoredBackupManagerService.sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
+                        BackupManager.ERROR_BACKUP_NOT_ALLOWED);
+                executeNextState(BackupState.RUNNING_QUEUE);
+                return;
+            }
+
+            if (RefactoredBackupManagerService.appGetsFullBackup(mCurrentPackage)) {
+                // It's possible that this app *formerly* was enqueued for key/value backup,
+                // but has since been updated and now only supports the full-data path.
+                // Don't proceed with a key/value backup for it in this case.
+                Slog.i(TAG, "Package " + request.packageName
+                        + " requests full-data rather than key/value; skipping");
+                backupManagerService.addBackupTrace("skipping - fullBackupOnly, completion is noop");
+                // Shouldn't happen in case of requested backup, as pre-check was done in
+                // #requestBackup()
+                RefactoredBackupManagerService.sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
+                        BackupManager.ERROR_BACKUP_NOT_ALLOWED);
+                executeNextState(BackupState.RUNNING_QUEUE);
+                return;
+            }
+
+            if (RefactoredBackupManagerService.appIsStopped(mCurrentPackage.applicationInfo)) {
+                // The app has been force-stopped or cleared or just installed,
+                // and not yet launched out of that state, so just as it won't
+                // receive broadcasts, we won't run it for backup.
+                backupManagerService.addBackupTrace("skipping - stopped");
+                RefactoredBackupManagerService.sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
+                        BackupManager.ERROR_BACKUP_NOT_ALLOWED);
+                executeNextState(BackupState.RUNNING_QUEUE);
+                return;
+            }
+
+            IBackupAgent agent = null;
+            try {
+                backupManagerService.mWakelock.setWorkSource(new WorkSource(mCurrentPackage.applicationInfo.uid));
+                agent = backupManagerService.bindToAgentSynchronous(mCurrentPackage.applicationInfo,
+                        ApplicationThreadConstants.BACKUP_MODE_INCREMENTAL);
+                backupManagerService.addBackupTrace("agent bound; a? = " + (agent != null));
+                if (agent != null) {
+                    mAgentBinder = agent;
+                    mStatus = invokeAgentForBackup(request.packageName, agent, mTransport);
+                    // at this point we'll either get a completion callback from the
+                    // agent, or a timeout message on the main handler.  either way, we're
+                    // done here as long as we're successful so far.
+                } else {
+                    // Timeout waiting for the agent
+                    mStatus = BackupTransport.AGENT_ERROR;
+                }
+            } catch (SecurityException ex) {
+                // Try for the next one.
+                Slog.d(TAG, "error in bind/backup", ex);
+                mStatus = BackupTransport.AGENT_ERROR;
+                        backupManagerService.addBackupTrace("agent SE");
+            }
+        } catch (NameNotFoundException e) {
+            Slog.d(TAG, "Package does not exist; skipping");
+            backupManagerService.addBackupTrace("no such package");
+            mStatus = BackupTransport.AGENT_UNKNOWN;
+        } finally {
+            backupManagerService.mWakelock.setWorkSource(null);
+
+            // If there was an agent error, no timeout/completion handling will occur.
+            // That means we need to direct to the next state ourselves.
+            if (mStatus != BackupTransport.TRANSPORT_OK) {
+                BackupState nextState = BackupState.RUNNING_QUEUE;
+                mAgentBinder = null;
+
+                // An agent-level failure means we reenqueue this one agent for
+                // a later retry, but otherwise proceed normally.
+                if (mStatus == BackupTransport.AGENT_ERROR) {
+                    if (RefactoredBackupManagerService.MORE_DEBUG) Slog.i(TAG, "Agent failure for " + request.packageName
+                            + " - restaging");
+                    backupManagerService.dataChangedImpl(request.packageName);
+                    mStatus = BackupTransport.TRANSPORT_OK;
+                    if (mQueue.isEmpty()) nextState = BackupState.FINAL;
+                    RefactoredBackupManagerService
+                        .sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
+                            BackupManager.ERROR_AGENT_FAILURE);
+                } else if (mStatus == BackupTransport.AGENT_UNKNOWN) {
+                    // Failed lookup of the app, so we couldn't bring up an agent, but
+                    // we're otherwise fine.  Just drop it and go on to the next as usual.
+                    mStatus = BackupTransport.TRANSPORT_OK;
+                    RefactoredBackupManagerService
+                        .sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
+                            BackupManager.ERROR_PACKAGE_NOT_FOUND);
+                } else {
+                    // Transport-level failure means we reenqueue everything
+                    revertAndEndBackup();
+                    nextState = BackupState.FINAL;
+                }
+
+                executeNextState(nextState);
+            } else {
+                // success case
+                backupManagerService.addBackupTrace("expecting completion/timeout callback");
+            }
+        }
+    }
+
+    void finalizeBackup() {
+        backupManagerService.addBackupTrace("finishing");
+
+        // Mark packages that we didn't backup (because backup was cancelled, etc.) as needing
+        // backup.
+        for (BackupRequest req : mQueue) {
+            backupManagerService.dataChangedImpl(req.packageName);
+        }
+
+        // Either backup was successful, in which case we of course do not need
+        // this pass's journal any more; or it failed, in which case we just
+        // re-enqueued all of these packages in the current active journal.
+        // Either way, we no longer need this pass's journal.
+        if (mJournal != null && !mJournal.delete()) {
+            Slog.e(TAG, "Unable to remove backup journal file " + mJournal);
+        }
+
+        // If everything actually went through and this is the first time we've
+        // done a backup, we can now record what the current backup dataset token
+        // is.
+        if ((backupManagerService.mCurrentToken == 0) && (mStatus == BackupTransport.TRANSPORT_OK)) {
+            backupManagerService.addBackupTrace("success; recording token");
+            try {
+                backupManagerService.mCurrentToken = mTransport.getCurrentRestoreSet();
+                backupManagerService.writeRestoreTokens();
+            } catch (Exception e) {
+                // nothing for it at this point, unfortunately, but this will be
+                // recorded the next time we fully succeed.
+                Slog.e(TAG, "Transport threw reporting restore set: " + e.getMessage());
+                backupManagerService.addBackupTrace("transport threw returning token");
+            }
+        }
+
+        // Set up the next backup pass - at this point we can set mBackupRunning
+        // to false to allow another pass to fire, because we're done with the
+        // state machine sequence and the wakelock is refcounted.
+        synchronized (backupManagerService.mQueueLock) {
+            backupManagerService.mBackupRunning = false;
+            if (mStatus == BackupTransport.TRANSPORT_NOT_INITIALIZED) {
+                // Make sure we back up everything and perform the one-time init
+                if (RefactoredBackupManagerService.MORE_DEBUG) Slog.d(TAG, "Server requires init; rerunning");
+                backupManagerService.addBackupTrace("init required; rerunning");
+                try {
+                    final String name = backupManagerService.mTransportManager.getTransportName(mTransport);
+                    if (name != null) {
+                        backupManagerService.mPendingInits.add(name);
+                    } else {
+                        if (RefactoredBackupManagerService.DEBUG) {
+                            Slog.w(TAG, "Couldn't find name of transport " + mTransport
+                                    + " for init");
+                        }
+                    }
+                } catch (Exception e) {
+                    Slog.w(TAG, "Failed to query transport name for init: " + e.getMessage());
+                    // swallow it and proceed; we don't rely on this
+                }
+                clearMetadata();
+                backupManagerService.backupNow();
+            }
+        }
+
+        backupManagerService.clearBackupTrace();
+
+        unregisterTask();
+
+        if (!mCancelAll && mStatus == BackupTransport.TRANSPORT_OK &&
+                mPendingFullBackups != null && !mPendingFullBackups.isEmpty()) {
+            Slog.d(TAG, "Starting full backups for: " + mPendingFullBackups);
+            // Acquiring wakelock for PerformFullTransportBackupTask before its start.
+            backupManagerService.mWakelock.acquire();
+            (new Thread(mFullBackupTask, "full-transport-requested")).start();
+        } else if (mCancelAll) {
+            if (mFullBackupTask != null) {
+                mFullBackupTask.unregisterTask();
+            }
+            RefactoredBackupManagerService.sendBackupFinished(mObserver, BackupManager.ERROR_BACKUP_CANCELLED);
+        } else {
+            mFullBackupTask.unregisterTask();
+            switch (mStatus) {
+                case BackupTransport.TRANSPORT_OK:
+                    RefactoredBackupManagerService.sendBackupFinished(mObserver, BackupManager.SUCCESS);
+                    break;
+                case BackupTransport.TRANSPORT_NOT_INITIALIZED:
+                    RefactoredBackupManagerService.sendBackupFinished(mObserver, BackupManager.ERROR_TRANSPORT_ABORTED);
+                    break;
+                case BackupTransport.TRANSPORT_ERROR:
+                default:
+                    RefactoredBackupManagerService.sendBackupFinished(mObserver, BackupManager.ERROR_TRANSPORT_ABORTED);
+                    break;
+            }
+        }
+        Slog.i(RefactoredBackupManagerService.TAG, "K/V backup pass finished.");
+        // Only once we're entirely finished do we release the wakelock for k/v backup.
+        backupManagerService.mWakelock.release();
+    }
+
+    // Remove the PM metadata state. This will generate an init on the next pass.
+    void clearMetadata() {
+        final File pmState = new File(mStateDir, RefactoredBackupManagerService.PACKAGE_MANAGER_SENTINEL);
+        if (pmState.exists()) pmState.delete();
+    }
+
+    // Invoke an agent's doBackup() and start a timeout message spinning on the main
+    // handler in case it doesn't get back to us.
+    int invokeAgentForBackup(String packageName, IBackupAgent agent,
+            IBackupTransport transport) {
+        if (RefactoredBackupManagerService.DEBUG) Slog.d(TAG, "invokeAgentForBackup on " + packageName);
+        backupManagerService.addBackupTrace("invoking " + packageName);
+
+        File blankStateName = new File(mStateDir, "blank_state");
+        mSavedStateName = new File(mStateDir, packageName);
+        mBackupDataName = new File(backupManagerService.mDataDir, packageName + ".data");
+        mNewStateName = new File(mStateDir, packageName + ".new");
+        if (RefactoredBackupManagerService.MORE_DEBUG) Slog.d(TAG, "data file: " + mBackupDataName);
+
+        mSavedState = null;
+        mBackupData = null;
+        mNewState = null;
+
+        boolean callingAgent = false;
+        mEphemeralOpToken = backupManagerService.generateRandomIntegerToken();
+        try {
+            // Look up the package info & signatures.  This is first so that if it
+            // throws an exception, there's no file setup yet that would need to
+            // be unraveled.
+            if (packageName.equals(RefactoredBackupManagerService.PACKAGE_MANAGER_SENTINEL)) {
+                // The metadata 'package' is synthetic; construct one and make
+                // sure our global state is pointed at it
+                mCurrentPackage = new PackageInfo();
+                mCurrentPackage.packageName = packageName;
+            }
+
+            // In a full backup, we pass a null ParcelFileDescriptor as
+            // the saved-state "file". For key/value backups we pass the old state if
+            // an incremental backup is required, and a blank state otherwise.
+            mSavedState = ParcelFileDescriptor.open(
+                    mNonIncremental ? blankStateName : mSavedStateName,
+                    ParcelFileDescriptor.MODE_READ_ONLY |
+                    ParcelFileDescriptor.MODE_CREATE);  // Make an empty file if necessary
+
+            mBackupData = ParcelFileDescriptor.open(mBackupDataName,
+                    ParcelFileDescriptor.MODE_READ_WRITE |
+                    ParcelFileDescriptor.MODE_CREATE |
+                    ParcelFileDescriptor.MODE_TRUNCATE);
+
+            if (!SELinux.restorecon(mBackupDataName)) {
+                Slog.e(TAG, "SELinux restorecon failed on " + mBackupDataName);
+            }
+
+            mNewState = ParcelFileDescriptor.open(mNewStateName,
+                    ParcelFileDescriptor.MODE_READ_WRITE |
+                    ParcelFileDescriptor.MODE_CREATE |
+                    ParcelFileDescriptor.MODE_TRUNCATE);
+
+            final long quota = mTransport.getBackupQuota(packageName, false /* isFullBackup */);
+            callingAgent = true;
+
+            // Initiate the target's backup pass
+            backupManagerService.addBackupTrace("setting timeout");
+            backupManagerService
+                .prepareOperationTimeout(mEphemeralOpToken, RefactoredBackupManagerService.TIMEOUT_BACKUP_INTERVAL, this,
+                    RefactoredBackupManagerService.OP_TYPE_BACKUP_WAIT);
+            backupManagerService.addBackupTrace("calling agent doBackup()");
+
+            agent.doBackup(mSavedState, mBackupData, mNewState, quota, mEphemeralOpToken,
+                    backupManagerService.mBackupManagerBinder);
+        } catch (Exception e) {
+            Slog.e(TAG, "Error invoking for backup on " + packageName + ". " + e);
+            backupManagerService.addBackupTrace("exception: " + e);
+            EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName,
+                    e.toString());
+            errorCleanup();
+            return callingAgent ? BackupTransport.AGENT_ERROR
+                    : BackupTransport.TRANSPORT_ERROR;
+        } finally {
+            if (mNonIncremental) {
+                blankStateName.delete();
+            }
+        }
+
+        // At this point the agent is off and running.  The next thing to happen will
+        // either be a callback from the agent, at which point we'll process its data
+        // for transport, or a timeout.  Either way the next phase will happen in
+        // response to the TimeoutHandler interface callbacks.
+        backupManagerService.addBackupTrace("invoke success");
+        return BackupTransport.TRANSPORT_OK;
+    }
+
+    public void failAgent(IBackupAgent agent, String message) {
+        try {
+            agent.fail(message);
+        } catch (Exception e) {
+            Slog.w(TAG, "Error conveying failure to " + mCurrentPackage.packageName);
+        }
+    }
+
+    // SHA-1 a byte array and return the result in hex
+    private String SHA1Checksum(byte[] input) {
+        final byte[] checksum;
+        try {
+            MessageDigest md = MessageDigest.getInstance("SHA-1");
+            checksum = md.digest(input);
+        } catch (NoSuchAlgorithmException e) {
+            Slog.e(TAG, "Unable to use SHA-1!");
+            return "00";
+        }
+
+        StringBuffer sb = new StringBuffer(checksum.length * 2);
+        for (int i = 0; i < checksum.length; i++) {
+            sb.append(Integer.toHexString(checksum[i]));
+        }
+        return sb.toString();
+    }
+
+    private void writeWidgetPayloadIfAppropriate(FileDescriptor fd, String pkgName)
+            throws IOException {
+        // TODO: http://b/22388012
+        byte[] widgetState = AppWidgetBackupBridge.getWidgetState(pkgName,
+                UserHandle.USER_SYSTEM);
+        // has the widget state changed since last time?
+        final File widgetFile = new File(mStateDir, pkgName + "_widget");
+        final boolean priorStateExists = widgetFile.exists();
+
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+            if (priorStateExists || widgetState != null) {
+                Slog.i(TAG, "Checking widget update: state=" + (widgetState != null)
+                        + " prior=" + priorStateExists);
+            }
+        }
+
+        if (!priorStateExists && widgetState == null) {
+            // no prior state, no new state => nothing to do
+            return;
+        }
+
+        // if the new state is not null, we might need to compare checksums to
+        // determine whether to update the widget blob in the archive.  If the
+        // widget state *is* null, we know a priori at this point that we simply
+        // need to commit a deletion for it.
+        String newChecksum = null;
+        if (widgetState != null) {
+            newChecksum = SHA1Checksum(widgetState);
+            if (priorStateExists) {
+                final String priorChecksum;
+                try (
+                    FileInputStream fin = new FileInputStream(widgetFile);
+                    DataInputStream in = new DataInputStream(fin)
+                ) {
+                    priorChecksum = in.readUTF();
+                }
+                if (Objects.equals(newChecksum, priorChecksum)) {
+                    // Same checksum => no state change => don't rewrite the widget data
+                    return;
+                }
+            }
+        } // else widget state *became* empty, so we need to commit a deletion
+
+        BackupDataOutput out = new BackupDataOutput(fd);
+        if (widgetState != null) {
+            try (
+                FileOutputStream fout = new FileOutputStream(widgetFile);
+                DataOutputStream stateOut = new DataOutputStream(fout)
+            ) {
+                stateOut.writeUTF(newChecksum);
+            }
+
+            out.writeEntityHeader(RefactoredBackupManagerService.KEY_WIDGET_STATE, widgetState.length);
+            out.writeEntityData(widgetState, widgetState.length);
+        } else {
+            // Widget state for this app has been removed; commit a deletion
+            out.writeEntityHeader(RefactoredBackupManagerService.KEY_WIDGET_STATE, -1);
+            widgetFile.delete();
+        }
+    }
+
+    @Override
+    @GuardedBy("mCancelLock")
+    public void operationComplete(long unusedResult) {
+        backupManagerService.removeOperation(mEphemeralOpToken);
+        synchronized (mCancelLock) {
+            // The agent reported back to us!
+            if (mFinished) {
+                Slog.d(TAG, "operationComplete received after task finished.");
+                return;
+            }
+
+            if (mBackupData == null) {
+                // This callback was racing with our timeout, so we've cleaned up the
+                // agent state already and are on to the next thing.  We have nothing
+                // further to do here: agent state having been cleared means that we've
+                // initiated the appropriate next operation.
+                final String pkg = (mCurrentPackage != null)
+                        ? mCurrentPackage.packageName : "[none]";
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    Slog.i(TAG, "Callback after agent teardown: " + pkg);
+                }
+                backupManagerService.addBackupTrace("late opComplete; curPkg = " + pkg);
+                return;
+            }
+
+            final String pkgName = mCurrentPackage.packageName;
+            final long filepos = mBackupDataName.length();
+            FileDescriptor fd = mBackupData.getFileDescriptor();
+            try {
+                // If it's a 3rd party app, see whether they wrote any protected keys
+                // and complain mightily if they are attempting shenanigans.
+                if (mCurrentPackage.applicationInfo != null &&
+                        (mCurrentPackage.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
+                                == 0) {
+                    ParcelFileDescriptor readFd = ParcelFileDescriptor.open(mBackupDataName,
+                            ParcelFileDescriptor.MODE_READ_ONLY);
+                    BackupDataInput in = new BackupDataInput(readFd.getFileDescriptor());
+                    try {
+                        while (in.readNextHeader()) {
+                            final String key = in.getKey();
+                            if (key != null && key.charAt(0) >= 0xff00) {
+                                // Not okay: crash them and bail.
+                                failAgent(mAgentBinder, "Illegal backup key: " + key);
+                                backupManagerService
+                                    .addBackupTrace("illegal key " + key + " from " + pkgName);
+                                EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, pkgName,
+                                        "bad key");
+                                mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                                        BackupManagerMonitor.LOG_EVENT_ID_ILLEGAL_KEY,
+                                        mCurrentPackage,
+                                        BackupManagerMonitor
+                                                .LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                                        backupManagerService.putMonitoringExtra(null,
+                                                BackupManagerMonitor.EXTRA_LOG_ILLEGAL_KEY,
+                                                key));
+                                backupManagerService.mBackupHandler.removeMessages(
+                                    RefactoredBackupManagerService.MSG_BACKUP_OPERATION_TIMEOUT);
+                                RefactoredBackupManagerService
+                                    .sendBackupOnPackageResult(mObserver, pkgName,
+                                        BackupManager.ERROR_AGENT_FAILURE);
+                                errorCleanup();
+                                // agentErrorCleanup() implicitly executes next state properly
+                                return;
+                            }
+                            in.skipEntityData();
+                        }
+                    } finally {
+                        if (readFd != null) {
+                            readFd.close();
+                        }
+                    }
+                }
+
+                // Piggyback the widget state payload, if any
+                writeWidgetPayloadIfAppropriate(fd, pkgName);
+            } catch (IOException e) {
+                // Hard disk error; recovery/failure policy TBD.  For now roll back,
+                // but we may want to consider this a transport-level failure (i.e.
+                // we're in such a bad state that we can't contemplate doing backup
+                // operations any more during this pass).
+                Slog.w(TAG, "Unable to save widget state for " + pkgName);
+                try {
+                    Os.ftruncate(fd, filepos);
+                } catch (ErrnoException ee) {
+                    Slog.w(TAG, "Unable to roll back!");
+                }
+            }
+
+            // Spin the data off to the transport and proceed with the next stage.
+            if (RefactoredBackupManagerService.MORE_DEBUG) Slog.v(TAG, "operationComplete(): sending data to transport for "
+                    + pkgName);
+            backupManagerService.mBackupHandler.removeMessages(
+                RefactoredBackupManagerService.MSG_BACKUP_OPERATION_TIMEOUT);
+            clearAgentState();
+            backupManagerService.addBackupTrace("operation complete");
+
+            ParcelFileDescriptor backupData = null;
+            mStatus = BackupTransport.TRANSPORT_OK;
+            long size = 0;
+            try {
+                size = mBackupDataName.length();
+                if (size > 0) {
+                    if (mStatus == BackupTransport.TRANSPORT_OK) {
+                        backupData = ParcelFileDescriptor.open(mBackupDataName,
+                                ParcelFileDescriptor.MODE_READ_ONLY);
+                        backupManagerService.addBackupTrace("sending data to transport");
+                        int flags = mUserInitiated ? BackupTransport.FLAG_USER_INITIATED : 0;
+                        mStatus = mTransport.performBackup(mCurrentPackage, backupData, flags);
+                    }
+
+                    // TODO - We call finishBackup() for each application backed up, because
+                    // we need to know now whether it succeeded or failed.  Instead, we should
+                    // hold off on finishBackup() until the end, which implies holding off on
+                    // renaming *all* the output state files (see below) until that happens.
+
+                    backupManagerService.addBackupTrace("data delivered: " + mStatus);
+                    if (mStatus == BackupTransport.TRANSPORT_OK) {
+                        backupManagerService.addBackupTrace("finishing op on transport");
+                        mStatus = mTransport.finishBackup();
+                        backupManagerService.addBackupTrace("finished: " + mStatus);
+                    } else if (mStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
+                        backupManagerService.addBackupTrace("transport rejected package");
+                    }
+                } else {
+                    if (RefactoredBackupManagerService.MORE_DEBUG) Slog.i(TAG,
+                            "no backup data written; not calling transport");
+                    backupManagerService.addBackupTrace("no data to send");
+                    mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                            BackupManagerMonitor.LOG_EVENT_ID_NO_DATA_TO_SEND,
+                            mCurrentPackage,
+                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                            null);
+                }
+
+                if (mStatus == BackupTransport.TRANSPORT_OK) {
+                    // After successful transport, delete the now-stale data
+                    // and juggle the files so that next time we supply the agent
+                    // with the new state file it just created.
+                    mBackupDataName.delete();
+                    mNewStateName.renameTo(mSavedStateName);
+                    RefactoredBackupManagerService
+                        .sendBackupOnPackageResult(mObserver, pkgName, BackupManager.SUCCESS);
+                    EventLog.writeEvent(EventLogTags.BACKUP_PACKAGE, pkgName, size);
+                    backupManagerService.logBackupComplete(pkgName);
+                } else if (mStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
+                    // The transport has rejected backup of this specific package.  Roll it
+                    // back but proceed with running the rest of the queue.
+                    mBackupDataName.delete();
+                    mNewStateName.delete();
+                    RefactoredBackupManagerService.sendBackupOnPackageResult(mObserver, pkgName,
+                            BackupManager.ERROR_TRANSPORT_PACKAGE_REJECTED);
+                    EventLogTags.writeBackupAgentFailure(pkgName, "Transport rejected");
+                } else if (mStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
+                    RefactoredBackupManagerService.sendBackupOnPackageResult(mObserver, pkgName,
+                            BackupManager.ERROR_TRANSPORT_QUOTA_EXCEEDED);
+                    EventLog.writeEvent(EventLogTags.BACKUP_QUOTA_EXCEEDED, pkgName);
+                } else {
+                    // Actual transport-level failure to communicate the data to the backend
+                    RefactoredBackupManagerService.sendBackupOnPackageResult(mObserver, pkgName,
+                            BackupManager.ERROR_TRANSPORT_ABORTED);
+                    EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, pkgName);
+                }
+            } catch (Exception e) {
+                RefactoredBackupManagerService.sendBackupOnPackageResult(mObserver, pkgName,
+                        BackupManager.ERROR_TRANSPORT_ABORTED);
+                Slog.e(TAG, "Transport error backing up " + pkgName, e);
+                EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, pkgName);
+                mStatus = BackupTransport.TRANSPORT_ERROR;
+            } finally {
+                try {
+                    if (backupData != null) backupData.close();
+                } catch (IOException e) {
+                }
+            }
+
+            final BackupState nextState;
+            if (mStatus == BackupTransport.TRANSPORT_OK
+                    || mStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
+                // Success or single-package rejection.  Proceed with the next app if any,
+                // otherwise we're done.
+                nextState = (mQueue.isEmpty()) ? BackupState.FINAL : BackupState.RUNNING_QUEUE;
+            } else if (mStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    Slog.d(TAG, "Package " + mCurrentPackage.packageName +
+                            " hit quota limit on k/v backup");
+                }
+                if (mAgentBinder != null) {
+                    try {
+                        long quota = mTransport.getBackupQuota(mCurrentPackage.packageName,
+                                false);
+                        mAgentBinder.doQuotaExceeded(size, quota);
+                    } catch (Exception e) {
+                        Slog.e(TAG, "Unable to notify about quota exceeded: " + e.getMessage());
+                    }
+                }
+                nextState = (mQueue.isEmpty()) ? BackupState.FINAL : BackupState.RUNNING_QUEUE;
+            } else {
+                // Any other error here indicates a transport-level failure.  That means
+                // we need to halt everything and reschedule everything for next time.
+                revertAndEndBackup();
+                nextState = BackupState.FINAL;
+            }
+
+            executeNextState(nextState);
+        }
+    }
+
+
+    @Override
+    @GuardedBy("mCancelLock")
+    public void handleCancel(boolean cancelAll) {
+        backupManagerService.removeOperation(mEphemeralOpToken);
+        synchronized (mCancelLock) {
+            if (mFinished) {
+                // We have already cancelled this operation.
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    Slog.d(TAG, "Ignoring stale cancel. cancelAll=" + cancelAll);
+                }
+                return;
+            }
+            mCancelAll = cancelAll;
+            final String logPackageName = (mCurrentPackage != null)
+                    ? mCurrentPackage.packageName
+                    : "no_package_yet";
+            Slog.i(TAG, "Cancel backing up " + logPackageName);
+            EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, logPackageName);
+            backupManagerService.addBackupTrace("cancel of " + logPackageName + ", cancelAll=" + cancelAll);
+            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                    BackupManagerMonitor.LOG_EVENT_ID_KEY_VALUE_BACKUP_CANCEL,
+                    mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT,
+                    backupManagerService.putMonitoringExtra(null, BackupManagerMonitor.EXTRA_LOG_CANCEL_ALL,
+                            mCancelAll));
+            errorCleanup();
+            if (!cancelAll) {
+                // The current agent either timed out or was cancelled running doBackup().
+                // Restage it for the next time we run a backup pass.
+                // !!! TODO: keep track of failure counts per agent, and blacklist those which
+                // fail repeatedly (i.e. have proved themselves to be buggy).
+                executeNextState(
+                        mQueue.isEmpty() ? BackupState.FINAL : BackupState.RUNNING_QUEUE);
+                backupManagerService.dataChangedImpl(mCurrentPackage.packageName);
+            } else {
+                finalizeBackup();
+            }
+        }
+    }
+
+    void revertAndEndBackup() {
+        if (RefactoredBackupManagerService.MORE_DEBUG) Slog.i(TAG, "Reverting backup queue - restaging everything");
+        backupManagerService.addBackupTrace("transport error; reverting");
+
+        // We want to reset the backup schedule based on whatever the transport suggests
+        // by way of retry/backoff time.
+        long delay;
+        try {
+            delay = mTransport.requestBackupTime();
+        } catch (Exception e) {
+            Slog.w(TAG, "Unable to contact transport for recommended backoff: " + e.getMessage());
+            delay = 0;  // use the scheduler's default
+        }
+        KeyValueBackupJob.schedule(backupManagerService.mContext, delay);
+
+        for (BackupRequest request : mOriginalQueue) {
+            backupManagerService.dataChangedImpl(request.packageName);
+        }
+
+    }
+
+    void errorCleanup() {
+        mBackupDataName.delete();
+        mNewStateName.delete();
+        clearAgentState();
+    }
+
+    // Cleanup common to both success and failure cases
+    void clearAgentState() {
+        try { if (mSavedState != null) mSavedState.close(); } catch (IOException e) {}
+        try { if (mBackupData != null) mBackupData.close(); } catch (IOException e) {}
+        try { if (mNewState != null) mNewState.close(); } catch (IOException e) {}
+        synchronized (backupManagerService.mCurrentOpLock) {
+            // Current-operation callback handling requires the validity of these various
+            // bits of internal state as an invariant of the operation still being live.
+            // This means we make sure to clear all of the state in unison inside the lock.
+            backupManagerService.mCurrentOperations.remove(mEphemeralOpToken);
+            mSavedState = mBackupData = mNewState = null;
+        }
+
+        // If this was a pseudopackage there's no associated Activity Manager state
+        if (mCurrentPackage.applicationInfo != null) {
+            backupManagerService.addBackupTrace("unbinding " + mCurrentPackage.packageName);
+            try {  // unbind even on timeout, just in case
+                backupManagerService.mActivityManager.unbindBackupAgent(mCurrentPackage.applicationInfo);
+            } catch (RemoteException e) { /* can't happen; activity manager is local */ }
+        }
+    }
+
+    void executeNextState(BackupState nextState) {
+        if (RefactoredBackupManagerService.MORE_DEBUG) Slog.i(TAG, " => executing next step on "
+                + this + " nextState=" + nextState);
+        backupManagerService.addBackupTrace("executeNextState => " + nextState);
+        mCurrentState = nextState;
+        Message msg = backupManagerService.mBackupHandler.obtainMessage(
+            RefactoredBackupManagerService.MSG_BACKUP_RESTORE_STEP, this);
+        backupManagerService.mBackupHandler.sendMessage(msg);
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/internal/PerformClearTask.java b/services/backup/java/com/android/server/backup/internal/PerformClearTask.java
new file mode 100644
index 0000000..8575227
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/internal/PerformClearTask.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2017 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.server.backup.internal;
+
+import android.content.pm.PackageInfo;
+import android.util.Slog;
+import com.android.internal.backup.IBackupTransport;
+import com.android.server.backup.RefactoredBackupManagerService;
+import java.io.File;
+
+public class PerformClearTask implements Runnable {
+
+    private RefactoredBackupManagerService backupManagerService;
+    IBackupTransport mTransport;
+    PackageInfo mPackage;
+
+    PerformClearTask(RefactoredBackupManagerService backupManagerService,
+        IBackupTransport transport, PackageInfo packageInfo) {
+        this.backupManagerService = backupManagerService;
+        mTransport = transport;
+        mPackage = packageInfo;
+    }
+
+    public void run() {
+        try {
+            // Clear the on-device backup state to ensure a full backup next time
+            File stateDir = new File(backupManagerService.mBaseStateDir, mTransport.transportDirName());
+            File stateFile = new File(stateDir, mPackage.packageName);
+            stateFile.delete();
+
+            // Tell the transport to remove all the persistent storage for the app
+            // TODO - need to handle failures
+            mTransport.clearBackupData(mPackage);
+        } catch (Exception e) {
+            Slog.e(RefactoredBackupManagerService.TAG,
+                "Transport threw clearing data for " + mPackage + ": " + e.getMessage());
+        } finally {
+            try {
+                // TODO - need to handle failures
+                mTransport.finishBackup();
+            } catch (Exception e) {
+                // Nothing we can do here, alas
+                Slog.e(RefactoredBackupManagerService.TAG, "Unable to mark clear operation finished: " + e.getMessage());
+            }
+
+            // Last but not least, release the cpu
+            backupManagerService.mWakelock.release();
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/internal/PerformInitializeTask.java b/services/backup/java/com/android/server/backup/internal/PerformInitializeTask.java
new file mode 100644
index 0000000..003794f
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/internal/PerformInitializeTask.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2017 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.server.backup.internal;
+
+import android.app.AlarmManager;
+import android.app.backup.BackupTransport;
+import android.os.SystemClock;
+import android.util.EventLog;
+import android.util.Slog;
+import com.android.internal.backup.IBackupTransport;
+import com.android.server.EventLogTags;
+import com.android.server.backup.RefactoredBackupManagerService;
+import java.io.File;
+import java.util.HashSet;
+
+public class PerformInitializeTask implements Runnable {
+
+    private RefactoredBackupManagerService backupManagerService;
+    HashSet<String> mQueue;
+
+    PerformInitializeTask(RefactoredBackupManagerService backupManagerService,
+        HashSet<String> transportNames) {
+        this.backupManagerService = backupManagerService;
+        mQueue = transportNames;
+    }
+
+    public void run() {
+        try {
+            for (String transportName : mQueue) {
+                IBackupTransport transport =
+                    backupManagerService.mTransportManager.getTransportBinder(transportName);
+                if (transport == null) {
+                    Slog.e(
+                        RefactoredBackupManagerService.TAG, "Requested init for " + transportName + " but not found");
+                    continue;
+                }
+
+                Slog.i(RefactoredBackupManagerService.TAG, "Initializing (wiping) backup transport storage: " + transportName);
+                EventLog.writeEvent(EventLogTags.BACKUP_START, transport.transportDirName());
+                long startRealtime = SystemClock.elapsedRealtime();
+                int status = transport.initializeDevice();
+
+                if (status == BackupTransport.TRANSPORT_OK) {
+                    status = transport.finishBackup();
+                }
+
+                // Okay, the wipe really happened.  Clean up our local bookkeeping.
+                if (status == BackupTransport.TRANSPORT_OK) {
+                    Slog.i(RefactoredBackupManagerService.TAG, "Device init successful");
+                    int millis = (int) (SystemClock.elapsedRealtime() - startRealtime);
+                    EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE);
+                    backupManagerService
+                        .resetBackupState(new File(backupManagerService.mBaseStateDir, transport.transportDirName()));
+                    EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis);
+                    synchronized (backupManagerService.mQueueLock) {
+                        backupManagerService.recordInitPendingLocked(false, transportName);
+                    }
+                } else {
+                    // If this didn't work, requeue this one and try again
+                    // after a suitable interval
+                    Slog.e(RefactoredBackupManagerService.TAG, "Transport error in initializeDevice()");
+                    EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)");
+                    synchronized (backupManagerService.mQueueLock) {
+                        backupManagerService.recordInitPendingLocked(true, transportName);
+                    }
+                    // do this via another alarm to make sure of the wakelock states
+                    long delay = transport.requestBackupTime();
+                    Slog.w(RefactoredBackupManagerService.TAG, "Init failed on " + transportName + " resched in " + delay);
+                    backupManagerService.mAlarmManager.set(AlarmManager.RTC_WAKEUP,
+                        System.currentTimeMillis() + delay, backupManagerService.mRunInitIntent);
+                }
+            }
+        } catch (Exception e) {
+            Slog.e(RefactoredBackupManagerService.TAG, "Unexpected error performing init", e);
+        } finally {
+            // Done; release the wakelock
+            backupManagerService.mWakelock.release();
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/internal/ProvisionedObserver.java b/services/backup/java/com/android/server/backup/internal/ProvisionedObserver.java
new file mode 100644
index 0000000..54545c1b9
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/internal/ProvisionedObserver.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2017 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.server.backup.internal;
+
+import android.database.ContentObserver;
+import android.os.Handler;
+import android.util.Slog;
+import com.android.server.backup.KeyValueBackupJob;
+import com.android.server.backup.RefactoredBackupManagerService;
+
+public class ProvisionedObserver extends ContentObserver {
+
+    private RefactoredBackupManagerService backupManagerService;
+
+    public ProvisionedObserver(
+        RefactoredBackupManagerService backupManagerService, Handler handler) {
+        super(handler);
+        this.backupManagerService = backupManagerService;
+    }
+
+    public void onChange(boolean selfChange) {
+        final boolean wasProvisioned = backupManagerService.mProvisioned;
+        final boolean isProvisioned = backupManagerService.deviceIsProvisioned();
+        // latch: never unprovision
+        backupManagerService.mProvisioned = wasProvisioned || isProvisioned;
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+            Slog.d(RefactoredBackupManagerService.TAG, "Provisioning change: was=" + wasProvisioned
+                + " is=" + isProvisioned + " now=" + backupManagerService.mProvisioned);
+        }
+
+        synchronized (backupManagerService.mQueueLock) {
+            if (backupManagerService.mProvisioned && !wasProvisioned && backupManagerService.mEnabled) {
+                // we're now good to go, so start the backup alarms
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                  Slog.d(RefactoredBackupManagerService.TAG, "Now provisioned, so starting backups");
+                }
+                KeyValueBackupJob.schedule(backupManagerService.mContext);
+                backupManagerService.scheduleNextFullBackupJob(0);
+            }
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/internal/RunBackupReceiver.java b/services/backup/java/com/android/server/backup/internal/RunBackupReceiver.java
new file mode 100644
index 0000000..0347c36
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/internal/RunBackupReceiver.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2017 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.server.backup.internal;
+
+import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Message;
+import android.util.Slog;
+import com.android.server.backup.RefactoredBackupManagerService;
+
+public class RunBackupReceiver extends BroadcastReceiver {
+
+    private RefactoredBackupManagerService backupManagerService;
+
+    public RunBackupReceiver(RefactoredBackupManagerService backupManagerService) {
+        this.backupManagerService = backupManagerService;
+    }
+
+    public void onReceive(Context context, Intent intent) {
+        if (RefactoredBackupManagerService.RUN_BACKUP_ACTION.equals(intent.getAction())) {
+            synchronized (backupManagerService.mQueueLock) {
+                if (backupManagerService.mPendingInits.size() > 0) {
+                    // If there are pending init operations, we process those
+                    // and then settle into the usual periodic backup schedule.
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                      Slog.v(RefactoredBackupManagerService.TAG, "Init pending at scheduled backup");
+                    }
+                    try {
+                        backupManagerService.mAlarmManager.cancel(
+                            backupManagerService.mRunInitIntent);
+                        backupManagerService.mRunInitIntent.send();
+                    } catch (PendingIntent.CanceledException ce) {
+                        Slog.e(RefactoredBackupManagerService.TAG, "Run init intent cancelled");
+                        // can't really do more than bail here
+                    }
+                } else {
+                    // Don't run backups now if we're disabled or not yet
+                    // fully set up.
+                    if (backupManagerService.mEnabled && backupManagerService.mProvisioned) {
+                        if (!backupManagerService.mBackupRunning) {
+                            if (RefactoredBackupManagerService.DEBUG) {
+                              Slog.v(RefactoredBackupManagerService.TAG, "Running a backup pass");
+                            }
+
+                            // Acquire the wakelock and pass it to the backup thread.  it will
+                            // be released once backup concludes.
+                            backupManagerService.mBackupRunning = true;
+                            backupManagerService.mWakelock.acquire();
+
+                            Message msg = backupManagerService.mBackupHandler.obtainMessage(
+                                RefactoredBackupManagerService.MSG_RUN_BACKUP);
+                            backupManagerService.mBackupHandler.sendMessage(msg);
+                        } else {
+                            Slog.i(RefactoredBackupManagerService.TAG, "Backup time but one already running");
+                        }
+                    } else {
+                        Slog.w(
+                            RefactoredBackupManagerService.TAG, "Backup pass but e=" + backupManagerService.mEnabled + " p=" + backupManagerService.mProvisioned);
+                    }
+                }
+            }
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/internal/RunInitializeReceiver.java b/services/backup/java/com/android/server/backup/internal/RunInitializeReceiver.java
new file mode 100644
index 0000000..1f1c714
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/internal/RunInitializeReceiver.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2017 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.server.backup.internal;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Message;
+import android.util.Slog;
+import com.android.server.backup.RefactoredBackupManagerService;
+
+public class RunInitializeReceiver extends BroadcastReceiver {
+
+    private RefactoredBackupManagerService backupManagerService;
+
+    public RunInitializeReceiver(RefactoredBackupManagerService backupManagerService) {
+        this.backupManagerService = backupManagerService;
+    }
+
+    public void onReceive(Context context, Intent intent) {
+        if (RefactoredBackupManagerService.RUN_INITIALIZE_ACTION.equals(intent.getAction())) {
+            synchronized (backupManagerService.mQueueLock) {
+                if (RefactoredBackupManagerService.DEBUG) {
+                  Slog.v(RefactoredBackupManagerService.TAG, "Running a device init");
+                }
+
+                // Acquire the wakelock and pass it to the init thread.  it will
+                // be released once init concludes.
+                backupManagerService.mWakelock.acquire();
+
+                Message msg = backupManagerService.mBackupHandler.obtainMessage(
+                    RefactoredBackupManagerService.MSG_RUN_INITIALIZE);
+                backupManagerService.mBackupHandler.sendMessage(msg);
+            }
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/params/AdbBackupParams.java b/services/backup/java/com/android/server/backup/params/AdbBackupParams.java
new file mode 100644
index 0000000..86bc5fc
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/params/AdbBackupParams.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2017 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.server.backup.params;
+
+import android.os.ParcelFileDescriptor;
+
+public class AdbBackupParams extends AdbParams {
+
+    public boolean includeApks;
+    public boolean includeObbs;
+    public boolean includeShared;
+    public boolean doWidgets;
+    public boolean allApps;
+    public boolean includeSystem;
+    public boolean doCompress;
+    public boolean includeKeyValue;
+    public String[] packages;
+
+    public AdbBackupParams(ParcelFileDescriptor output, boolean saveApks, boolean saveObbs,
+        boolean saveShared, boolean alsoWidgets, boolean doAllApps, boolean doSystem,
+        boolean compress, boolean doKeyValue, String[] pkgList) {
+        fd = output;
+        includeApks = saveApks;
+        includeObbs = saveObbs;
+        includeShared = saveShared;
+        doWidgets = alsoWidgets;
+        allApps = doAllApps;
+        includeSystem = doSystem;
+        doCompress = compress;
+        includeKeyValue = doKeyValue;
+        packages = pkgList;
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/params/AdbParams.java b/services/backup/java/com/android/server/backup/params/AdbParams.java
new file mode 100644
index 0000000..f189aec
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/params/AdbParams.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2017 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.server.backup.params;
+
+import android.app.backup.IFullBackupRestoreObserver;
+import android.os.ParcelFileDescriptor;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * Parameters used by adbBackup() and adbRestore().
+ */
+public class AdbParams {
+
+    public ParcelFileDescriptor fd;
+    public final AtomicBoolean latch;
+    public IFullBackupRestoreObserver observer;
+    public String curPassword;     // filled in by the confirmation step
+    public String encryptPassword;
+
+    AdbParams() {
+        latch = new AtomicBoolean(false);
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/params/AdbRestoreParams.java b/services/backup/java/com/android/server/backup/params/AdbRestoreParams.java
new file mode 100644
index 0000000..0142fe0
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/params/AdbRestoreParams.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2017 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.server.backup.params;
+
+import android.os.ParcelFileDescriptor;
+
+public class AdbRestoreParams extends AdbParams {
+
+    public AdbRestoreParams(ParcelFileDescriptor input) {
+        fd = input;
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/params/BackupParams.java b/services/backup/java/com/android/server/backup/params/BackupParams.java
new file mode 100644
index 0000000..8173506
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/params/BackupParams.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2017 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.server.backup.params;
+
+import android.app.backup.IBackupManagerMonitor;
+import android.app.backup.IBackupObserver;
+import com.android.internal.backup.IBackupTransport;
+import java.util.ArrayList;
+
+public class BackupParams {
+
+    public IBackupTransport transport;
+    public String dirName;
+    public ArrayList<String> kvPackages;
+    public ArrayList<String> fullPackages;
+    public IBackupObserver observer;
+    public IBackupManagerMonitor monitor;
+    public boolean userInitiated;
+    public boolean nonIncrementalBackup;
+
+    public BackupParams(IBackupTransport transport, String dirName, ArrayList<String> kvPackages,
+        ArrayList<String> fullPackages, IBackupObserver observer,
+        IBackupManagerMonitor monitor, boolean userInitiated, boolean nonIncrementalBackup) {
+        this.transport = transport;
+        this.dirName = dirName;
+        this.kvPackages = kvPackages;
+        this.fullPackages = fullPackages;
+        this.observer = observer;
+        this.monitor = monitor;
+        this.userInitiated = userInitiated;
+        this.nonIncrementalBackup = nonIncrementalBackup;
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/params/ClearParams.java b/services/backup/java/com/android/server/backup/params/ClearParams.java
new file mode 100644
index 0000000..a62350c
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/params/ClearParams.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2017 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.server.backup.params;
+
+import android.content.pm.PackageInfo;
+import com.android.internal.backup.IBackupTransport;
+
+public class ClearParams {
+
+    public IBackupTransport transport;
+    public PackageInfo packageInfo;
+
+    public ClearParams(IBackupTransport _transport, PackageInfo _info) {
+        transport = _transport;
+        packageInfo = _info;
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/params/ClearRetryParams.java b/services/backup/java/com/android/server/backup/params/ClearRetryParams.java
new file mode 100644
index 0000000..fcf66e4
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/params/ClearRetryParams.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2017 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.server.backup.params;
+
+public class ClearRetryParams {
+
+    public String transportName;
+    public String packageName;
+
+    public ClearRetryParams(String transport, String pkg) {
+        transportName = transport;
+        packageName = pkg;
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/params/RestoreGetSetsParams.java b/services/backup/java/com/android/server/backup/params/RestoreGetSetsParams.java
new file mode 100644
index 0000000..5495d5b
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/params/RestoreGetSetsParams.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2017 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.server.backup.params;
+
+import android.app.backup.IBackupManagerMonitor;
+import android.app.backup.IRestoreObserver;
+import com.android.internal.backup.IBackupTransport;
+import com.android.server.backup.restore.ActiveRestoreSession;
+
+public class RestoreGetSetsParams {
+
+    public IBackupTransport transport;
+    public ActiveRestoreSession session;
+    public IRestoreObserver observer;
+    public IBackupManagerMonitor monitor;
+
+    public RestoreGetSetsParams(IBackupTransport _transport, ActiveRestoreSession _session,
+        IRestoreObserver _observer, IBackupManagerMonitor _monitor) {
+        transport = _transport;
+        session = _session;
+        observer = _observer;
+        monitor = _monitor;
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/params/RestoreParams.java b/services/backup/java/com/android/server/backup/params/RestoreParams.java
new file mode 100644
index 0000000..a0b4187
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/params/RestoreParams.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2017 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.server.backup.params;
+
+import android.app.backup.IBackupManagerMonitor;
+import android.app.backup.IRestoreObserver;
+import android.content.pm.PackageInfo;
+import com.android.internal.backup.IBackupTransport;
+
+public class RestoreParams {
+
+    public IBackupTransport transport;
+    public String dirName;
+    public IRestoreObserver observer;
+    public IBackupManagerMonitor monitor;
+    public long token;
+    public PackageInfo pkgInfo;
+    public int pmToken; // in post-install restore, the PM's token for this transaction
+    public boolean isSystemRestore;
+    public String[] filterSet;
+
+    /**
+     * Restore a single package; no kill after restore
+     */
+    public RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
+        IBackupManagerMonitor _monitor, long _token, PackageInfo _pkg) {
+        transport = _transport;
+        dirName = _dirName;
+        observer = _obs;
+        monitor = _monitor;
+        token = _token;
+        pkgInfo = _pkg;
+        pmToken = 0;
+        isSystemRestore = false;
+        filterSet = null;
+    }
+
+    /**
+     * Restore at install: PM token needed, kill after restore
+     */
+    public RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
+        IBackupManagerMonitor _monitor, long _token, String _pkgName, int _pmToken) {
+        transport = _transport;
+        dirName = _dirName;
+        observer = _obs;
+        monitor = _monitor;
+        token = _token;
+        pkgInfo = null;
+        pmToken = _pmToken;
+        isSystemRestore = false;
+        filterSet = new String[]{_pkgName};
+    }
+
+    /**
+     * Restore everything possible.  This is the form that Setup Wizard or similar
+     * restore UXes use.
+     */
+    public RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
+        IBackupManagerMonitor _monitor, long _token) {
+        transport = _transport;
+        dirName = _dirName;
+        observer = _obs;
+        monitor = _monitor;
+        token = _token;
+        pkgInfo = null;
+        pmToken = 0;
+        isSystemRestore = true;
+        filterSet = null;
+    }
+
+    /**
+     * Restore some set of packages.  Leave this one up to the caller to specify
+     * whether it's to be considered a system-level restore.
+     */
+    public RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
+        IBackupManagerMonitor _monitor, long _token,
+        String[] _filterSet, boolean _isSystemRestore) {
+        transport = _transport;
+        dirName = _dirName;
+        observer = _obs;
+        monitor = _monitor;
+        token = _token;
+        pkgInfo = null;
+        pmToken = 0;
+        isSystemRestore = _isSystemRestore;
+        filterSet = _filterSet;
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/restore/ActiveRestoreSession.java b/services/backup/java/com/android/server/backup/restore/ActiveRestoreSession.java
new file mode 100644
index 0000000..090d8f8
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/restore/ActiveRestoreSession.java
@@ -0,0 +1,398 @@
+/*
+ * Copyright (C) 2017 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.server.backup.restore;
+
+import android.app.backup.IBackupManagerMonitor;
+import android.app.backup.IRestoreObserver;
+import android.app.backup.IRestoreSession;
+import android.app.backup.RestoreSet;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.os.Binder;
+import android.os.Message;
+import android.util.Slog;
+import com.android.internal.backup.IBackupTransport;
+import com.android.server.backup.RefactoredBackupManagerService;
+import com.android.server.backup.params.RestoreGetSetsParams;
+import com.android.server.backup.params.RestoreParams;
+
+/**
+ * Restore session.
+ */
+public class ActiveRestoreSession extends IRestoreSession.Stub {
+
+    private static final String TAG = "RestoreSession";
+
+    private RefactoredBackupManagerService backupManagerService;
+    private String mPackageName;
+    private IBackupTransport mRestoreTransport = null;
+    public RestoreSet[] mRestoreSets = null;
+    boolean mEnded = false;
+    boolean mTimedOut = false;
+
+    public ActiveRestoreSession(RefactoredBackupManagerService backupManagerService,
+        String packageName, String transport) {
+        this.backupManagerService = backupManagerService;
+        mPackageName = packageName;
+        mRestoreTransport = backupManagerService.mTransportManager.getTransportBinder(transport);
+    }
+
+    public void markTimedOut() {
+        mTimedOut = true;
+    }
+
+    // --- Binder interface ---
+    public synchronized int getAvailableRestoreSets(IRestoreObserver observer,
+        IBackupManagerMonitor monitor) {
+        backupManagerService.mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
+            "getAvailableRestoreSets");
+        if (observer == null) {
+            throw new IllegalArgumentException("Observer must not be null");
+        }
+
+        if (mEnded) {
+            throw new IllegalStateException("Restore session already ended");
+        }
+
+        if (mTimedOut) {
+            Slog.i(TAG, "Session already timed out");
+            return -1;
+        }
+
+        long oldId = Binder.clearCallingIdentity();
+        try {
+            if (mRestoreTransport == null) {
+                Slog.w(TAG, "Null transport getting restore sets");
+                return -1;
+            }
+
+            // We know we're doing legit work now, so halt the timeout
+            // until we're done.  It gets started again when the result
+            // comes in.
+            backupManagerService.mBackupHandler.removeMessages(
+                RefactoredBackupManagerService.MSG_RESTORE_SESSION_TIMEOUT);
+
+            // spin off the transport request to our service thread
+            backupManagerService.mWakelock.acquire();
+            Message msg = backupManagerService.mBackupHandler.obtainMessage(
+                RefactoredBackupManagerService.MSG_RUN_GET_RESTORE_SETS,
+                new RestoreGetSetsParams(mRestoreTransport, this, observer,
+                    monitor));
+            backupManagerService.mBackupHandler.sendMessage(msg);
+            return 0;
+        } catch (Exception e) {
+            Slog.e(TAG, "Error in getAvailableRestoreSets", e);
+            return -1;
+        } finally {
+            Binder.restoreCallingIdentity(oldId);
+        }
+    }
+
+    public synchronized int restoreAll(long token, IRestoreObserver observer,
+        IBackupManagerMonitor monitor) {
+        backupManagerService.mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
+            "performRestore");
+
+        if (RefactoredBackupManagerService.DEBUG) {
+          Slog.d(TAG, "restoreAll token=" + Long.toHexString(token)
+              + " observer=" + observer);
+        }
+
+        if (mEnded) {
+            throw new IllegalStateException("Restore session already ended");
+        }
+
+        if (mTimedOut) {
+            Slog.i(TAG, "Session already timed out");
+            return -1;
+        }
+
+        if (mRestoreTransport == null || mRestoreSets == null) {
+            Slog.e(TAG, "Ignoring restoreAll() with no restore set");
+            return -1;
+        }
+
+        if (mPackageName != null) {
+            Slog.e(TAG, "Ignoring restoreAll() on single-package session");
+            return -1;
+        }
+
+        String dirName;
+        try {
+            dirName = mRestoreTransport.transportDirName();
+        } catch (Exception e) {
+            // Transport went AWOL; fail.
+            Slog.e(TAG, "Unable to get transport dir for restore: " + e.getMessage());
+            return -1;
+        }
+
+        synchronized (backupManagerService.mQueueLock) {
+            for (int i = 0; i < mRestoreSets.length; i++) {
+                if (token == mRestoreSets[i].token) {
+                    // Real work, so stop the session timeout until we finalize the restore
+                    backupManagerService.mBackupHandler.removeMessages(
+                        RefactoredBackupManagerService.MSG_RESTORE_SESSION_TIMEOUT);
+
+                    long oldId = Binder.clearCallingIdentity();
+                    backupManagerService.mWakelock.acquire();
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                        Slog.d(TAG, "restoreAll() kicking off");
+                    }
+                    Message msg = backupManagerService.mBackupHandler.obtainMessage(
+                        RefactoredBackupManagerService.MSG_RUN_RESTORE);
+                    msg.obj = new RestoreParams(mRestoreTransport, dirName,
+                        observer, monitor, token);
+                    backupManagerService.mBackupHandler.sendMessage(msg);
+                    Binder.restoreCallingIdentity(oldId);
+                    return 0;
+                }
+            }
+        }
+
+        Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found");
+        return -1;
+    }
+
+    // Restores of more than a single package are treated as 'system' restores
+    public synchronized int restoreSome(long token, IRestoreObserver observer,
+        IBackupManagerMonitor monitor, String[] packages) {
+        backupManagerService.mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
+            "performRestore");
+
+        if (RefactoredBackupManagerService.DEBUG) {
+            StringBuilder b = new StringBuilder(128);
+            b.append("restoreSome token=");
+            b.append(Long.toHexString(token));
+            b.append(" observer=");
+            b.append(observer.toString());
+            b.append(" monitor=");
+            if (monitor == null) {
+                b.append("null");
+            } else {
+                b.append(monitor.toString());
+            }
+            b.append(" packages=");
+            if (packages == null) {
+                b.append("null");
+            } else {
+                b.append('{');
+                boolean first = true;
+                for (String s : packages) {
+                    if (!first) {
+                        b.append(", ");
+                    } else {
+                      first = false;
+                    }
+                    b.append(s);
+                }
+                b.append('}');
+            }
+            Slog.d(TAG, b.toString());
+        }
+
+        if (mEnded) {
+            throw new IllegalStateException("Restore session already ended");
+        }
+
+        if (mTimedOut) {
+            Slog.i(TAG, "Session already timed out");
+            return -1;
+        }
+
+        if (mRestoreTransport == null || mRestoreSets == null) {
+            Slog.e(TAG, "Ignoring restoreAll() with no restore set");
+            return -1;
+        }
+
+        if (mPackageName != null) {
+            Slog.e(TAG, "Ignoring restoreAll() on single-package session");
+            return -1;
+        }
+
+        String dirName;
+        try {
+            dirName = mRestoreTransport.transportDirName();
+        } catch (Exception e) {
+            // Transport went AWOL; fail.
+            Slog.e(TAG, "Unable to get transport name for restoreSome: " + e.getMessage());
+            return -1;
+        }
+
+        synchronized (backupManagerService.mQueueLock) {
+            for (int i = 0; i < mRestoreSets.length; i++) {
+                if (token == mRestoreSets[i].token) {
+                    // Stop the session timeout until we finalize the restore
+                    backupManagerService.mBackupHandler.removeMessages(
+                        RefactoredBackupManagerService.MSG_RESTORE_SESSION_TIMEOUT);
+
+                    long oldId = Binder.clearCallingIdentity();
+                    backupManagerService.mWakelock.acquire();
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                        Slog.d(TAG, "restoreSome() of " + packages.length + " packages");
+                    }
+                    Message msg = backupManagerService.mBackupHandler.obtainMessage(
+                        RefactoredBackupManagerService.MSG_RUN_RESTORE);
+                    msg.obj = new RestoreParams(mRestoreTransport, dirName, observer, monitor,
+                        token, packages, packages.length > 1);
+                    backupManagerService.mBackupHandler.sendMessage(msg);
+                    Binder.restoreCallingIdentity(oldId);
+                    return 0;
+                }
+            }
+        }
+
+        Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found");
+        return -1;
+    }
+
+    public synchronized int restorePackage(String packageName, IRestoreObserver observer,
+        IBackupManagerMonitor monitor) {
+        if (RefactoredBackupManagerService.DEBUG) {
+          Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer
+              + "monitor=" + monitor);
+        }
+
+        if (mEnded) {
+            throw new IllegalStateException("Restore session already ended");
+        }
+
+        if (mTimedOut) {
+            Slog.i(TAG, "Session already timed out");
+            return -1;
+        }
+
+        if (mPackageName != null) {
+            if (!mPackageName.equals(packageName)) {
+                Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName
+                    + " on session for package " + mPackageName);
+                return -1;
+            }
+        }
+
+        PackageInfo app = null;
+        try {
+            app = backupManagerService.mPackageManager.getPackageInfo(packageName, 0);
+        } catch (NameNotFoundException nnf) {
+            Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName);
+            return -1;
+        }
+
+        // If the caller is not privileged and is not coming from the target
+        // app's uid, throw a permission exception back to the caller.
+        int perm = backupManagerService.mContext.checkPermission(android.Manifest.permission.BACKUP,
+            Binder.getCallingPid(), Binder.getCallingUid());
+        if ((perm == PackageManager.PERMISSION_DENIED) &&
+            (app.applicationInfo.uid != Binder.getCallingUid())) {
+            Slog.w(TAG, "restorePackage: bad packageName=" + packageName
+                + " or calling uid=" + Binder.getCallingUid());
+            throw new SecurityException("No permission to restore other packages");
+        }
+
+        // So far so good; we're allowed to try to restore this package.
+        long oldId = Binder.clearCallingIdentity();
+        try {
+            // Check whether there is data for it in the current dataset, falling back
+            // to the ancestral dataset if not.
+            long token = backupManagerService.getAvailableRestoreToken(packageName);
+            if (RefactoredBackupManagerService.DEBUG) {
+              Slog.v(TAG, "restorePackage pkg=" + packageName
+                  + " token=" + Long.toHexString(token));
+            }
+
+            // If we didn't come up with a place to look -- no ancestral dataset and
+            // the app has never been backed up from this device -- there's nothing
+            // to do but return failure.
+            if (token == 0) {
+                if (RefactoredBackupManagerService.DEBUG) {
+                  Slog.w(TAG, "No data available for this package; not restoring");
+                }
+                return -1;
+            }
+
+            String dirName;
+            try {
+                dirName = mRestoreTransport.transportDirName();
+            } catch (Exception e) {
+                // Transport went AWOL; fail.
+                Slog.e(TAG,
+                    "Unable to get transport dir for restorePackage: " + e.getMessage());
+                return -1;
+            }
+
+            // Stop the session timeout until we finalize the restore
+            backupManagerService.mBackupHandler.removeMessages(
+                RefactoredBackupManagerService.MSG_RESTORE_SESSION_TIMEOUT);
+
+            // Ready to go:  enqueue the restore request and claim success
+            backupManagerService.mWakelock.acquire();
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.d(TAG, "restorePackage() : " + packageName);
+            }
+            Message msg = backupManagerService.mBackupHandler.obtainMessage(
+                RefactoredBackupManagerService.MSG_RUN_RESTORE);
+            msg.obj = new RestoreParams(mRestoreTransport, dirName, observer, monitor,
+                token, app);
+            backupManagerService.mBackupHandler.sendMessage(msg);
+        } finally {
+            Binder.restoreCallingIdentity(oldId);
+        }
+        return 0;
+    }
+
+    // Posted to the handler to tear down a restore session in a cleanly synchronized way
+    public class EndRestoreRunnable implements Runnable {
+
+      RefactoredBackupManagerService mBackupManager;
+        ActiveRestoreSession mSession;
+
+        public EndRestoreRunnable(RefactoredBackupManagerService manager,
+            ActiveRestoreSession session) {
+            mBackupManager = manager;
+            mSession = session;
+        }
+
+        public void run() {
+            // clean up the session's bookkeeping
+            synchronized (mSession) {
+                mSession.mRestoreTransport = null;
+                mSession.mEnded = true;
+            }
+
+            // clean up the BackupManagerImpl side of the bookkeeping
+            // and cancel any pending timeout message
+            mBackupManager.clearRestoreSession(mSession);
+        }
+    }
+
+    public synchronized void endRestoreSession() {
+        if (RefactoredBackupManagerService.DEBUG) {
+          Slog.d(TAG, "endRestoreSession");
+        }
+
+        if (mTimedOut) {
+            Slog.i(TAG, "Session already timed out");
+            return;
+        }
+
+        if (mEnded) {
+            throw new IllegalStateException("Restore session already ended");
+        }
+
+        backupManagerService.mBackupHandler.post(new EndRestoreRunnable(backupManagerService, this));
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/restore/AdbRestoreFinishedLatch.java b/services/backup/java/com/android/server/backup/restore/AdbRestoreFinishedLatch.java
new file mode 100644
index 0000000..2360b5c
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/restore/AdbRestoreFinishedLatch.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2017 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.server.backup.restore;
+
+import android.util.Slog;
+import com.android.server.backup.BackupRestoreTask;
+import com.android.server.backup.RefactoredBackupManagerService;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Used for synchronizing doRestoreFinished during adb restore.
+ */
+public class AdbRestoreFinishedLatch implements BackupRestoreTask {
+
+  private static final String TAG = "AdbRestoreFinishedLatch";
+    private RefactoredBackupManagerService backupManagerService;
+    final CountDownLatch mLatch;
+    private final int mCurrentOpToken;
+
+    public AdbRestoreFinishedLatch(RefactoredBackupManagerService backupManagerService,
+        int currentOpToken) {
+        this.backupManagerService = backupManagerService;
+        mLatch = new CountDownLatch(1);
+        mCurrentOpToken = currentOpToken;
+    }
+
+    void await() {
+        boolean latched = false;
+        try {
+            latched = mLatch.await(RefactoredBackupManagerService.TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException e) {
+            Slog.w(TAG, "Interrupted!");
+        }
+    }
+
+    @Override
+    public void execute() {
+        // Unused
+    }
+
+    @Override
+    public void operationComplete(long result) {
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+            Slog.w(TAG, "adb onRestoreFinished() complete");
+        }
+        mLatch.countDown();
+        backupManagerService.removeOperation(mCurrentOpToken);
+    }
+
+    @Override
+    public void handleCancel(boolean cancelAll) {
+        if (RefactoredBackupManagerService.DEBUG) {
+            Slog.w(TAG, "adb onRestoreFinished() timed out");
+        }
+        mLatch.countDown();
+        backupManagerService.removeOperation(mCurrentOpToken);
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/restore/FullRestoreEngine.java b/services/backup/java/com/android/server/backup/restore/FullRestoreEngine.java
new file mode 100644
index 0000000..5722543
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/restore/FullRestoreEngine.java
@@ -0,0 +1,1486 @@
+/*
+ * Copyright (C) 2017 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.server.backup.restore;
+
+import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_NAME;
+import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_VERSION;
+import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_MANIFEST_PACKAGE_NAME;
+import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_OLD_VERSION;
+import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_POLICY_ALLOW_APKS;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_APK_NOT_INSTALLED;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_CANNOT_RESTORE_WITHOUT_APK;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_EXPECTED_DIFFERENT_PACKAGE;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_ALLOW_BACKUP_FALSE;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_SIGNATURE_MISMATCH;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_MISSING_SIGNATURE;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_RESTORE_ANY_VERSION;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_SYSTEM_APP_NO_AGENT;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_VERSIONS_MATCH;
+import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_VERSION_OF_BACKUP_OLDER;
+
+import android.app.ApplicationThreadConstants;
+import android.app.IBackupAgent;
+import android.app.PackageInstallObserver;
+import android.app.backup.BackupAgent;
+import android.app.backup.BackupManagerMonitor;
+import android.app.backup.FullBackup;
+import android.app.backup.IBackupManagerMonitor;
+import android.app.backup.IFullBackupRestoreObserver;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.IPackageDeleteObserver;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.pm.Signature;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.ParcelFileDescriptor;
+import android.os.Process;
+import android.os.RemoteException;
+import android.util.Slog;
+import com.android.server.backup.BackupRestoreTask;
+import com.android.server.backup.FileMetadata;
+import com.android.server.backup.RefactoredBackupManagerService;
+import com.android.server.backup.fullbackup.FullBackupObbConnection;
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * Full restore engine, used by both adb restore and transport-based full restore.
+ */
+public class FullRestoreEngine extends RestoreEngine {
+
+    private RefactoredBackupManagerService backupManagerService;
+    // Task in charge of monitoring timeouts
+    BackupRestoreTask mMonitorTask;
+
+    // Dedicated observer, if any
+    IFullBackupRestoreObserver mObserver;
+
+    IBackupManagerMonitor mMonitor;
+
+    // Where we're delivering the file data as we go
+    IBackupAgent mAgent;
+
+    // Are we permitted to only deliver a specific package's metadata?
+    PackageInfo mOnlyPackage;
+
+    boolean mAllowApks;
+    boolean mAllowObbs;
+
+    // Which package are we currently handling data for?
+    String mAgentPackage;
+
+    // Info for working with the target app process
+    ApplicationInfo mTargetApp;
+
+    // Machinery for restoring OBBs
+    FullBackupObbConnection mObbConnection = null;
+
+    // possible handling states for a given package in the restore dataset
+    final HashMap<String, RestorePolicy> mPackagePolicies
+        = new HashMap<>();
+
+    // installer package names for each encountered app, derived from the manifests
+    final HashMap<String, String> mPackageInstallers = new HashMap<>();
+
+    // Signatures for a given package found in its manifest file
+    final HashMap<String, Signature[]> mManifestSignatures
+        = new HashMap<>();
+
+    // Packages we've already wiped data on when restoring their first file
+    final HashSet<String> mClearedPackages = new HashSet<>();
+
+    // How much data have we moved?
+    long mBytes;
+
+    // Working buffer
+    byte[] mBuffer;
+
+    // Pipes for moving data
+    ParcelFileDescriptor[] mPipes = null;
+
+    // Widget blob to be restored out-of-band
+    byte[] mWidgetData = null;
+
+    private final int mEphemeralOpToken;
+
+    // Runner that can be placed in a separate thread to do in-process
+    // invocations of the full restore API asynchronously. Used by adb restore.
+    class RestoreFileRunnable implements Runnable {
+
+      IBackupAgent mAgent;
+        FileMetadata mInfo;
+        ParcelFileDescriptor mSocket;
+        int mToken;
+
+        RestoreFileRunnable(IBackupAgent agent, FileMetadata info,
+            ParcelFileDescriptor socket, int token) throws IOException {
+            mAgent = agent;
+            mInfo = info;
+            mToken = token;
+
+            // This class is used strictly for process-local binder invocations.  The
+            // semantics of ParcelFileDescriptor differ in this case; in particular, we
+            // do not automatically get a 'dup'ed descriptor that we can can continue
+            // to use asynchronously from the caller.  So, we make sure to dup it ourselves
+            // before proceeding to do the restore.
+            mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor());
+        }
+
+        @Override
+        public void run() {
+            try {
+                mAgent.doRestoreFile(mSocket, mInfo.size, mInfo.type,
+                    mInfo.domain, mInfo.path, mInfo.mode, mInfo.mtime,
+                    mToken, backupManagerService.mBackupManagerBinder);
+            } catch (RemoteException e) {
+                // never happens; this is used strictly for local binder calls
+            }
+        }
+    }
+
+    public FullRestoreEngine(RefactoredBackupManagerService backupManagerService,
+        BackupRestoreTask monitorTask, IFullBackupRestoreObserver observer,
+        IBackupManagerMonitor monitor, PackageInfo onlyPackage, boolean allowApks,
+        boolean allowObbs, int ephemeralOpToken) {
+        this.backupManagerService = backupManagerService;
+        mEphemeralOpToken = ephemeralOpToken;
+        mMonitorTask = monitorTask;
+        mObserver = observer;
+        mMonitor = monitor;
+        mOnlyPackage = onlyPackage;
+        mAllowApks = allowApks;
+        mAllowObbs = allowObbs;
+        mBuffer = new byte[32 * 1024];
+        mBytes = 0;
+    }
+
+    public IBackupAgent getAgent() {
+        return mAgent;
+    }
+
+    public byte[] getWidgetData() {
+        return mWidgetData;
+    }
+
+    public boolean restoreOneFile(InputStream instream, boolean mustKillAgent) {
+        if (!isRunning()) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Restore engine used after halting");
+            return false;
+        }
+
+        FileMetadata info;
+        try {
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.v(RefactoredBackupManagerService.TAG, "Reading tar header for restoring file");
+            }
+            info = readTarHeaders(instream);
+            if (info != null) {
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    dumpFileMetadata(info);
+                }
+
+                final String pkg = info.packageName;
+                if (!pkg.equals(mAgentPackage)) {
+                    // In the single-package case, it's a semantic error to expect
+                    // one app's data but see a different app's on the wire
+                    if (mOnlyPackage != null) {
+                        if (!pkg.equals(mOnlyPackage.packageName)) {
+                            Slog.w(RefactoredBackupManagerService.TAG, "Expected data for " + mOnlyPackage
+                                + " but saw " + pkg);
+                            setResult(RestoreEngine.TRANSPORT_FAILURE);
+                            setRunning(false);
+                            return false;
+                        }
+                    }
+
+                    // okay, change in package; set up our various
+                    // bookkeeping if we haven't seen it yet
+                    if (!mPackagePolicies.containsKey(pkg)) {
+                        mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
+                    }
+
+                    // Clean up the previous agent relationship if necessary,
+                    // and let the observer know we're considering a new app.
+                    if (mAgent != null) {
+                        if (RefactoredBackupManagerService.DEBUG) {
+                          Slog.d(RefactoredBackupManagerService.TAG, "Saw new package; finalizing old one");
+                        }
+                        // Now we're really done
+                        tearDownPipes();
+                        tearDownAgent(mTargetApp);
+                        mTargetApp = null;
+                        mAgentPackage = null;
+                    }
+                }
+
+                if (info.path.equals(RefactoredBackupManagerService.BACKUP_MANIFEST_FILENAME)) {
+                    mPackagePolicies.put(pkg, readAppManifest(info, instream));
+                    mPackageInstallers.put(pkg, info.installerPackageName);
+                    // We've read only the manifest content itself at this point,
+                    // so consume the footer before looping around to the next
+                    // input file
+                    skipTarPadding(info.size, instream);
+                    sendOnRestorePackage(pkg);
+                } else if (info.path.equals(RefactoredBackupManagerService.BACKUP_METADATA_FILENAME)) {
+                    // Metadata blobs!
+                    readMetadata(info, instream);
+                    skipTarPadding(info.size, instream);
+                } else {
+                    // Non-manifest, so it's actual file data.  Is this a package
+                    // we're ignoring?
+                    boolean okay = true;
+                    RestorePolicy policy = mPackagePolicies.get(pkg);
+                    switch (policy) {
+                        case IGNORE:
+                            okay = false;
+                            break;
+
+                        case ACCEPT_IF_APK:
+                            // If we're in accept-if-apk state, then the first file we
+                            // see MUST be the apk.
+                            if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.d(RefactoredBackupManagerService.TAG, "APK file; installing");
+                                }
+                                // Try to install the app.
+                                String installerName = mPackageInstallers.get(pkg);
+                                okay = installApk(info, installerName, instream);
+                                // good to go; promote to ACCEPT
+                                mPackagePolicies.put(pkg, (okay)
+                                    ? RestorePolicy.ACCEPT
+                                    : RestorePolicy.IGNORE);
+                                // At this point we've consumed this file entry
+                                // ourselves, so just strip the tar footer and
+                                // go on to the next file in the input stream
+                                skipTarPadding(info.size, instream);
+                                return true;
+                            } else {
+                                // File data before (or without) the apk.  We can't
+                                // handle it coherently in this case so ignore it.
+                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
+                                okay = false;
+                            }
+                            break;
+
+                        case ACCEPT:
+                            if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.d(RefactoredBackupManagerService.TAG, "apk present but ACCEPT");
+                                }
+                                // we can take the data without the apk, so we
+                                // *want* to do so.  skip the apk by declaring this
+                                // one file not-okay without changing the restore
+                                // policy for the package.
+                                okay = false;
+                            }
+                            break;
+
+                        default:
+                            // Something has gone dreadfully wrong when determining
+                            // the restore policy from the manifest.  Ignore the
+                            // rest of this package's data.
+                            Slog.e(RefactoredBackupManagerService.TAG, "Invalid policy from manifest");
+                            okay = false;
+                            mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
+                            break;
+                    }
+
+                    // Is it a *file* we need to drop?
+                    if (!isRestorableFile(info)) {
+                        okay = false;
+                    }
+
+                    // If the policy is satisfied, go ahead and set up to pipe the
+                    // data to the agent.
+                    if (RefactoredBackupManagerService.MORE_DEBUG && okay && mAgent != null) {
+                        Slog.i(RefactoredBackupManagerService.TAG, "Reusing existing agent instance");
+                    }
+                    if (okay && mAgent == null) {
+                        if (RefactoredBackupManagerService.MORE_DEBUG) {
+                          Slog.d(RefactoredBackupManagerService.TAG, "Need to launch agent for " + pkg);
+                        }
+
+                        try {
+                            mTargetApp = backupManagerService.mPackageManager.getApplicationInfo(pkg, 0);
+
+                            // If we haven't sent any data to this app yet, we probably
+                            // need to clear it first.  Check that.
+                            if (!mClearedPackages.contains(pkg)) {
+                                // apps with their own backup agents are
+                                // responsible for coherently managing a full
+                                // restore.
+                                if (mTargetApp.backupAgentName == null) {
+                                    if (RefactoredBackupManagerService.DEBUG) {
+                                      Slog.d(RefactoredBackupManagerService.TAG,
+                                          "Clearing app data preparatory to full restore");
+                                    }
+                                    backupManagerService.clearApplicationDataSynchronous(pkg);
+                                } else {
+                                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                                      Slog.d(RefactoredBackupManagerService.TAG, "backup agent ("
+                                          + mTargetApp.backupAgentName + ") => no clear");
+                                    }
+                                }
+                                mClearedPackages.add(pkg);
+                            } else {
+                                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                                    Slog.d(RefactoredBackupManagerService.TAG,
+                                        "We've initialized this app already; no clear required");
+                                }
+                            }
+
+                            // All set; now set up the IPC and launch the agent
+                            setUpPipes();
+                            mAgent = backupManagerService.bindToAgentSynchronous(mTargetApp,
+                                ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL);
+                            mAgentPackage = pkg;
+                        } catch (IOException e) {
+                            // fall through to error handling
+                        } catch (NameNotFoundException e) {
+                            // fall through to error handling
+                        }
+
+                        if (mAgent == null) {
+                            Slog.e(
+                                RefactoredBackupManagerService.TAG, "Unable to create agent for " + pkg);
+                            okay = false;
+                            tearDownPipes();
+                            mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
+                        }
+                    }
+
+                    // Sanity check: make sure we never give data to the wrong app.  This
+                    // should never happen but a little paranoia here won't go amiss.
+                    if (okay && !pkg.equals(mAgentPackage)) {
+                        Slog.e(RefactoredBackupManagerService.TAG, "Restoring data for " + pkg
+                            + " but agent is for " + mAgentPackage);
+                        okay = false;
+                    }
+
+                    // At this point we have an agent ready to handle the full
+                    // restore data as well as a pipe for sending data to
+                    // that agent.  Tell the agent to start reading from the
+                    // pipe.
+                    if (okay) {
+                        boolean agentSuccess = true;
+                        long toCopy = info.size;
+                        try {
+                            backupManagerService.prepareOperationTimeout(mEphemeralOpToken,
+                                RefactoredBackupManagerService.TIMEOUT_FULL_BACKUP_INTERVAL, mMonitorTask,
+                                RefactoredBackupManagerService.OP_TYPE_RESTORE_WAIT);
+
+                            if (info.domain.equals(FullBackup.OBB_TREE_TOKEN)) {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.d(RefactoredBackupManagerService.TAG, "Restoring OBB file for " + pkg
+                                      + " : " + info.path);
+                                }
+                                mObbConnection.restoreObbFile(pkg, mPipes[0],
+                                    info.size, info.type, info.path, info.mode,
+                                    info.mtime, mEphemeralOpToken, backupManagerService.mBackupManagerBinder);
+                            } else {
+                                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                                  Slog.d(RefactoredBackupManagerService.TAG, "Invoking agent to restore file "
+                                      + info.path);
+                                }
+                                // fire up the app's agent listening on the socket.  If
+                                // the agent is running in the system process we can't
+                                // just invoke it asynchronously, so we provide a thread
+                                // for it here.
+                                if (mTargetApp.processName.equals("system")) {
+                                    Slog.d(RefactoredBackupManagerService.TAG, "system process agent - spinning a thread");
+                                    RestoreFileRunnable runner = new RestoreFileRunnable(
+                                        mAgent, info, mPipes[0], mEphemeralOpToken);
+                                    new Thread(runner, "restore-sys-runner").start();
+                                } else {
+                                    mAgent.doRestoreFile(mPipes[0], info.size, info.type,
+                                        info.domain, info.path, info.mode, info.mtime,
+                                        mEphemeralOpToken, backupManagerService.mBackupManagerBinder);
+                                }
+                            }
+                        } catch (IOException e) {
+                            // couldn't dup the socket for a process-local restore
+                            Slog.d(RefactoredBackupManagerService.TAG, "Couldn't establish restore");
+                            agentSuccess = false;
+                            okay = false;
+                        } catch (RemoteException e) {
+                            // whoops, remote entity went away.  We'll eat the content
+                            // ourselves, then, and not copy it over.
+                            Slog.e(RefactoredBackupManagerService.TAG, "Agent crashed during full restore");
+                            agentSuccess = false;
+                            okay = false;
+                        }
+
+                        // Copy over the data if the agent is still good
+                        if (okay) {
+                            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                                Slog.v(RefactoredBackupManagerService.TAG, "  copying to restore agent: "
+                                    + toCopy + " bytes");
+                            }
+                            boolean pipeOkay = true;
+                            FileOutputStream pipe = new FileOutputStream(
+                                mPipes[1].getFileDescriptor());
+                            while (toCopy > 0) {
+                                int toRead = (toCopy > mBuffer.length)
+                                    ? mBuffer.length : (int) toCopy;
+                                int nRead = instream.read(mBuffer, 0, toRead);
+                                if (nRead >= 0) {
+                                  mBytes += nRead;
+                                }
+                                if (nRead <= 0) {
+                                  break;
+                                }
+                                toCopy -= nRead;
+
+                                // send it to the output pipe as long as things
+                                // are still good
+                                if (pipeOkay) {
+                                    try {
+                                        pipe.write(mBuffer, 0, nRead);
+                                    } catch (IOException e) {
+                                        Slog.e(RefactoredBackupManagerService.TAG, "Failed to write to restore pipe: "
+                                            + e.getMessage());
+                                        pipeOkay = false;
+                                    }
+                                }
+                            }
+
+                            // done sending that file!  Now we just need to consume
+                            // the delta from info.size to the end of block.
+                            skipTarPadding(info.size, instream);
+
+                            // and now that we've sent it all, wait for the remote
+                            // side to acknowledge receipt
+                            agentSuccess = backupManagerService.waitUntilOperationComplete(mEphemeralOpToken);
+                        }
+
+                        // okay, if the remote end failed at any point, deal with
+                        // it by ignoring the rest of the restore on it
+                        if (!agentSuccess) {
+                            Slog.w(RefactoredBackupManagerService.TAG, "Agent failure; ending restore");
+                            backupManagerService.mBackupHandler.removeMessages(
+                                RefactoredBackupManagerService.MSG_RESTORE_OPERATION_TIMEOUT);
+                            tearDownPipes();
+                            tearDownAgent(mTargetApp);
+                            mAgent = null;
+                            mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
+
+                            // If this was a single-package restore, we halt immediately
+                            // with an agent error under these circumstances
+                            if (mOnlyPackage != null) {
+                                setResult(RestoreEngine.TARGET_FAILURE);
+                                setRunning(false);
+                                return false;
+                            }
+                        }
+                    }
+
+                    // Problems setting up the agent communication, an explicitly
+                    // dropped file, or an already-ignored package: skip to the
+                    // next stream entry by reading and discarding this file.
+                    if (!okay) {
+                        if (RefactoredBackupManagerService.MORE_DEBUG) {
+                          Slog.d(RefactoredBackupManagerService.TAG, "[discarding file content]");
+                        }
+                        long bytesToConsume = (info.size + 511) & ~511;
+                        while (bytesToConsume > 0) {
+                            int toRead = (bytesToConsume > mBuffer.length)
+                                ? mBuffer.length : (int) bytesToConsume;
+                            long nRead = instream.read(mBuffer, 0, toRead);
+                            if (nRead >= 0) {
+                              mBytes += nRead;
+                            }
+                            if (nRead <= 0) {
+                              break;
+                            }
+                            bytesToConsume -= nRead;
+                        }
+                    }
+                }
+            }
+        } catch (IOException e) {
+            if (RefactoredBackupManagerService.DEBUG) {
+              Slog.w(RefactoredBackupManagerService.TAG, "io exception on restore socket read: " + e.getMessage());
+            }
+            setResult(RestoreEngine.TRANSPORT_FAILURE);
+            info = null;
+        }
+
+        // If we got here we're either running smoothly or we've finished
+        if (info == null) {
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.i(RefactoredBackupManagerService.TAG, "No [more] data for this package; tearing down");
+            }
+            tearDownPipes();
+            setRunning(false);
+            if (mustKillAgent) {
+                tearDownAgent(mTargetApp);
+            }
+        }
+        return (info != null);
+    }
+
+    void setUpPipes() throws IOException {
+        mPipes = ParcelFileDescriptor.createPipe();
+    }
+
+    void tearDownPipes() {
+        // Teardown might arise from the inline restore processing or from the asynchronous
+        // timeout mechanism, and these might race.  Make sure we don't try to close and
+        // null out the pipes twice.
+        synchronized (this) {
+            if (mPipes != null) {
+                try {
+                    mPipes[0].close();
+                    mPipes[0] = null;
+                    mPipes[1].close();
+                    mPipes[1] = null;
+                } catch (IOException e) {
+                    Slog.w(RefactoredBackupManagerService.TAG, "Couldn't close agent pipes", e);
+                }
+                mPipes = null;
+            }
+        }
+    }
+
+    void tearDownAgent(ApplicationInfo app) {
+        if (mAgent != null) {
+            backupManagerService.tearDownAgentAndKill(app);
+            mAgent = null;
+        }
+    }
+
+    void handleTimeout() {
+        tearDownPipes();
+        setResult(RestoreEngine.TARGET_FAILURE);
+        setRunning(false);
+    }
+
+    class RestoreInstallObserver extends PackageInstallObserver {
+
+      final AtomicBoolean mDone = new AtomicBoolean();
+        String mPackageName;
+        int mResult;
+
+        public void reset() {
+            synchronized (mDone) {
+                mDone.set(false);
+            }
+        }
+
+        public void waitForCompletion() {
+            synchronized (mDone) {
+                while (mDone.get() == false) {
+                    try {
+                        mDone.wait();
+                    } catch (InterruptedException e) {
+                    }
+                }
+            }
+        }
+
+        int getResult() {
+            return mResult;
+        }
+
+        @Override
+        public void onPackageInstalled(String packageName, int returnCode,
+            String msg, Bundle extras) {
+            synchronized (mDone) {
+                mResult = returnCode;
+                mPackageName = packageName;
+                mDone.set(true);
+                mDone.notifyAll();
+            }
+        }
+    }
+
+    class RestoreDeleteObserver extends IPackageDeleteObserver.Stub {
+
+      final AtomicBoolean mDone = new AtomicBoolean();
+        int mResult;
+
+        public void reset() {
+            synchronized (mDone) {
+                mDone.set(false);
+            }
+        }
+
+        public void waitForCompletion() {
+            synchronized (mDone) {
+                while (mDone.get() == false) {
+                    try {
+                        mDone.wait();
+                    } catch (InterruptedException e) {
+                    }
+                }
+            }
+        }
+
+        @Override
+        public void packageDeleted(String packageName, int returnCode) throws RemoteException {
+            synchronized (mDone) {
+                mResult = returnCode;
+                mDone.set(true);
+                mDone.notifyAll();
+            }
+        }
+    }
+
+    final RestoreInstallObserver mInstallObserver = new RestoreInstallObserver();
+    final RestoreDeleteObserver mDeleteObserver = new RestoreDeleteObserver();
+
+    boolean installApk(FileMetadata info, String installerPackage, InputStream instream) {
+        boolean okay = true;
+
+        if (RefactoredBackupManagerService.DEBUG) {
+          Slog.d(RefactoredBackupManagerService.TAG, "Installing from backup: " + info.packageName);
+        }
+
+        // The file content is an .apk file.  Copy it out to a staging location and
+        // attempt to install it.
+        File apkFile = new File(backupManagerService.mDataDir, info.packageName);
+        try {
+            FileOutputStream apkStream = new FileOutputStream(apkFile);
+            byte[] buffer = new byte[32 * 1024];
+            long size = info.size;
+            while (size > 0) {
+                long toRead = (buffer.length < size) ? buffer.length : size;
+                int didRead = instream.read(buffer, 0, (int) toRead);
+                if (didRead >= 0) {
+                  mBytes += didRead;
+                }
+                apkStream.write(buffer, 0, didRead);
+                size -= didRead;
+            }
+            apkStream.close();
+
+            // make sure the installer can read it
+            apkFile.setReadable(true, false);
+
+            // Now install it
+            Uri packageUri = Uri.fromFile(apkFile);
+            mInstallObserver.reset();
+            backupManagerService.mPackageManager.installPackage(packageUri, mInstallObserver,
+                PackageManager.INSTALL_REPLACE_EXISTING | PackageManager.INSTALL_FROM_ADB,
+                installerPackage);
+            mInstallObserver.waitForCompletion();
+
+            if (mInstallObserver.getResult() != PackageManager.INSTALL_SUCCEEDED) {
+                // The only time we continue to accept install of data even if the
+                // apk install failed is if we had already determined that we could
+                // accept the data regardless.
+                if (mPackagePolicies.get(info.packageName) != RestorePolicy.ACCEPT) {
+                    okay = false;
+                }
+            } else {
+                // Okay, the install succeeded.  Make sure it was the right app.
+                boolean uninstall = false;
+                if (!mInstallObserver.mPackageName.equals(info.packageName)) {
+                    Slog.w(RefactoredBackupManagerService.TAG, "Restore stream claimed to include apk for "
+                        + info.packageName + " but apk was really "
+                        + mInstallObserver.mPackageName);
+                    // delete the package we just put in place; it might be fraudulent
+                    okay = false;
+                    uninstall = true;
+                } else {
+                    try {
+                        PackageInfo pkg = backupManagerService.mPackageManager.getPackageInfo(info.packageName,
+                            PackageManager.GET_SIGNATURES);
+                        if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP)
+                            == 0) {
+                            Slog.w(RefactoredBackupManagerService.TAG, "Restore stream contains apk of package "
+                                + info.packageName + " but it disallows backup/restore");
+                            okay = false;
+                        } else {
+                            // So far so good -- do the signatures match the manifest?
+                            Signature[] sigs = mManifestSignatures.get(info.packageName);
+                            if (RefactoredBackupManagerService.signaturesMatch(sigs, pkg)) {
+                                // If this is a system-uid app without a declared backup agent,
+                                // don't restore any of the file data.
+                                if ((pkg.applicationInfo.uid < Process.FIRST_APPLICATION_UID)
+                                    && (pkg.applicationInfo.backupAgentName == null)) {
+                                    Slog.w(RefactoredBackupManagerService.TAG, "Installed app " + info.packageName
+                                        + " has restricted uid and no agent");
+                                    okay = false;
+                                }
+                            } else {
+                                Slog.w(RefactoredBackupManagerService.TAG, "Installed app " + info.packageName
+                                    + " signatures do not match restore manifest");
+                                okay = false;
+                                uninstall = true;
+                            }
+                        }
+                    } catch (NameNotFoundException e) {
+                        Slog.w(RefactoredBackupManagerService.TAG, "Install of package " + info.packageName
+                            + " succeeded but now not found");
+                        okay = false;
+                    }
+                }
+
+                // If we're not okay at this point, we need to delete the package
+                // that we just installed.
+                if (uninstall) {
+                    mDeleteObserver.reset();
+                    backupManagerService.mPackageManager.deletePackage(mInstallObserver.mPackageName,
+                        mDeleteObserver, 0);
+                    mDeleteObserver.waitForCompletion();
+                }
+            }
+        } catch (IOException e) {
+            Slog.e(RefactoredBackupManagerService.TAG, "Unable to transcribe restored apk for install");
+            okay = false;
+        } finally {
+            apkFile.delete();
+        }
+
+        return okay;
+    }
+
+    // Given an actual file content size, consume the post-content padding mandated
+    // by the tar format.
+    void skipTarPadding(long size, InputStream instream) throws IOException {
+        long partial = (size + 512) % 512;
+        if (partial > 0) {
+            final int needed = 512 - (int) partial;
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.i(RefactoredBackupManagerService.TAG, "Skipping tar padding: " + needed + " bytes");
+            }
+            byte[] buffer = new byte[needed];
+            if (readExactly(instream, buffer, 0, needed) == needed) {
+                mBytes += needed;
+            } else {
+              throw new IOException("Unexpected EOF in padding");
+            }
+        }
+    }
+
+    // Read a widget metadata file, returning the restored blob
+    void readMetadata(FileMetadata info, InputStream instream) throws IOException {
+        // Fail on suspiciously large widget dump files
+        if (info.size > 64 * 1024) {
+            throw new IOException("Metadata too big; corrupt? size=" + info.size);
+        }
+
+        byte[] buffer = new byte[(int) info.size];
+        if (readExactly(instream, buffer, 0, (int) info.size) == info.size) {
+            mBytes += info.size;
+        } else {
+          throw new IOException("Unexpected EOF in widget data");
+        }
+
+        String[] str = new String[1];
+        int offset = extractLine(buffer, 0, str);
+        int version = Integer.parseInt(str[0]);
+        if (version == RefactoredBackupManagerService.BACKUP_MANIFEST_VERSION) {
+            offset = extractLine(buffer, offset, str);
+            final String pkg = str[0];
+            if (info.packageName.equals(pkg)) {
+                // Data checks out -- the rest of the buffer is a concatenation of
+                // binary blobs as described in the comment at writeAppWidgetData()
+                ByteArrayInputStream bin = new ByteArrayInputStream(buffer,
+                    offset, buffer.length - offset);
+                DataInputStream in = new DataInputStream(bin);
+                while (bin.available() > 0) {
+                    int token = in.readInt();
+                    int size = in.readInt();
+                    if (size > 64 * 1024) {
+                        throw new IOException("Datum "
+                            + Integer.toHexString(token)
+                            + " too big; corrupt? size=" + info.size);
+                    }
+                    switch (token) {
+                        case RefactoredBackupManagerService.BACKUP_WIDGET_METADATA_TOKEN: {
+                            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                                Slog.i(RefactoredBackupManagerService.TAG, "Got widget metadata for " + info.packageName);
+                            }
+                            mWidgetData = new byte[size];
+                            in.read(mWidgetData);
+                            break;
+                        }
+                        default: {
+                            if (RefactoredBackupManagerService.DEBUG) {
+                                Slog.i(RefactoredBackupManagerService.TAG, "Ignoring metadata blob "
+                                    + Integer.toHexString(token)
+                                    + " for " + info.packageName);
+                            }
+                            in.skipBytes(size);
+                            break;
+                        }
+                    }
+                }
+            } else {
+                Slog.w(RefactoredBackupManagerService.TAG, "Metadata mismatch: package " + info.packageName
+                    + " but widget data for " + pkg);
+
+                Bundle monitoringExtras = backupManagerService.putMonitoringExtra(null,
+                    EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
+                monitoringExtras = backupManagerService.putMonitoringExtra(monitoringExtras,
+                    BackupManagerMonitor.EXTRA_LOG_WIDGET_PACKAGE_NAME, pkg);
+                mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                    BackupManagerMonitor.LOG_EVENT_ID_WIDGET_METADATA_MISMATCH,
+                    null,
+                    LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                    monitoringExtras);
+            }
+        } else {
+            Slog.w(RefactoredBackupManagerService.TAG, "Unsupported metadata version " + version);
+
+            Bundle monitoringExtras = backupManagerService
+                .putMonitoringExtra(null, EXTRA_LOG_EVENT_PACKAGE_NAME,
+                    info.packageName);
+            monitoringExtras = backupManagerService.putMonitoringExtra(monitoringExtras,
+                EXTRA_LOG_EVENT_PACKAGE_VERSION, version);
+            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                BackupManagerMonitor.LOG_EVENT_ID_WIDGET_UNKNOWN_VERSION,
+                null,
+                LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                monitoringExtras);
+        }
+    }
+
+    // Returns a policy constant
+    RestorePolicy readAppManifest(FileMetadata info, InputStream instream)
+        throws IOException {
+        // Fail on suspiciously large manifest files
+        if (info.size > 64 * 1024) {
+            throw new IOException("Restore manifest too big; corrupt? size=" + info.size);
+        }
+
+        byte[] buffer = new byte[(int) info.size];
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+            Slog.i(RefactoredBackupManagerService.TAG, "   readAppManifest() looking for " + info.size + " bytes, "
+                + mBytes + " already consumed");
+        }
+        if (readExactly(instream, buffer, 0, (int) info.size) == info.size) {
+            mBytes += info.size;
+        } else {
+          throw new IOException("Unexpected EOF in manifest");
+        }
+
+        RestorePolicy policy = RestorePolicy.IGNORE;
+        String[] str = new String[1];
+        int offset = 0;
+
+        try {
+            offset = extractLine(buffer, offset, str);
+            int version = Integer.parseInt(str[0]);
+            if (version == RefactoredBackupManagerService.BACKUP_MANIFEST_VERSION) {
+                offset = extractLine(buffer, offset, str);
+                String manifestPackage = str[0];
+                // TODO: handle <original-package>
+                if (manifestPackage.equals(info.packageName)) {
+                    offset = extractLine(buffer, offset, str);
+                    version = Integer.parseInt(str[0]);  // app version
+                    offset = extractLine(buffer, offset, str);
+                    // This is the platform version, which we don't use, but we parse it
+                    // as a safety against corruption in the manifest.
+                    Integer.parseInt(str[0]);
+                    offset = extractLine(buffer, offset, str);
+                    info.installerPackageName = (str[0].length() > 0) ? str[0] : null;
+                    offset = extractLine(buffer, offset, str);
+                    boolean hasApk = str[0].equals("1");
+                    offset = extractLine(buffer, offset, str);
+                    int numSigs = Integer.parseInt(str[0]);
+                    if (numSigs > 0) {
+                        Signature[] sigs = new Signature[numSigs];
+                        for (int i = 0; i < numSigs; i++) {
+                            offset = extractLine(buffer, offset, str);
+                            sigs[i] = new Signature(str[0]);
+                        }
+                        mManifestSignatures.put(info.packageName, sigs);
+
+                        // Okay, got the manifest info we need...
+                        try {
+                            PackageInfo pkgInfo = backupManagerService.mPackageManager.getPackageInfo(
+                                info.packageName, PackageManager.GET_SIGNATURES);
+                            // Fall through to IGNORE if the app explicitly disallows backup
+                            final int flags = pkgInfo.applicationInfo.flags;
+                            if ((flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) {
+                                // Restore system-uid-space packages only if they have
+                                // defined a custom backup agent
+                                if ((pkgInfo.applicationInfo.uid
+                                    >= Process.FIRST_APPLICATION_UID)
+                                    || (pkgInfo.applicationInfo.backupAgentName != null)) {
+                                    // Verify signatures against any installed version; if they
+                                    // don't match, then we fall though and ignore the data.  The
+                                    // signatureMatch() method explicitly ignores the signature
+                                    // check for packages installed on the system partition, because
+                                    // such packages are signed with the platform cert instead of
+                                    // the app developer's cert, so they're different on every
+                                    // device.
+                                    if (RefactoredBackupManagerService.signaturesMatch(sigs, pkgInfo)) {
+                                        if ((pkgInfo.applicationInfo.flags
+                                            & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) != 0) {
+                                            Slog.i(RefactoredBackupManagerService.TAG,
+                                                "Package has restoreAnyVersion; taking data");
+                                            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                                                LOG_EVENT_ID_RESTORE_ANY_VERSION,
+                                                pkgInfo,
+                                                LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                                                null);
+                                            policy = RestorePolicy.ACCEPT;
+                                        } else if (pkgInfo.versionCode >= version) {
+                                            Slog.i(RefactoredBackupManagerService.TAG, "Sig + version match; taking data");
+                                            policy = RestorePolicy.ACCEPT;
+                                            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                                                LOG_EVENT_ID_VERSIONS_MATCH,
+                                                pkgInfo,
+                                                LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                                                null);
+                                        } else {
+                                            // The data is from a newer version of the app than
+                                            // is presently installed.  That means we can only
+                                            // use it if the matching apk is also supplied.
+                                            if (mAllowApks) {
+                                                Slog.i(RefactoredBackupManagerService.TAG, "Data version " + version
+                                                    + " is newer than installed version "
+                                                    + pkgInfo.versionCode
+                                                    + " - requiring apk");
+                                                policy = RestorePolicy.ACCEPT_IF_APK;
+                                            } else {
+                                                Slog.i(RefactoredBackupManagerService.TAG, "Data requires newer version "
+                                                    + version + "; ignoring");
+                                                mMonitor = RefactoredBackupManagerService
+                                                    .monitorEvent(mMonitor,
+                                                        LOG_EVENT_ID_VERSION_OF_BACKUP_OLDER,
+                                                        pkgInfo,
+                                                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                                                        backupManagerService.putMonitoringExtra(null,
+                                                            EXTRA_LOG_OLD_VERSION,
+                                                            version));
+
+                                                policy = RestorePolicy.IGNORE;
+                                            }
+                                        }
+                                    } else {
+                                        Slog.w(RefactoredBackupManagerService.TAG, "Restore manifest signatures do not match "
+                                            + "installed application for " + info.packageName);
+                                        mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                                            LOG_EVENT_ID_FULL_RESTORE_SIGNATURE_MISMATCH,
+                                            pkgInfo,
+                                            LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                                            null);
+                                    }
+                                } else {
+                                    Slog.w(RefactoredBackupManagerService.TAG, "Package " + info.packageName
+                                        + " is system level with no agent");
+                                    mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                                        LOG_EVENT_ID_SYSTEM_APP_NO_AGENT,
+                                        pkgInfo,
+                                        LOG_EVENT_CATEGORY_AGENT,
+                                        null);
+                                }
+                            } else {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.i(RefactoredBackupManagerService.TAG, "Restore manifest from "
+                                      + info.packageName + " but allowBackup=false");
+                                }
+                                mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                                    LOG_EVENT_ID_FULL_RESTORE_ALLOW_BACKUP_FALSE,
+                                    pkgInfo,
+                                    LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                                    null);
+                            }
+                        } catch (NameNotFoundException e) {
+                            // Okay, the target app isn't installed.  We can process
+                            // the restore properly only if the dataset provides the
+                            // apk file and we can successfully install it.
+                            if (mAllowApks) {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.i(RefactoredBackupManagerService.TAG, "Package " + info.packageName
+                                      + " not installed; requiring apk in dataset");
+                                }
+                                policy = RestorePolicy.ACCEPT_IF_APK;
+                            } else {
+                                policy = RestorePolicy.IGNORE;
+                            }
+                            Bundle monitoringExtras = backupManagerService.putMonitoringExtra(null,
+                                EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
+                            monitoringExtras = backupManagerService.putMonitoringExtra(monitoringExtras,
+                                EXTRA_LOG_POLICY_ALLOW_APKS, mAllowApks);
+                            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                                LOG_EVENT_ID_APK_NOT_INSTALLED,
+                                null,
+                                LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                                monitoringExtras);
+                        }
+
+                        if (policy == RestorePolicy.ACCEPT_IF_APK && !hasApk) {
+                            Slog.i(RefactoredBackupManagerService.TAG, "Cannot restore package " + info.packageName
+                                + " without the matching .apk");
+                            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                                LOG_EVENT_ID_CANNOT_RESTORE_WITHOUT_APK,
+                                null,
+                                LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                                backupManagerService.putMonitoringExtra(null,
+                                    EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName));
+                        }
+                    } else {
+                        Slog.i(RefactoredBackupManagerService.TAG, "Missing signature on backed-up package "
+                            + info.packageName);
+                        mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                            LOG_EVENT_ID_MISSING_SIGNATURE,
+                            null,
+                            LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                            backupManagerService.putMonitoringExtra(null,
+                                EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName));
+                    }
+                } else {
+                    Slog.i(RefactoredBackupManagerService.TAG, "Expected package " + info.packageName
+                        + " but restore manifest claims " + manifestPackage);
+                    Bundle monitoringExtras = backupManagerService.putMonitoringExtra(null,
+                        EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
+                    monitoringExtras = backupManagerService.putMonitoringExtra(monitoringExtras,
+                        EXTRA_LOG_MANIFEST_PACKAGE_NAME, manifestPackage);
+                    mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                        LOG_EVENT_ID_EXPECTED_DIFFERENT_PACKAGE,
+                        null,
+                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                        monitoringExtras);
+                }
+            } else {
+                Slog.i(RefactoredBackupManagerService.TAG, "Unknown restore manifest version " + version
+                    + " for package " + info.packageName);
+                Bundle monitoringExtras = backupManagerService.putMonitoringExtra(null,
+                    EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
+                monitoringExtras = backupManagerService.putMonitoringExtra(monitoringExtras,
+                    EXTRA_LOG_EVENT_PACKAGE_VERSION, version);
+                mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                    BackupManagerMonitor.LOG_EVENT_ID_UNKNOWN_VERSION,
+                    null,
+                    LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                    monitoringExtras);
+
+            }
+        } catch (NumberFormatException e) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Corrupt restore manifest for package " + info.packageName);
+            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                BackupManagerMonitor.LOG_EVENT_ID_CORRUPT_MANIFEST,
+                null,
+                LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                backupManagerService.putMonitoringExtra(null, EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName));
+        } catch (IllegalArgumentException e) {
+            Slog.w(RefactoredBackupManagerService.TAG, e.getMessage());
+        }
+
+        return policy;
+    }
+
+    // Builds a line from a byte buffer starting at 'offset', and returns
+    // the index of the next unconsumed data in the buffer.
+    int extractLine(byte[] buffer, int offset, String[] outStr) throws IOException {
+        final int end = buffer.length;
+        if (offset >= end) {
+          throw new IOException("Incomplete data");
+        }
+
+        int pos;
+        for (pos = offset; pos < end; pos++) {
+            byte c = buffer[pos];
+            // at LF we declare end of line, and return the next char as the
+            // starting point for the next time through
+            if (c == '\n') {
+                break;
+            }
+        }
+        outStr[0] = new String(buffer, offset, pos - offset);
+        pos++;  // may be pointing an extra byte past the end but that's okay
+        return pos;
+    }
+
+    void dumpFileMetadata(FileMetadata info) {
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+            StringBuilder b = new StringBuilder(128);
+
+            // mode string
+            b.append((info.type == BackupAgent.TYPE_DIRECTORY) ? 'd' : '-');
+            b.append(((info.mode & 0400) != 0) ? 'r' : '-');
+            b.append(((info.mode & 0200) != 0) ? 'w' : '-');
+            b.append(((info.mode & 0100) != 0) ? 'x' : '-');
+            b.append(((info.mode & 0040) != 0) ? 'r' : '-');
+            b.append(((info.mode & 0020) != 0) ? 'w' : '-');
+            b.append(((info.mode & 0010) != 0) ? 'x' : '-');
+            b.append(((info.mode & 0004) != 0) ? 'r' : '-');
+            b.append(((info.mode & 0002) != 0) ? 'w' : '-');
+            b.append(((info.mode & 0001) != 0) ? 'x' : '-');
+            b.append(String.format(" %9d ", info.size));
+
+            Date stamp = new Date(info.mtime);
+            b.append(new SimpleDateFormat("MMM dd HH:mm:ss ").format(stamp));
+
+            b.append(info.packageName);
+            b.append(" :: ");
+            b.append(info.domain);
+            b.append(" :: ");
+            b.append(info.path);
+
+            Slog.i(RefactoredBackupManagerService.TAG, b.toString());
+        }
+    }
+
+    // Consume a tar file header block [sequence] and accumulate the relevant metadata
+    FileMetadata readTarHeaders(InputStream instream) throws IOException {
+        byte[] block = new byte[512];
+        FileMetadata info = null;
+
+        boolean gotHeader = readTarHeader(instream, block);
+        if (gotHeader) {
+            try {
+                // okay, presume we're okay, and extract the various metadata
+                info = new FileMetadata();
+                info.size = extractRadix(block, RefactoredBackupManagerService.TAR_HEADER_OFFSET_FILESIZE,
+                    RefactoredBackupManagerService.TAR_HEADER_LENGTH_FILESIZE, RefactoredBackupManagerService.TAR_HEADER_LONG_RADIX);
+                info.mtime = extractRadix(block, RefactoredBackupManagerService.TAR_HEADER_OFFSET_MODTIME,
+                    RefactoredBackupManagerService.TAR_HEADER_LENGTH_MODTIME, RefactoredBackupManagerService.TAR_HEADER_LONG_RADIX);
+                info.mode = extractRadix(block, RefactoredBackupManagerService.TAR_HEADER_OFFSET_MODE,
+                    RefactoredBackupManagerService.TAR_HEADER_LENGTH_MODE, RefactoredBackupManagerService.TAR_HEADER_LONG_RADIX);
+
+                info.path = extractString(block, RefactoredBackupManagerService.TAR_HEADER_OFFSET_PATH_PREFIX,
+                    RefactoredBackupManagerService.TAR_HEADER_LENGTH_PATH_PREFIX);
+                String path = extractString(block, RefactoredBackupManagerService.TAR_HEADER_OFFSET_PATH,
+                    RefactoredBackupManagerService.TAR_HEADER_LENGTH_PATH);
+                if (path.length() > 0) {
+                    if (info.path.length() > 0) {
+                      info.path += '/';
+                    }
+                    info.path += path;
+                }
+
+                // tar link indicator field: 1 byte at offset 156 in the header.
+                int typeChar = block[RefactoredBackupManagerService.TAR_HEADER_OFFSET_TYPE_CHAR];
+                if (typeChar == 'x') {
+                    // pax extended header, so we need to read that
+                    gotHeader = readPaxExtendedHeader(instream, info);
+                    if (gotHeader) {
+                        // and after a pax extended header comes another real header -- read
+                        // that to find the real file type
+                        gotHeader = readTarHeader(instream, block);
+                    }
+                    if (!gotHeader) {
+                      throw new IOException("Bad or missing pax header");
+                    }
+
+                    typeChar = block[RefactoredBackupManagerService.TAR_HEADER_OFFSET_TYPE_CHAR];
+                }
+
+                switch (typeChar) {
+                    case '0':
+                      info.type = BackupAgent.TYPE_FILE;
+                      break;
+                    case '5': {
+                        info.type = BackupAgent.TYPE_DIRECTORY;
+                        if (info.size != 0) {
+                            Slog.w(RefactoredBackupManagerService.TAG, "Directory entry with nonzero size in header");
+                            info.size = 0;
+                        }
+                        break;
+                    }
+                    case 0: {
+                        // presume EOF
+                        if (RefactoredBackupManagerService.MORE_DEBUG) {
+                          Slog.w(RefactoredBackupManagerService.TAG, "Saw type=0 in tar header block, info=" + info);
+                        }
+                        return null;
+                    }
+                    default: {
+                        Slog.e(RefactoredBackupManagerService.TAG, "Unknown tar entity type: " + typeChar);
+                        throw new IOException("Unknown entity type " + typeChar);
+                    }
+                }
+
+                // Parse out the path
+                //
+                // first: apps/shared/unrecognized
+                if (FullBackup.SHARED_PREFIX.regionMatches(0,
+                    info.path, 0, FullBackup.SHARED_PREFIX.length())) {
+                    // File in shared storage.  !!! TODO: implement this.
+                    info.path = info.path.substring(FullBackup.SHARED_PREFIX.length());
+                    info.packageName = RefactoredBackupManagerService.SHARED_BACKUP_AGENT_PACKAGE;
+                    info.domain = FullBackup.SHARED_STORAGE_TOKEN;
+                    if (RefactoredBackupManagerService.DEBUG) {
+                      Slog.i(RefactoredBackupManagerService.TAG, "File in shared storage: " + info.path);
+                    }
+                } else if (FullBackup.APPS_PREFIX.regionMatches(0,
+                    info.path, 0, FullBackup.APPS_PREFIX.length())) {
+                    // App content!  Parse out the package name and domain
+
+                    // strip the apps/ prefix
+                    info.path = info.path.substring(FullBackup.APPS_PREFIX.length());
+
+                    // extract the package name
+                    int slash = info.path.indexOf('/');
+                    if (slash < 0) {
+                      throw new IOException("Illegal semantic path in " + info.path);
+                    }
+                    info.packageName = info.path.substring(0, slash);
+                    info.path = info.path.substring(slash + 1);
+
+                    // if it's a manifest or metadata payload we're done, otherwise parse
+                    // out the domain into which the file will be restored
+                    if (!info.path.equals(RefactoredBackupManagerService.BACKUP_MANIFEST_FILENAME)
+                        && !info.path.equals(RefactoredBackupManagerService.BACKUP_METADATA_FILENAME)) {
+                        slash = info.path.indexOf('/');
+                        if (slash < 0) {
+                            throw new IOException("Illegal semantic path in non-manifest "
+                                + info.path);
+                        }
+                        info.domain = info.path.substring(0, slash);
+                        info.path = info.path.substring(slash + 1);
+                    }
+                }
+            } catch (IOException e) {
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.e(RefactoredBackupManagerService.TAG, "Parse error in header: " + e.getMessage());
+                    if (RefactoredBackupManagerService.MORE_DEBUG) {
+                        HEXLOG(block);
+                    }
+                }
+                throw e;
+            }
+        }
+        return info;
+    }
+
+    private boolean isRestorableFile(FileMetadata info) {
+        if (FullBackup.CACHE_TREE_TOKEN.equals(info.domain)) {
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.i(RefactoredBackupManagerService.TAG, "Dropping cache file path " + info.path);
+            }
+            return false;
+        }
+
+        if (FullBackup.ROOT_TREE_TOKEN.equals(info.domain)) {
+            // It's possible this is "no-backup" dir contents in an archive stream
+            // produced on a device running a version of the OS that predates that
+            // API.  Respect the no-backup intention and don't let the data get to
+            // the app.
+            if (info.path.startsWith("no_backup/")) {
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    Slog.i(RefactoredBackupManagerService.TAG, "Dropping no_backup file path " + info.path);
+                }
+                return false;
+            }
+        }
+
+        // The path needs to be canonical
+        if (info.path.contains("..") || info.path.contains("//")) {
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.w(RefactoredBackupManagerService.TAG, "Dropping invalid path " + info.path);
+            }
+            return false;
+        }
+
+        // Otherwise we think this file is good to go
+        return true;
+    }
+
+    private void HEXLOG(byte[] block) {
+        int offset = 0;
+        int todo = block.length;
+        StringBuilder buf = new StringBuilder(64);
+        while (todo > 0) {
+            buf.append(String.format("%04x   ", offset));
+            int numThisLine = (todo > 16) ? 16 : todo;
+            for (int i = 0; i < numThisLine; i++) {
+                buf.append(String.format("%02x ", block[offset + i]));
+            }
+            Slog.i("hexdump", buf.toString());
+            buf.setLength(0);
+            todo -= numThisLine;
+            offset += numThisLine;
+        }
+    }
+
+    // Read exactly the given number of bytes into a buffer at the stated offset.
+    // Returns false if EOF is encountered before the requested number of bytes
+    // could be read.
+    int readExactly(InputStream in, byte[] buffer, int offset, int size)
+        throws IOException {
+        if (size <= 0) {
+          throw new IllegalArgumentException("size must be > 0");
+        }
+      if (RefactoredBackupManagerService.MORE_DEBUG) {
+        Slog.i(RefactoredBackupManagerService.TAG, "  ... readExactly(" + size + ") called");
+      }
+        int soFar = 0;
+        while (soFar < size) {
+            int nRead = in.read(buffer, offset + soFar, size - soFar);
+            if (nRead <= 0) {
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                  Slog.w(RefactoredBackupManagerService.TAG, "- wanted exactly " + size + " but got only " + soFar);
+                }
+                break;
+            }
+            soFar += nRead;
+          if (RefactoredBackupManagerService.MORE_DEBUG) {
+            Slog.v(RefactoredBackupManagerService.TAG, "   + got " + nRead + "; now wanting " + (size - soFar));
+          }
+        }
+        return soFar;
+    }
+
+    boolean readTarHeader(InputStream instream, byte[] block) throws IOException {
+        final int got = readExactly(instream, block, 0, 512);
+        if (got == 0) {
+          return false;     // Clean EOF
+        }
+        if (got < 512) {
+          throw new IOException("Unable to read full block header");
+        }
+        mBytes += 512;
+        return true;
+    }
+
+    // overwrites 'info' fields based on the pax extended header
+    boolean readPaxExtendedHeader(InputStream instream, FileMetadata info)
+        throws IOException {
+        // We should never see a pax extended header larger than this
+        if (info.size > 32 * 1024) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Suspiciously large pax header size " + info.size
+                + " - aborting");
+            throw new IOException("Sanity failure: pax header size " + info.size);
+        }
+
+        // read whole blocks, not just the content size
+        int numBlocks = (int) ((info.size + 511) >> 9);
+        byte[] data = new byte[numBlocks * 512];
+        if (readExactly(instream, data, 0, data.length) < data.length) {
+            throw new IOException("Unable to read full pax header");
+        }
+        mBytes += data.length;
+
+        final int contentSize = (int) info.size;
+        int offset = 0;
+        do {
+            // extract the line at 'offset'
+            int eol = offset + 1;
+            while (eol < contentSize && data[eol] != ' ') {
+              eol++;
+            }
+            if (eol >= contentSize) {
+                // error: we just hit EOD looking for the end of the size field
+                throw new IOException("Invalid pax data");
+            }
+            // eol points to the space between the count and the key
+            int linelen = (int) extractRadix(data, offset, eol - offset, 10);
+            int key = eol + 1;  // start of key=value
+            eol = offset + linelen - 1; // trailing LF
+            int value;
+            for (value = key + 1; data[value] != '=' && value <= eol; value++) {
+              ;
+            }
+            if (value > eol) {
+                throw new IOException("Invalid pax declaration");
+            }
+
+            // pax requires that key/value strings be in UTF-8
+            String keyStr = new String(data, key, value - key, "UTF-8");
+            // -1 to strip the trailing LF
+            String valStr = new String(data, value + 1, eol - value - 1, "UTF-8");
+
+            if ("path".equals(keyStr)) {
+                info.path = valStr;
+            } else if ("size".equals(keyStr)) {
+                info.size = Long.parseLong(valStr);
+            } else {
+                if (RefactoredBackupManagerService.DEBUG) {
+                  Slog.i(RefactoredBackupManagerService.TAG, "Unhandled pax key: " + key);
+                }
+            }
+
+            offset += linelen;
+        } while (offset < contentSize);
+
+        return true;
+    }
+
+    long extractRadix(byte[] data, int offset, int maxChars, int radix)
+        throws IOException {
+        long value = 0;
+        final int end = offset + maxChars;
+        for (int i = offset; i < end; i++) {
+            final byte b = data[i];
+            // Numeric fields in tar can terminate with either NUL or SPC
+            if (b == 0 || b == ' ') {
+              break;
+            }
+            if (b < '0' || b > ('0' + radix - 1)) {
+                throw new IOException("Invalid number in header: '" + (char) b
+                    + "' for radix " + radix);
+            }
+            value = radix * value + (b - '0');
+        }
+        return value;
+    }
+
+    String extractString(byte[] data, int offset, int maxChars) throws IOException {
+        final int end = offset + maxChars;
+        int eos = offset;
+        // tar string fields terminate early with a NUL
+        while (eos < end && data[eos] != 0) {
+          eos++;
+        }
+        return new String(data, offset, eos - offset, "US-ASCII");
+    }
+
+    void sendStartRestore() {
+        if (mObserver != null) {
+            try {
+                mObserver.onStartRestore();
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "full restore observer went away: startRestore");
+                mObserver = null;
+            }
+        }
+    }
+
+    void sendOnRestorePackage(String name) {
+        if (mObserver != null) {
+            try {
+                // TODO: use a more user-friendly name string
+                mObserver.onRestorePackage(name);
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "full restore observer went away: restorePackage");
+                mObserver = null;
+            }
+        }
+    }
+
+    void sendEndRestore() {
+        if (mObserver != null) {
+            try {
+                mObserver.onEndRestore();
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "full restore observer went away: endRestore");
+                mObserver = null;
+            }
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/restore/PerformAdbRestoreTask.java b/services/backup/java/com/android/server/backup/restore/PerformAdbRestoreTask.java
new file mode 100644
index 0000000..39c001c
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/restore/PerformAdbRestoreTask.java
@@ -0,0 +1,1550 @@
+/*
+ * Copyright (C) 2017 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.server.backup.restore;
+
+import android.app.ApplicationThreadConstants;
+import android.app.IBackupAgent;
+import android.app.PackageInstallObserver;
+import android.app.backup.BackupAgent;
+import android.app.backup.FullBackup;
+import android.app.backup.IFullBackupRestoreObserver;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.IPackageDeleteObserver;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.pm.Signature;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.Environment;
+import android.os.ParcelFileDescriptor;
+import android.os.Process;
+import android.os.RemoteException;
+import android.util.Slog;
+import com.android.server.backup.FileMetadata;
+import com.android.server.backup.KeyValueAdbRestoreEngine;
+import com.android.server.backup.PackageManagerBackupAgent;
+import com.android.server.backup.RefactoredBackupManagerService;
+import com.android.server.backup.fullbackup.FullBackupObbConnection;
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.zip.InflaterInputStream;
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.CipherInputStream;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+
+public class PerformAdbRestoreTask implements Runnable {
+
+    private RefactoredBackupManagerService backupManagerService;
+    ParcelFileDescriptor mInputFile;
+    String mCurrentPassword;
+    String mDecryptPassword;
+    IFullBackupRestoreObserver mObserver;
+    AtomicBoolean mLatchObject;
+    IBackupAgent mAgent;
+    PackageManagerBackupAgent mPackageManagerBackupAgent;
+    String mAgentPackage;
+    ApplicationInfo mTargetApp;
+    FullBackupObbConnection mObbConnection = null;
+    ParcelFileDescriptor[] mPipes = null;
+    byte[] mWidgetData = null;
+
+    long mBytes;
+
+    // Runner that can be placed on a separate thread to do in-process invocation
+    // of the "restore finished" API asynchronously.  Used by adb restore.
+    class RestoreFinishedRunnable implements Runnable {
+
+      final IBackupAgent mAgent;
+        final int mToken;
+
+        RestoreFinishedRunnable(IBackupAgent agent, int token) {
+            mAgent = agent;
+            mToken = token;
+        }
+
+        @Override
+        public void run() {
+            try {
+                mAgent.doRestoreFinished(mToken, backupManagerService.mBackupManagerBinder);
+            } catch (RemoteException e) {
+                // never happens; this is used only for local binder calls
+            }
+        }
+    }
+
+    // possible handling states for a given package in the restore dataset
+    final HashMap<String, RestorePolicy> mPackagePolicies
+        = new HashMap<>();
+
+    // installer package names for each encountered app, derived from the manifests
+    final HashMap<String, String> mPackageInstallers = new HashMap<>();
+
+    // Signatures for a given package found in its manifest file
+    final HashMap<String, Signature[]> mManifestSignatures
+        = new HashMap<>();
+
+    // Packages we've already wiped data on when restoring their first file
+    final HashSet<String> mClearedPackages = new HashSet<>();
+
+    public PerformAdbRestoreTask(RefactoredBackupManagerService backupManagerService,
+        ParcelFileDescriptor fd, String curPassword, String decryptPassword,
+        IFullBackupRestoreObserver observer, AtomicBoolean latch) {
+        this.backupManagerService = backupManagerService;
+        mInputFile = fd;
+        mCurrentPassword = curPassword;
+        mDecryptPassword = decryptPassword;
+        mObserver = observer;
+        mLatchObject = latch;
+        mAgent = null;
+        mPackageManagerBackupAgent = new PackageManagerBackupAgent(
+            backupManagerService.mPackageManager);
+        mAgentPackage = null;
+        mTargetApp = null;
+        mObbConnection = new FullBackupObbConnection(backupManagerService);
+
+        // Which packages we've already wiped data on.  We prepopulate this
+        // with a whitelist of packages known to be unclearable.
+        mClearedPackages.add("android");
+        mClearedPackages.add(RefactoredBackupManagerService.SETTINGS_PACKAGE);
+    }
+
+    class RestoreFileRunnable implements Runnable {
+
+      IBackupAgent mAgent;
+        FileMetadata mInfo;
+        ParcelFileDescriptor mSocket;
+        int mToken;
+
+        RestoreFileRunnable(IBackupAgent agent, FileMetadata info,
+            ParcelFileDescriptor socket, int token) throws IOException {
+            mAgent = agent;
+            mInfo = info;
+            mToken = token;
+
+            // This class is used strictly for process-local binder invocations.  The
+            // semantics of ParcelFileDescriptor differ in this case; in particular, we
+            // do not automatically get a 'dup'ed descriptor that we can can continue
+            // to use asynchronously from the caller.  So, we make sure to dup it ourselves
+            // before proceeding to do the restore.
+            mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor());
+        }
+
+        @Override
+        public void run() {
+            try {
+                mAgent.doRestoreFile(mSocket, mInfo.size, mInfo.type,
+                    mInfo.domain, mInfo.path, mInfo.mode, mInfo.mtime,
+                    mToken, backupManagerService.mBackupManagerBinder);
+            } catch (RemoteException e) {
+                // never happens; this is used strictly for local binder calls
+            }
+        }
+    }
+
+    @Override
+    public void run() {
+        Slog.i(RefactoredBackupManagerService.TAG, "--- Performing full-dataset restore ---");
+        mObbConnection.establish();
+        sendStartRestore();
+
+        // Are we able to restore shared-storage data?
+        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
+            mPackagePolicies.put(RefactoredBackupManagerService.SHARED_BACKUP_AGENT_PACKAGE, RestorePolicy.ACCEPT);
+        }
+
+        FileInputStream rawInStream = null;
+        DataInputStream rawDataIn = null;
+        try {
+            if (!backupManagerService.backupPasswordMatches(mCurrentPassword)) {
+                if (RefactoredBackupManagerService.DEBUG) {
+                  Slog.w(RefactoredBackupManagerService.TAG, "Backup password mismatch; aborting");
+                }
+                return;
+            }
+
+            mBytes = 0;
+            byte[] buffer = new byte[32 * 1024];
+            rawInStream = new FileInputStream(mInputFile.getFileDescriptor());
+            rawDataIn = new DataInputStream(rawInStream);
+
+            // First, parse out the unencrypted/uncompressed header
+            boolean compressed = false;
+            InputStream preCompressStream = rawInStream;
+            final InputStream in;
+
+            boolean okay = false;
+            final int headerLen = RefactoredBackupManagerService.BACKUP_FILE_HEADER_MAGIC.length();
+            byte[] streamHeader = new byte[headerLen];
+            rawDataIn.readFully(streamHeader);
+            byte[] magicBytes = RefactoredBackupManagerService.BACKUP_FILE_HEADER_MAGIC.getBytes("UTF-8");
+            if (Arrays.equals(magicBytes, streamHeader)) {
+                // okay, header looks good.  now parse out the rest of the fields.
+                String s = readHeaderLine(rawInStream);
+                final int archiveVersion = Integer.parseInt(s);
+                if (archiveVersion <= RefactoredBackupManagerService.BACKUP_FILE_VERSION) {
+                    // okay, it's a version we recognize.  if it's version 1, we may need
+                    // to try two different PBKDF2 regimes to compare checksums.
+                    final boolean pbkdf2Fallback = (archiveVersion == 1);
+
+                    s = readHeaderLine(rawInStream);
+                    compressed = (Integer.parseInt(s) != 0);
+                    s = readHeaderLine(rawInStream);
+                    if (s.equals("none")) {
+                        // no more header to parse; we're good to go
+                        okay = true;
+                    } else if (mDecryptPassword != null && mDecryptPassword.length() > 0) {
+                        preCompressStream = decodeAesHeaderAndInitialize(s, pbkdf2Fallback,
+                            rawInStream);
+                        if (preCompressStream != null) {
+                            okay = true;
+                        }
+                    } else {
+                      Slog.w(RefactoredBackupManagerService.TAG, "Archive is encrypted but no password given");
+                    }
+                } else {
+                  Slog.w(RefactoredBackupManagerService.TAG, "Wrong header version: " + s);
+                }
+            } else {
+              Slog.w(RefactoredBackupManagerService.TAG, "Didn't read the right header magic");
+            }
+
+            if (!okay) {
+                Slog.w(RefactoredBackupManagerService.TAG, "Invalid restore data; aborting.");
+                return;
+            }
+
+            // okay, use the right stream layer based on compression
+            in = (compressed) ? new InflaterInputStream(preCompressStream) : preCompressStream;
+
+            boolean didRestore;
+            do {
+                didRestore = restoreOneFile(in, buffer);
+            } while (didRestore);
+
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+              Slog.v(RefactoredBackupManagerService.TAG, "Done consuming input tarfile, total bytes=" + mBytes);
+            }
+        } catch (IOException e) {
+            Slog.e(RefactoredBackupManagerService.TAG, "Unable to read restore input");
+        } finally {
+            tearDownPipes();
+            tearDownAgent(mTargetApp, true);
+
+            try {
+                if (rawDataIn != null) {
+                  rawDataIn.close();
+                }
+                if (rawInStream != null) {
+                  rawInStream.close();
+                }
+                mInputFile.close();
+            } catch (IOException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "Close of restore data pipe threw", e);
+                /* nothing we can do about this */
+            }
+            synchronized (mLatchObject) {
+                mLatchObject.set(true);
+                mLatchObject.notifyAll();
+            }
+            mObbConnection.tearDown();
+            sendEndRestore();
+            Slog.d(RefactoredBackupManagerService.TAG, "Full restore pass complete.");
+            backupManagerService.mWakelock.release();
+        }
+    }
+
+    String readHeaderLine(InputStream in) throws IOException {
+        int c;
+        StringBuilder buffer = new StringBuilder(80);
+        while ((c = in.read()) >= 0) {
+            if (c == '\n') {
+              break;   // consume and discard the newlines
+            }
+            buffer.append((char) c);
+        }
+        return buffer.toString();
+    }
+
+    InputStream attemptMasterKeyDecryption(String algorithm, byte[] userSalt, byte[] ckSalt,
+        int rounds, String userIvHex, String masterKeyBlobHex, InputStream rawInStream,
+        boolean doLog) {
+        InputStream result = null;
+
+        try {
+            Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
+            SecretKey userKey = backupManagerService
+                .buildPasswordKey(algorithm, mDecryptPassword, userSalt,
+                    rounds);
+            byte[] IV = backupManagerService.hexToByteArray(userIvHex);
+            IvParameterSpec ivSpec = new IvParameterSpec(IV);
+            c.init(Cipher.DECRYPT_MODE,
+                new SecretKeySpec(userKey.getEncoded(), "AES"),
+                ivSpec);
+            byte[] mkCipher = backupManagerService.hexToByteArray(masterKeyBlobHex);
+            byte[] mkBlob = c.doFinal(mkCipher);
+
+            // first, the master key IV
+            int offset = 0;
+            int len = mkBlob[offset++];
+            IV = Arrays.copyOfRange(mkBlob, offset, offset + len);
+            offset += len;
+            // then the master key itself
+            len = mkBlob[offset++];
+            byte[] mk = Arrays.copyOfRange(mkBlob,
+                offset, offset + len);
+            offset += len;
+            // and finally the master key checksum hash
+            len = mkBlob[offset++];
+            byte[] mkChecksum = Arrays.copyOfRange(mkBlob,
+                offset, offset + len);
+
+            // now validate the decrypted master key against the checksum
+            byte[] calculatedCk = backupManagerService.makeKeyChecksum(algorithm, mk, ckSalt, rounds);
+            if (Arrays.equals(calculatedCk, mkChecksum)) {
+                ivSpec = new IvParameterSpec(IV);
+                c.init(Cipher.DECRYPT_MODE,
+                    new SecretKeySpec(mk, "AES"),
+                    ivSpec);
+                // Only if all of the above worked properly will 'result' be assigned
+                result = new CipherInputStream(rawInStream, c);
+            } else if (doLog) {
+              Slog.w(RefactoredBackupManagerService.TAG, "Incorrect password");
+            }
+        } catch (InvalidAlgorithmParameterException e) {
+            if (doLog) {
+              Slog.e(RefactoredBackupManagerService.TAG, "Needed parameter spec unavailable!", e);
+            }
+        } catch (BadPaddingException e) {
+            // This case frequently occurs when the wrong password is used to decrypt
+            // the master key.  Use the identical "incorrect password" log text as is
+            // used in the checksum failure log in order to avoid providing additional
+            // information to an attacker.
+            if (doLog) {
+              Slog.w(RefactoredBackupManagerService.TAG, "Incorrect password");
+            }
+        } catch (IllegalBlockSizeException e) {
+            if (doLog) {
+              Slog.w(RefactoredBackupManagerService.TAG, "Invalid block size in master key");
+            }
+        } catch (NoSuchAlgorithmException e) {
+            if (doLog) {
+              Slog.e(RefactoredBackupManagerService.TAG, "Needed decryption algorithm unavailable!");
+            }
+        } catch (NoSuchPaddingException e) {
+            if (doLog) {
+              Slog.e(RefactoredBackupManagerService.TAG, "Needed padding mechanism unavailable!");
+            }
+        } catch (InvalidKeyException e) {
+            if (doLog) {
+              Slog.w(RefactoredBackupManagerService.TAG, "Illegal password; aborting");
+            }
+        }
+
+        return result;
+    }
+
+    InputStream decodeAesHeaderAndInitialize(String encryptionName, boolean pbkdf2Fallback,
+        InputStream rawInStream) {
+        InputStream result = null;
+        try {
+            if (encryptionName.equals(RefactoredBackupManagerService.ENCRYPTION_ALGORITHM_NAME)) {
+
+                String userSaltHex = readHeaderLine(rawInStream); // 5
+                byte[] userSalt = backupManagerService.hexToByteArray(userSaltHex);
+
+                String ckSaltHex = readHeaderLine(rawInStream); // 6
+                byte[] ckSalt = backupManagerService.hexToByteArray(ckSaltHex);
+
+                int rounds = Integer.parseInt(readHeaderLine(rawInStream)); // 7
+                String userIvHex = readHeaderLine(rawInStream); // 8
+
+                String masterKeyBlobHex = readHeaderLine(rawInStream); // 9
+
+                // decrypt the master key blob
+                result = attemptMasterKeyDecryption(RefactoredBackupManagerService.PBKDF_CURRENT, userSalt, ckSalt,
+                    rounds, userIvHex, masterKeyBlobHex, rawInStream, false);
+                if (result == null && pbkdf2Fallback) {
+                    result = attemptMasterKeyDecryption(
+                        RefactoredBackupManagerService.PBKDF_FALLBACK, userSalt, ckSalt,
+                        rounds, userIvHex, masterKeyBlobHex, rawInStream, true);
+                }
+            } else {
+              Slog.w(RefactoredBackupManagerService.TAG, "Unsupported encryption method: " + encryptionName);
+            }
+        } catch (NumberFormatException e) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Can't parse restore data header");
+        } catch (IOException e) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Can't read input header");
+        }
+
+        return result;
+    }
+
+    boolean restoreOneFile(InputStream instream, byte[] buffer) {
+        FileMetadata info;
+        try {
+            info = readTarHeaders(instream);
+            if (info != null) {
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    dumpFileMetadata(info);
+                }
+
+                final String pkg = info.packageName;
+                if (!pkg.equals(mAgentPackage)) {
+                    // okay, change in package; set up our various
+                    // bookkeeping if we haven't seen it yet
+                    if (!mPackagePolicies.containsKey(pkg)) {
+                        mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
+                    }
+
+                    // Clean up the previous agent relationship if necessary,
+                    // and let the observer know we're considering a new app.
+                    if (mAgent != null) {
+                        if (RefactoredBackupManagerService.DEBUG) {
+                          Slog.d(RefactoredBackupManagerService.TAG, "Saw new package; finalizing old one");
+                        }
+                        // Now we're really done
+                        tearDownPipes();
+                        tearDownAgent(mTargetApp, true);
+                        mTargetApp = null;
+                        mAgentPackage = null;
+                    }
+                }
+
+                if (info.path.equals(RefactoredBackupManagerService.BACKUP_MANIFEST_FILENAME)) {
+                    mPackagePolicies.put(pkg, readAppManifest(info, instream));
+                    mPackageInstallers.put(pkg, info.installerPackageName);
+                    // We've read only the manifest content itself at this point,
+                    // so consume the footer before looping around to the next
+                    // input file
+                    skipTarPadding(info.size, instream);
+                    sendOnRestorePackage(pkg);
+                } else if (info.path.equals(RefactoredBackupManagerService.BACKUP_METADATA_FILENAME)) {
+                    // Metadata blobs!
+                    readMetadata(info, instream);
+                    skipTarPadding(info.size, instream);
+                } else {
+                    // Non-manifest, so it's actual file data.  Is this a package
+                    // we're ignoring?
+                    boolean okay = true;
+                    RestorePolicy policy = mPackagePolicies.get(pkg);
+                    switch (policy) {
+                        case IGNORE:
+                            okay = false;
+                            break;
+
+                        case ACCEPT_IF_APK:
+                            // If we're in accept-if-apk state, then the first file we
+                            // see MUST be the apk.
+                            if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.d(RefactoredBackupManagerService.TAG, "APK file; installing");
+                                }
+                                // Try to install the app.
+                                String installerName = mPackageInstallers.get(pkg);
+                                okay = installApk(info, installerName, instream);
+                                // good to go; promote to ACCEPT
+                                mPackagePolicies.put(pkg, (okay)
+                                    ? RestorePolicy.ACCEPT
+                                    : RestorePolicy.IGNORE);
+                                // At this point we've consumed this file entry
+                                // ourselves, so just strip the tar footer and
+                                // go on to the next file in the input stream
+                                skipTarPadding(info.size, instream);
+                                return true;
+                            } else {
+                                // File data before (or without) the apk.  We can't
+                                // handle it coherently in this case so ignore it.
+                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
+                                okay = false;
+                            }
+                            break;
+
+                        case ACCEPT:
+                            if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.d(RefactoredBackupManagerService.TAG, "apk present but ACCEPT");
+                                }
+                                // we can take the data without the apk, so we
+                                // *want* to do so.  skip the apk by declaring this
+                                // one file not-okay without changing the restore
+                                // policy for the package.
+                                okay = false;
+                            }
+                            break;
+
+                        default:
+                            // Something has gone dreadfully wrong when determining
+                            // the restore policy from the manifest.  Ignore the
+                            // rest of this package's data.
+                            Slog.e(RefactoredBackupManagerService.TAG, "Invalid policy from manifest");
+                            okay = false;
+                            mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
+                            break;
+                    }
+
+                    // The path needs to be canonical
+                    if (info.path.contains("..") || info.path.contains("//")) {
+                        if (RefactoredBackupManagerService.MORE_DEBUG) {
+                            Slog.w(RefactoredBackupManagerService.TAG, "Dropping invalid path " + info.path);
+                        }
+                        okay = false;
+                    }
+
+                    // If the policy is satisfied, go ahead and set up to pipe the
+                    // data to the agent.
+                    if (RefactoredBackupManagerService.DEBUG && okay && mAgent != null) {
+                        Slog.i(RefactoredBackupManagerService.TAG, "Reusing existing agent instance");
+                    }
+                    if (okay && mAgent == null) {
+                        if (RefactoredBackupManagerService.DEBUG) {
+                          Slog.d(RefactoredBackupManagerService.TAG, "Need to launch agent for " + pkg);
+                        }
+
+                        try {
+                            mTargetApp = backupManagerService.mPackageManager.getApplicationInfo(pkg, 0);
+
+                            // If we haven't sent any data to this app yet, we probably
+                            // need to clear it first.  Check that.
+                            if (!mClearedPackages.contains(pkg)) {
+                                // apps with their own backup agents are
+                                // responsible for coherently managing a full
+                                // restore.
+                                if (mTargetApp.backupAgentName == null) {
+                                    if (RefactoredBackupManagerService.DEBUG) {
+                                      Slog.d(RefactoredBackupManagerService.TAG,
+                                          "Clearing app data preparatory to full restore");
+                                    }
+                                    backupManagerService.clearApplicationDataSynchronous(pkg);
+                                } else {
+                                    if (RefactoredBackupManagerService.DEBUG) {
+                                      Slog.d(RefactoredBackupManagerService.TAG, "backup agent ("
+                                          + mTargetApp.backupAgentName + ") => no clear");
+                                    }
+                                }
+                                mClearedPackages.add(pkg);
+                            } else {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.d(RefactoredBackupManagerService.TAG,
+                                      "We've initialized this app already; no clear required");
+                                }
+                            }
+
+                            // All set; now set up the IPC and launch the agent
+                            setUpPipes();
+                            mAgent = backupManagerService.bindToAgentSynchronous(mTargetApp,
+                                ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL);
+                            mAgentPackage = pkg;
+                        } catch (IOException e) {
+                            // fall through to error handling
+                        } catch (NameNotFoundException e) {
+                            // fall through to error handling
+                        }
+
+                        if (mAgent == null) {
+                            if (RefactoredBackupManagerService.DEBUG) {
+                              Slog.d(RefactoredBackupManagerService.TAG, "Unable to create agent for " + pkg);
+                            }
+                            okay = false;
+                            tearDownPipes();
+                            mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
+                        }
+                    }
+
+                    // Sanity check: make sure we never give data to the wrong app.  This
+                    // should never happen but a little paranoia here won't go amiss.
+                    if (okay && !pkg.equals(mAgentPackage)) {
+                        Slog.e(RefactoredBackupManagerService.TAG, "Restoring data for " + pkg
+                            + " but agent is for " + mAgentPackage);
+                        okay = false;
+                    }
+
+                    // At this point we have an agent ready to handle the full
+                    // restore data as well as a pipe for sending data to
+                    // that agent.  Tell the agent to start reading from the
+                    // pipe.
+                    if (okay) {
+                        boolean agentSuccess = true;
+                        long toCopy = info.size;
+                        final int token = backupManagerService.generateRandomIntegerToken();
+                        try {
+                            backupManagerService
+                                .prepareOperationTimeout(token, RefactoredBackupManagerService.TIMEOUT_RESTORE_INTERVAL, null,
+                                    RefactoredBackupManagerService.OP_TYPE_RESTORE_WAIT);
+                            if (FullBackup.OBB_TREE_TOKEN.equals(info.domain)) {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.d(RefactoredBackupManagerService.TAG, "Restoring OBB file for " + pkg
+                                      + " : " + info.path);
+                                }
+                                mObbConnection.restoreObbFile(pkg, mPipes[0],
+                                    info.size, info.type, info.path, info.mode,
+                                    info.mtime, token, backupManagerService.mBackupManagerBinder);
+                            } else if (FullBackup.KEY_VALUE_DATA_TOKEN.equals(info.domain)) {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.d(RefactoredBackupManagerService.TAG, "Restoring key-value file for " + pkg
+                                      + " : " + info.path);
+                                }
+                                KeyValueAdbRestoreEngine restoreEngine =
+                                    new KeyValueAdbRestoreEngine(
+                                        backupManagerService,
+                                        backupManagerService.mDataDir, info, mPipes[0], mAgent, token);
+                                new Thread(restoreEngine, "restore-key-value-runner").start();
+                            } else {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.d(RefactoredBackupManagerService.TAG, "Invoking agent to restore file "
+                                      + info.path);
+                                }
+                                // fire up the app's agent listening on the socket.  If
+                                // the agent is running in the system process we can't
+                                // just invoke it asynchronously, so we provide a thread
+                                // for it here.
+                                if (mTargetApp.processName.equals("system")) {
+                                    Slog.d(RefactoredBackupManagerService.TAG, "system process agent - spinning a thread");
+                                    RestoreFileRunnable runner = new RestoreFileRunnable(
+                                        mAgent, info, mPipes[0], token);
+                                    new Thread(runner, "restore-sys-runner").start();
+                                } else {
+                                    mAgent.doRestoreFile(mPipes[0], info.size, info.type,
+                                        info.domain, info.path, info.mode, info.mtime,
+                                        token, backupManagerService.mBackupManagerBinder);
+                                }
+                            }
+                        } catch (IOException e) {
+                            // couldn't dup the socket for a process-local restore
+                            Slog.d(RefactoredBackupManagerService.TAG, "Couldn't establish restore");
+                            agentSuccess = false;
+                            okay = false;
+                        } catch (RemoteException e) {
+                            // whoops, remote entity went away.  We'll eat the content
+                            // ourselves, then, and not copy it over.
+                            Slog.e(RefactoredBackupManagerService.TAG, "Agent crashed during full restore");
+                            agentSuccess = false;
+                            okay = false;
+                        }
+
+                        // Copy over the data if the agent is still good
+                        if (okay) {
+                            boolean pipeOkay = true;
+                            FileOutputStream pipe = new FileOutputStream(
+                                mPipes[1].getFileDescriptor());
+                            while (toCopy > 0) {
+                                int toRead = (toCopy > buffer.length)
+                                    ? buffer.length : (int) toCopy;
+                                int nRead = instream.read(buffer, 0, toRead);
+                                if (nRead >= 0) {
+                                  mBytes += nRead;
+                                }
+                                if (nRead <= 0) {
+                                  break;
+                                }
+                                toCopy -= nRead;
+
+                                // send it to the output pipe as long as things
+                                // are still good
+                                if (pipeOkay) {
+                                    try {
+                                        pipe.write(buffer, 0, nRead);
+                                    } catch (IOException e) {
+                                        Slog.e(RefactoredBackupManagerService.TAG, "Failed to write to restore pipe", e);
+                                        pipeOkay = false;
+                                    }
+                                }
+                            }
+
+                            // done sending that file!  Now we just need to consume
+                            // the delta from info.size to the end of block.
+                            skipTarPadding(info.size, instream);
+
+                            // and now that we've sent it all, wait for the remote
+                            // side to acknowledge receipt
+                            agentSuccess = backupManagerService.waitUntilOperationComplete(token);
+                        }
+
+                        // okay, if the remote end failed at any point, deal with
+                        // it by ignoring the rest of the restore on it
+                        if (!agentSuccess) {
+                            if (RefactoredBackupManagerService.DEBUG) {
+                                Slog.d(RefactoredBackupManagerService.TAG,
+                                    "Agent failure restoring " + pkg + "; now ignoring");
+                            }
+                            backupManagerService.mBackupHandler.removeMessages(
+                                RefactoredBackupManagerService.MSG_RESTORE_OPERATION_TIMEOUT);
+                            tearDownPipes();
+                            tearDownAgent(mTargetApp, false);
+                            mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
+                        }
+                    }
+
+                    // Problems setting up the agent communication, or an already-
+                    // ignored package: skip to the next tar stream entry by
+                    // reading and discarding this file.
+                    if (!okay) {
+                        if (RefactoredBackupManagerService.DEBUG) {
+                          Slog.d(RefactoredBackupManagerService.TAG, "[discarding file content]");
+                        }
+                        long bytesToConsume = (info.size + 511) & ~511;
+                        while (bytesToConsume > 0) {
+                            int toRead = (bytesToConsume > buffer.length)
+                                ? buffer.length : (int) bytesToConsume;
+                            long nRead = instream.read(buffer, 0, toRead);
+                            if (nRead >= 0) {
+                              mBytes += nRead;
+                            }
+                            if (nRead <= 0) {
+                              break;
+                            }
+                            bytesToConsume -= nRead;
+                        }
+                    }
+                }
+            }
+        } catch (IOException e) {
+            if (RefactoredBackupManagerService.DEBUG) {
+              Slog.w(RefactoredBackupManagerService.TAG, "io exception on restore socket read", e);
+            }
+            // treat as EOF
+            info = null;
+        }
+
+        return (info != null);
+    }
+
+    void setUpPipes() throws IOException {
+        mPipes = ParcelFileDescriptor.createPipe();
+    }
+
+    void tearDownPipes() {
+        if (mPipes != null) {
+            try {
+                mPipes[0].close();
+                mPipes[0] = null;
+                mPipes[1].close();
+                mPipes[1] = null;
+            } catch (IOException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "Couldn't close agent pipes", e);
+            }
+            mPipes = null;
+        }
+    }
+
+    void tearDownAgent(ApplicationInfo app, boolean doRestoreFinished) {
+        if (mAgent != null) {
+            try {
+                // In the adb restore case, we do restore-finished here
+                if (doRestoreFinished) {
+                    final int token = backupManagerService.generateRandomIntegerToken();
+                    final AdbRestoreFinishedLatch latch = new AdbRestoreFinishedLatch(
+                        backupManagerService, token);
+                    backupManagerService
+                        .prepareOperationTimeout(token, RefactoredBackupManagerService.TIMEOUT_FULL_BACKUP_INTERVAL, latch,
+                            RefactoredBackupManagerService.OP_TYPE_RESTORE_WAIT);
+                    if (mTargetApp.processName.equals("system")) {
+                        if (RefactoredBackupManagerService.MORE_DEBUG) {
+                            Slog.d(RefactoredBackupManagerService.TAG, "system agent - restoreFinished on thread");
+                        }
+                        Runnable runner = new RestoreFinishedRunnable(mAgent, token);
+                        new Thread(runner, "restore-sys-finished-runner").start();
+                    } else {
+                        mAgent.doRestoreFinished(token, backupManagerService.mBackupManagerBinder);
+                    }
+
+                    latch.await();
+                }
+
+                // unbind and tidy up even on timeout or failure, just in case
+                backupManagerService.mActivityManager.unbindBackupAgent(app);
+
+                // The agent was running with a stub Application object, so shut it down.
+                // !!! We hardcode the confirmation UI's package name here rather than use a
+                //     manifest flag!  TODO something less direct.
+                if (app.uid >= Process.FIRST_APPLICATION_UID
+                    && !app.packageName.equals("com.android.backupconfirm")) {
+                    if (RefactoredBackupManagerService.DEBUG) {
+                      Slog.d(RefactoredBackupManagerService.TAG, "Killing host process");
+                    }
+                    backupManagerService.mActivityManager.killApplicationProcess(app.processName, app.uid);
+                } else {
+                    if (RefactoredBackupManagerService.DEBUG) {
+                      Slog.d(RefactoredBackupManagerService.TAG, "Not killing after full restore");
+                    }
+                }
+            } catch (RemoteException e) {
+                Slog.d(RefactoredBackupManagerService.TAG, "Lost app trying to shut down");
+            }
+            mAgent = null;
+        }
+    }
+
+    class RestoreInstallObserver extends PackageInstallObserver {
+
+      final AtomicBoolean mDone = new AtomicBoolean();
+        String mPackageName;
+        int mResult;
+
+        public void reset() {
+            synchronized (mDone) {
+                mDone.set(false);
+            }
+        }
+
+        public void waitForCompletion() {
+            synchronized (mDone) {
+                while (mDone.get() == false) {
+                    try {
+                        mDone.wait();
+                    } catch (InterruptedException e) {
+                    }
+                }
+            }
+        }
+
+        int getResult() {
+            return mResult;
+        }
+
+        @Override
+        public void onPackageInstalled(String packageName, int returnCode,
+            String msg, Bundle extras) {
+            synchronized (mDone) {
+                mResult = returnCode;
+                mPackageName = packageName;
+                mDone.set(true);
+                mDone.notifyAll();
+            }
+        }
+    }
+
+    class RestoreDeleteObserver extends IPackageDeleteObserver.Stub {
+
+      final AtomicBoolean mDone = new AtomicBoolean();
+        int mResult;
+
+        public void reset() {
+            synchronized (mDone) {
+                mDone.set(false);
+            }
+        }
+
+        public void waitForCompletion() {
+            synchronized (mDone) {
+                while (mDone.get() == false) {
+                    try {
+                        mDone.wait();
+                    } catch (InterruptedException e) {
+                    }
+                }
+            }
+        }
+
+        @Override
+        public void packageDeleted(String packageName, int returnCode) throws RemoteException {
+            synchronized (mDone) {
+                mResult = returnCode;
+                mDone.set(true);
+                mDone.notifyAll();
+            }
+        }
+    }
+
+    final RestoreInstallObserver mInstallObserver = new RestoreInstallObserver();
+    final RestoreDeleteObserver mDeleteObserver = new RestoreDeleteObserver();
+
+    boolean installApk(FileMetadata info, String installerPackage, InputStream instream) {
+        boolean okay = true;
+
+        if (RefactoredBackupManagerService.DEBUG) {
+          Slog.d(RefactoredBackupManagerService.TAG, "Installing from backup: " + info.packageName);
+        }
+
+        // The file content is an .apk file.  Copy it out to a staging location and
+        // attempt to install it.
+        File apkFile = new File(backupManagerService.mDataDir, info.packageName);
+        try {
+            FileOutputStream apkStream = new FileOutputStream(apkFile);
+            byte[] buffer = new byte[32 * 1024];
+            long size = info.size;
+            while (size > 0) {
+                long toRead = (buffer.length < size) ? buffer.length : size;
+                int didRead = instream.read(buffer, 0, (int) toRead);
+                if (didRead >= 0) {
+                  mBytes += didRead;
+                }
+                apkStream.write(buffer, 0, didRead);
+                size -= didRead;
+            }
+            apkStream.close();
+
+            // make sure the installer can read it
+            apkFile.setReadable(true, false);
+
+            // Now install it
+            Uri packageUri = Uri.fromFile(apkFile);
+            mInstallObserver.reset();
+            backupManagerService.mPackageManager.installPackage(packageUri, mInstallObserver,
+                PackageManager.INSTALL_REPLACE_EXISTING | PackageManager.INSTALL_FROM_ADB,
+                installerPackage);
+            mInstallObserver.waitForCompletion();
+
+            if (mInstallObserver.getResult() != PackageManager.INSTALL_SUCCEEDED) {
+                // The only time we continue to accept install of data even if the
+                // apk install failed is if we had already determined that we could
+                // accept the data regardless.
+                if (mPackagePolicies.get(info.packageName) != RestorePolicy.ACCEPT) {
+                    okay = false;
+                }
+            } else {
+                // Okay, the install succeeded.  Make sure it was the right app.
+                boolean uninstall = false;
+                if (!mInstallObserver.mPackageName.equals(info.packageName)) {
+                    Slog.w(RefactoredBackupManagerService.TAG, "Restore stream claimed to include apk for "
+                        + info.packageName + " but apk was really "
+                        + mInstallObserver.mPackageName);
+                    // delete the package we just put in place; it might be fraudulent
+                    okay = false;
+                    uninstall = true;
+                } else {
+                    try {
+                        PackageInfo pkg = backupManagerService.mPackageManager.getPackageInfo(info.packageName,
+                            PackageManager.GET_SIGNATURES);
+                        if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP)
+                            == 0) {
+                            Slog.w(RefactoredBackupManagerService.TAG, "Restore stream contains apk of package "
+                                + info.packageName + " but it disallows backup/restore");
+                            okay = false;
+                        } else {
+                            // So far so good -- do the signatures match the manifest?
+                            Signature[] sigs = mManifestSignatures.get(info.packageName);
+                            if (RefactoredBackupManagerService.signaturesMatch(sigs, pkg)) {
+                                // If this is a system-uid app without a declared backup agent,
+                                // don't restore any of the file data.
+                                if ((pkg.applicationInfo.uid < Process.FIRST_APPLICATION_UID)
+                                    && (pkg.applicationInfo.backupAgentName == null)) {
+                                    Slog.w(RefactoredBackupManagerService.TAG, "Installed app " + info.packageName
+                                        + " has restricted uid and no agent");
+                                    okay = false;
+                                }
+                            } else {
+                                Slog.w(RefactoredBackupManagerService.TAG, "Installed app " + info.packageName
+                                    + " signatures do not match restore manifest");
+                                okay = false;
+                                uninstall = true;
+                            }
+                        }
+                    } catch (NameNotFoundException e) {
+                        Slog.w(RefactoredBackupManagerService.TAG, "Install of package " + info.packageName
+                            + " succeeded but now not found");
+                        okay = false;
+                    }
+                }
+
+                // If we're not okay at this point, we need to delete the package
+                // that we just installed.
+                if (uninstall) {
+                    mDeleteObserver.reset();
+                    backupManagerService.mPackageManager.deletePackage(mInstallObserver.mPackageName,
+                        mDeleteObserver, 0);
+                    mDeleteObserver.waitForCompletion();
+                }
+            }
+        } catch (IOException e) {
+            Slog.e(RefactoredBackupManagerService.TAG, "Unable to transcribe restored apk for install");
+            okay = false;
+        } finally {
+            apkFile.delete();
+        }
+
+        return okay;
+    }
+
+    // Given an actual file content size, consume the post-content padding mandated
+    // by the tar format.
+    void skipTarPadding(long size, InputStream instream) throws IOException {
+        long partial = (size + 512) % 512;
+        if (partial > 0) {
+            final int needed = 512 - (int) partial;
+            byte[] buffer = new byte[needed];
+            if (readExactly(instream, buffer, 0, needed) == needed) {
+                mBytes += needed;
+            } else {
+              throw new IOException("Unexpected EOF in padding");
+            }
+        }
+    }
+
+    // Read a widget metadata file, returning the restored blob
+    void readMetadata(FileMetadata info, InputStream instream) throws IOException {
+        // Fail on suspiciously large widget dump files
+        if (info.size > 64 * 1024) {
+            throw new IOException("Metadata too big; corrupt? size=" + info.size);
+        }
+
+        byte[] buffer = new byte[(int) info.size];
+        if (readExactly(instream, buffer, 0, (int) info.size) == info.size) {
+            mBytes += info.size;
+        } else {
+          throw new IOException("Unexpected EOF in widget data");
+        }
+
+        String[] str = new String[1];
+        int offset = extractLine(buffer, 0, str);
+        int version = Integer.parseInt(str[0]);
+        if (version == RefactoredBackupManagerService.BACKUP_MANIFEST_VERSION) {
+            offset = extractLine(buffer, offset, str);
+            final String pkg = str[0];
+            if (info.packageName.equals(pkg)) {
+                // Data checks out -- the rest of the buffer is a concatenation of
+                // binary blobs as described in the comment at writeAppWidgetData()
+                ByteArrayInputStream bin = new ByteArrayInputStream(buffer,
+                    offset, buffer.length - offset);
+                DataInputStream in = new DataInputStream(bin);
+                while (bin.available() > 0) {
+                    int token = in.readInt();
+                    int size = in.readInt();
+                    if (size > 64 * 1024) {
+                        throw new IOException("Datum "
+                            + Integer.toHexString(token)
+                            + " too big; corrupt? size=" + info.size);
+                    }
+                    switch (token) {
+                        case RefactoredBackupManagerService.BACKUP_WIDGET_METADATA_TOKEN: {
+                            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                                Slog.i(RefactoredBackupManagerService.TAG, "Got widget metadata for " + info.packageName);
+                            }
+                            mWidgetData = new byte[size];
+                            in.read(mWidgetData);
+                            break;
+                        }
+                        default: {
+                            if (RefactoredBackupManagerService.DEBUG) {
+                                Slog.i(RefactoredBackupManagerService.TAG, "Ignoring metadata blob "
+                                    + Integer.toHexString(token)
+                                    + " for " + info.packageName);
+                            }
+                            in.skipBytes(size);
+                            break;
+                        }
+                    }
+                }
+            } else {
+                Slog.w(RefactoredBackupManagerService.TAG, "Metadata mismatch: package " + info.packageName
+                    + " but widget data for " + pkg);
+            }
+        } else {
+            Slog.w(RefactoredBackupManagerService.TAG, "Unsupported metadata version " + version);
+        }
+    }
+
+    // Returns a policy constant; takes a buffer arg to reduce memory churn
+    RestorePolicy readAppManifest(FileMetadata info, InputStream instream)
+        throws IOException {
+        // Fail on suspiciously large manifest files
+        if (info.size > 64 * 1024) {
+            throw new IOException("Restore manifest too big; corrupt? size=" + info.size);
+        }
+
+        byte[] buffer = new byte[(int) info.size];
+        if (readExactly(instream, buffer, 0, (int) info.size) == info.size) {
+            mBytes += info.size;
+        } else {
+          throw new IOException("Unexpected EOF in manifest");
+        }
+
+        RestorePolicy policy = RestorePolicy.IGNORE;
+        String[] str = new String[1];
+        int offset = 0;
+
+        try {
+            offset = extractLine(buffer, offset, str);
+            int version = Integer.parseInt(str[0]);
+            if (version == RefactoredBackupManagerService.BACKUP_MANIFEST_VERSION) {
+                offset = extractLine(buffer, offset, str);
+                String manifestPackage = str[0];
+                // TODO: handle <original-package>
+                if (manifestPackage.equals(info.packageName)) {
+                    offset = extractLine(buffer, offset, str);
+                    version = Integer.parseInt(str[0]);  // app version
+                    offset = extractLine(buffer, offset, str);
+                    // This is the platform version, which we don't use, but we parse it
+                    // as a safety against corruption in the manifest.
+                    Integer.parseInt(str[0]);
+                    offset = extractLine(buffer, offset, str);
+                    info.installerPackageName = (str[0].length() > 0) ? str[0] : null;
+                    offset = extractLine(buffer, offset, str);
+                    boolean hasApk = str[0].equals("1");
+                    offset = extractLine(buffer, offset, str);
+                    int numSigs = Integer.parseInt(str[0]);
+                    if (numSigs > 0) {
+                        Signature[] sigs = new Signature[numSigs];
+                        for (int i = 0; i < numSigs; i++) {
+                            offset = extractLine(buffer, offset, str);
+                            sigs[i] = new Signature(str[0]);
+                        }
+                        mManifestSignatures.put(info.packageName, sigs);
+
+                        // Okay, got the manifest info we need...
+                        try {
+                            PackageInfo pkgInfo = backupManagerService.mPackageManager.getPackageInfo(
+                                info.packageName, PackageManager.GET_SIGNATURES);
+                            // Fall through to IGNORE if the app explicitly disallows backup
+                            final int flags = pkgInfo.applicationInfo.flags;
+                            if ((flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) {
+                                // Restore system-uid-space packages only if they have
+                                // defined a custom backup agent
+                                if ((pkgInfo.applicationInfo.uid
+                                    >= Process.FIRST_APPLICATION_UID)
+                                    || (pkgInfo.applicationInfo.backupAgentName != null)) {
+                                    // Verify signatures against any installed version; if they
+                                    // don't match, then we fall though and ignore the data.  The
+                                    // signatureMatch() method explicitly ignores the signature
+                                    // check for packages installed on the system partition, because
+                                    // such packages are signed with the platform cert instead of
+                                    // the app developer's cert, so they're different on every
+                                    // device.
+                                    if (RefactoredBackupManagerService.signaturesMatch(sigs, pkgInfo)) {
+                                        if ((pkgInfo.applicationInfo.flags
+                                            & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) != 0) {
+                                            Slog.i(RefactoredBackupManagerService.TAG,
+                                                "Package has restoreAnyVersion; taking data");
+                                            policy = RestorePolicy.ACCEPT;
+                                        } else if (pkgInfo.versionCode >= version) {
+                                            Slog.i(RefactoredBackupManagerService.TAG, "Sig + version match; taking data");
+                                            policy = RestorePolicy.ACCEPT;
+                                        } else {
+                                            // The data is from a newer version of the app than
+                                            // is presently installed.  That means we can only
+                                            // use it if the matching apk is also supplied.
+                                            Slog.d(RefactoredBackupManagerService.TAG, "Data version " + version
+                                                + " is newer than installed version "
+                                                + pkgInfo.versionCode + " - requiring apk");
+                                            policy = RestorePolicy.ACCEPT_IF_APK;
+                                        }
+                                    } else {
+                                        Slog.w(RefactoredBackupManagerService.TAG, "Restore manifest signatures do not match "
+                                            + "installed application for " + info.packageName);
+                                    }
+                                } else {
+                                    Slog.w(RefactoredBackupManagerService.TAG, "Package " + info.packageName
+                                        + " is system level with no agent");
+                                }
+                            } else {
+                                if (RefactoredBackupManagerService.DEBUG) {
+                                  Slog.i(RefactoredBackupManagerService.TAG, "Restore manifest from "
+                                      + info.packageName + " but allowBackup=false");
+                                }
+                            }
+                        } catch (NameNotFoundException e) {
+                            // Okay, the target app isn't installed.  We can process
+                            // the restore properly only if the dataset provides the
+                            // apk file and we can successfully install it.
+                            if (RefactoredBackupManagerService.DEBUG) {
+                              Slog.i(RefactoredBackupManagerService.TAG, "Package " + info.packageName
+                                  + " not installed; requiring apk in dataset");
+                            }
+                            policy = RestorePolicy.ACCEPT_IF_APK;
+                        }
+
+                        if (policy == RestorePolicy.ACCEPT_IF_APK && !hasApk) {
+                            Slog.i(RefactoredBackupManagerService.TAG, "Cannot restore package " + info.packageName
+                                + " without the matching .apk");
+                        }
+                    } else {
+                        Slog.i(RefactoredBackupManagerService.TAG, "Missing signature on backed-up package "
+                            + info.packageName);
+                    }
+                } else {
+                    Slog.i(RefactoredBackupManagerService.TAG, "Expected package " + info.packageName
+                        + " but restore manifest claims " + manifestPackage);
+                }
+            } else {
+                Slog.i(RefactoredBackupManagerService.TAG, "Unknown restore manifest version " + version
+                    + " for package " + info.packageName);
+            }
+        } catch (NumberFormatException e) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Corrupt restore manifest for package " + info.packageName);
+        } catch (IllegalArgumentException e) {
+            Slog.w(RefactoredBackupManagerService.TAG, e.getMessage());
+        }
+
+        return policy;
+    }
+
+    // Builds a line from a byte buffer starting at 'offset', and returns
+    // the index of the next unconsumed data in the buffer.
+    int extractLine(byte[] buffer, int offset, String[] outStr) throws IOException {
+        final int end = buffer.length;
+        if (offset >= end) {
+          throw new IOException("Incomplete data");
+        }
+
+        int pos;
+        for (pos = offset; pos < end; pos++) {
+            byte c = buffer[pos];
+            // at LF we declare end of line, and return the next char as the
+            // starting point for the next time through
+            if (c == '\n') {
+                break;
+            }
+        }
+        outStr[0] = new String(buffer, offset, pos - offset);
+        pos++;  // may be pointing an extra byte past the end but that's okay
+        return pos;
+    }
+
+    void dumpFileMetadata(FileMetadata info) {
+        if (RefactoredBackupManagerService.DEBUG) {
+            StringBuilder b = new StringBuilder(128);
+
+            // mode string
+            b.append((info.type == BackupAgent.TYPE_DIRECTORY) ? 'd' : '-');
+            b.append(((info.mode & 0400) != 0) ? 'r' : '-');
+            b.append(((info.mode & 0200) != 0) ? 'w' : '-');
+            b.append(((info.mode & 0100) != 0) ? 'x' : '-');
+            b.append(((info.mode & 0040) != 0) ? 'r' : '-');
+            b.append(((info.mode & 0020) != 0) ? 'w' : '-');
+            b.append(((info.mode & 0010) != 0) ? 'x' : '-');
+            b.append(((info.mode & 0004) != 0) ? 'r' : '-');
+            b.append(((info.mode & 0002) != 0) ? 'w' : '-');
+            b.append(((info.mode & 0001) != 0) ? 'x' : '-');
+            b.append(String.format(" %9d ", info.size));
+
+            Date stamp = new Date(info.mtime);
+            b.append(new SimpleDateFormat("MMM dd HH:mm:ss ").format(stamp));
+
+            b.append(info.packageName);
+            b.append(" :: ");
+            b.append(info.domain);
+            b.append(" :: ");
+            b.append(info.path);
+
+            Slog.i(RefactoredBackupManagerService.TAG, b.toString());
+        }
+    }
+
+    // Consume a tar file header block [sequence] and accumulate the relevant metadata
+    FileMetadata readTarHeaders(InputStream instream) throws IOException {
+        byte[] block = new byte[512];
+        FileMetadata info = null;
+
+        boolean gotHeader = readTarHeader(instream, block);
+        if (gotHeader) {
+            try {
+                // okay, presume we're okay, and extract the various metadata
+                info = new FileMetadata();
+                info.size = extractRadix(block, 124, 12, 8);
+                info.mtime = extractRadix(block, 136, 12, 8);
+                info.mode = extractRadix(block, 100, 8, 8);
+
+                info.path = extractString(block, 345, 155); // prefix
+                String path = extractString(block, 0, 100);
+                if (path.length() > 0) {
+                    if (info.path.length() > 0) {
+                      info.path += '/';
+                    }
+                    info.path += path;
+                }
+
+                // tar link indicator field: 1 byte at offset 156 in the header.
+                int typeChar = block[156];
+                if (typeChar == 'x') {
+                    // pax extended header, so we need to read that
+                    gotHeader = readPaxExtendedHeader(instream, info);
+                    if (gotHeader) {
+                        // and after a pax extended header comes another real header -- read
+                        // that to find the real file type
+                        gotHeader = readTarHeader(instream, block);
+                    }
+                    if (!gotHeader) {
+                      throw new IOException("Bad or missing pax header");
+                    }
+
+                    typeChar = block[156];
+                }
+
+                switch (typeChar) {
+                    case '0':
+                      info.type = BackupAgent.TYPE_FILE;
+                      break;
+                    case '5': {
+                        info.type = BackupAgent.TYPE_DIRECTORY;
+                        if (info.size != 0) {
+                            Slog.w(RefactoredBackupManagerService.TAG, "Directory entry with nonzero size in header");
+                            info.size = 0;
+                        }
+                        break;
+                    }
+                    case 0: {
+                        // presume EOF
+                        if (RefactoredBackupManagerService.DEBUG) {
+                          Slog.w(RefactoredBackupManagerService.TAG, "Saw type=0 in tar header block, info=" + info);
+                        }
+                        return null;
+                    }
+                    default: {
+                        Slog.e(RefactoredBackupManagerService.TAG, "Unknown tar entity type: " + typeChar);
+                        throw new IOException("Unknown entity type " + typeChar);
+                    }
+                }
+
+                // Parse out the path
+                //
+                // first: apps/shared/unrecognized
+                if (FullBackup.SHARED_PREFIX.regionMatches(0,
+                    info.path, 0, FullBackup.SHARED_PREFIX.length())) {
+                    // File in shared storage.  !!! TODO: implement this.
+                    info.path = info.path.substring(FullBackup.SHARED_PREFIX.length());
+                    info.packageName = RefactoredBackupManagerService.SHARED_BACKUP_AGENT_PACKAGE;
+                    info.domain = FullBackup.SHARED_STORAGE_TOKEN;
+                    if (RefactoredBackupManagerService.DEBUG) {
+                      Slog.i(RefactoredBackupManagerService.TAG, "File in shared storage: " + info.path);
+                    }
+                } else if (FullBackup.APPS_PREFIX.regionMatches(0,
+                    info.path, 0, FullBackup.APPS_PREFIX.length())) {
+                    // App content!  Parse out the package name and domain
+
+                    // strip the apps/ prefix
+                    info.path = info.path.substring(FullBackup.APPS_PREFIX.length());
+
+                    // extract the package name
+                    int slash = info.path.indexOf('/');
+                    if (slash < 0) {
+                      throw new IOException("Illegal semantic path in " + info.path);
+                    }
+                    info.packageName = info.path.substring(0, slash);
+                    info.path = info.path.substring(slash + 1);
+
+                    // if it's a manifest or metadata payload we're done, otherwise parse
+                    // out the domain into which the file will be restored
+                    if (!info.path.equals(RefactoredBackupManagerService.BACKUP_MANIFEST_FILENAME)
+                        && !info.path.equals(RefactoredBackupManagerService.BACKUP_METADATA_FILENAME)) {
+                        slash = info.path.indexOf('/');
+                        if (slash < 0) {
+                          throw new IOException(
+                              "Illegal semantic path in non-manifest " + info.path);
+                        }
+                        info.domain = info.path.substring(0, slash);
+                        info.path = info.path.substring(slash + 1);
+                    }
+                }
+            } catch (IOException e) {
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.e(RefactoredBackupManagerService.TAG, "Parse error in header: " + e.getMessage());
+                    HEXLOG(block);
+                }
+                throw e;
+            }
+        }
+        return info;
+    }
+
+    private void HEXLOG(byte[] block) {
+        int offset = 0;
+        int todo = block.length;
+        StringBuilder buf = new StringBuilder(64);
+        while (todo > 0) {
+            buf.append(String.format("%04x   ", offset));
+            int numThisLine = (todo > 16) ? 16 : todo;
+            for (int i = 0; i < numThisLine; i++) {
+                buf.append(String.format("%02x ", block[offset + i]));
+            }
+            Slog.i("hexdump", buf.toString());
+            buf.setLength(0);
+            todo -= numThisLine;
+            offset += numThisLine;
+        }
+    }
+
+    // Read exactly the given number of bytes into a buffer at the stated offset.
+    // Returns false if EOF is encountered before the requested number of bytes
+    // could be read.
+    int readExactly(InputStream in, byte[] buffer, int offset, int size)
+        throws IOException {
+        if (size <= 0) {
+          throw new IllegalArgumentException("size must be > 0");
+        }
+
+        int soFar = 0;
+        while (soFar < size) {
+            int nRead = in.read(buffer, offset + soFar, size - soFar);
+            if (nRead <= 0) {
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                  Slog.w(RefactoredBackupManagerService.TAG, "- wanted exactly " + size + " but got only " + soFar);
+                }
+                break;
+            }
+            soFar += nRead;
+        }
+        return soFar;
+    }
+
+    boolean readTarHeader(InputStream instream, byte[] block) throws IOException {
+        final int got = readExactly(instream, block, 0, 512);
+        if (got == 0) {
+          return false;     // Clean EOF
+        }
+        if (got < 512) {
+          throw new IOException("Unable to read full block header");
+        }
+        mBytes += 512;
+        return true;
+    }
+
+    // overwrites 'info' fields based on the pax extended header
+    boolean readPaxExtendedHeader(InputStream instream, FileMetadata info)
+        throws IOException {
+        // We should never see a pax extended header larger than this
+        if (info.size > 32 * 1024) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Suspiciously large pax header size " + info.size
+                + " - aborting");
+            throw new IOException("Sanity failure: pax header size " + info.size);
+        }
+
+        // read whole blocks, not just the content size
+        int numBlocks = (int) ((info.size + 511) >> 9);
+        byte[] data = new byte[numBlocks * 512];
+        if (readExactly(instream, data, 0, data.length) < data.length) {
+            throw new IOException("Unable to read full pax header");
+        }
+        mBytes += data.length;
+
+        final int contentSize = (int) info.size;
+        int offset = 0;
+        do {
+            // extract the line at 'offset'
+            int eol = offset + 1;
+            while (eol < contentSize && data[eol] != ' ') {
+              eol++;
+            }
+            if (eol >= contentSize) {
+                // error: we just hit EOD looking for the end of the size field
+                throw new IOException("Invalid pax data");
+            }
+            // eol points to the space between the count and the key
+            int linelen = (int) extractRadix(data, offset, eol - offset, 10);
+            int key = eol + 1;  // start of key=value
+            eol = offset + linelen - 1; // trailing LF
+            int value;
+            for (value = key + 1; data[value] != '=' && value <= eol; value++) {
+              ;
+            }
+            if (value > eol) {
+                throw new IOException("Invalid pax declaration");
+            }
+
+            // pax requires that key/value strings be in UTF-8
+            String keyStr = new String(data, key, value - key, "UTF-8");
+            // -1 to strip the trailing LF
+            String valStr = new String(data, value + 1, eol - value - 1, "UTF-8");
+
+            if ("path".equals(keyStr)) {
+                info.path = valStr;
+            } else if ("size".equals(keyStr)) {
+                info.size = Long.parseLong(valStr);
+            } else {
+                if (RefactoredBackupManagerService.DEBUG) {
+                  Slog.i(RefactoredBackupManagerService.TAG, "Unhandled pax key: " + key);
+                }
+            }
+
+            offset += linelen;
+        } while (offset < contentSize);
+
+        return true;
+    }
+
+    long extractRadix(byte[] data, int offset, int maxChars, int radix)
+        throws IOException {
+        long value = 0;
+        final int end = offset + maxChars;
+        for (int i = offset; i < end; i++) {
+            final byte b = data[i];
+            // Numeric fields in tar can terminate with either NUL or SPC
+            if (b == 0 || b == ' ') {
+              break;
+            }
+            if (b < '0' || b > ('0' + radix - 1)) {
+                throw new IOException(
+                    "Invalid number in header: '" + (char) b + "' for radix " + radix);
+            }
+            value = radix * value + (b - '0');
+        }
+        return value;
+    }
+
+    String extractString(byte[] data, int offset, int maxChars) throws IOException {
+        final int end = offset + maxChars;
+        int eos = offset;
+        // tar string fields terminate early with a NUL
+        while (eos < end && data[eos] != 0) {
+          eos++;
+        }
+        return new String(data, offset, eos - offset, "US-ASCII");
+    }
+
+    void sendStartRestore() {
+        if (mObserver != null) {
+            try {
+                mObserver.onStartRestore();
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "full restore observer went away: startRestore");
+                mObserver = null;
+            }
+        }
+    }
+
+    void sendOnRestorePackage(String name) {
+        if (mObserver != null) {
+            try {
+                // TODO: use a more user-friendly name string
+                mObserver.onRestorePackage(name);
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "full restore observer went away: restorePackage");
+                mObserver = null;
+            }
+        }
+    }
+
+    void sendEndRestore() {
+        if (mObserver != null) {
+            try {
+                mObserver.onEndRestore();
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "full restore observer went away: endRestore");
+                mObserver = null;
+            }
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/restore/PerformUnifiedRestoreTask.java b/services/backup/java/com/android/server/backup/restore/PerformUnifiedRestoreTask.java
new file mode 100644
index 0000000..7c9bd38
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/restore/PerformUnifiedRestoreTask.java
@@ -0,0 +1,1294 @@
+/*
+ * Copyright (C) 2017 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.server.backup.restore;
+
+import android.app.ApplicationThreadConstants;
+import android.app.IBackupAgent;
+import android.app.backup.BackupDataInput;
+import android.app.backup.BackupDataOutput;
+import android.app.backup.BackupManagerMonitor;
+import android.app.backup.BackupTransport;
+import android.app.backup.IBackupManagerMonitor;
+import android.app.backup.IRestoreObserver;
+import android.app.backup.RestoreDescription;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.os.Bundle;
+import android.os.Message;
+import android.os.ParcelFileDescriptor;
+import android.os.Process;
+import android.os.RemoteException;
+import android.os.SystemClock;
+import android.os.UserHandle;
+import android.util.EventLog;
+import android.util.Slog;
+import com.android.internal.backup.IBackupTransport;
+import com.android.server.AppWidgetBackupBridge;
+import com.android.server.EventLogTags;
+import com.android.server.backup.BackupRestoreTask;
+import com.android.server.backup.BackupUtils;
+import com.android.server.backup.PackageManagerBackupAgent;
+import com.android.server.backup.PackageManagerBackupAgent.Metadata;
+import com.android.server.backup.RefactoredBackupManagerService;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import libcore.io.IoUtils;
+
+public class PerformUnifiedRestoreTask implements BackupRestoreTask {
+
+    private RefactoredBackupManagerService backupManagerService;
+    // Transport we're working with to do the restore
+    private IBackupTransport mTransport;
+
+    // Where per-transport saved state goes
+    File mStateDir;
+
+    // Restore observer; may be null
+    private IRestoreObserver mObserver;
+
+    // BackuoManagerMonitor; may be null
+    private IBackupManagerMonitor mMonitor;
+
+    // Token identifying the dataset to the transport
+    private long mToken;
+
+    // When this is a restore-during-install, this is the token identifying the
+    // operation to the Package Manager, and we must ensure that we let it know
+    // when we're finished.
+    private int mPmToken;
+
+    // When this is restore-during-install, we need to tell the package manager
+    // whether we actually launched the app, because this affects notifications
+    // around externally-visible state transitions.
+    private boolean mDidLaunch;
+
+    // Is this a whole-system restore, i.e. are we establishing a new ancestral
+    // dataset to base future restore-at-install operations from?
+    private boolean mIsSystemRestore;
+
+    // If this is a single-package restore, what package are we interested in?
+    private PackageInfo mTargetPackage;
+
+    // In all cases, the calculated list of packages that we are trying to restore
+    private List<PackageInfo> mAcceptSet;
+
+    // Our bookkeeping about the ancestral dataset
+    private PackageManagerBackupAgent mPmAgent;
+
+    // Currently-bound backup agent for restore + restoreFinished purposes
+    private IBackupAgent mAgent;
+
+    // What sort of restore we're doing now
+    private RestoreDescription mRestoreDescription;
+
+    // The package we're currently restoring
+    private PackageInfo mCurrentPackage;
+
+    // Widget-related data handled as part of this restore operation
+    private byte[] mWidgetData;
+
+    // Number of apps restored in this pass
+    private int mCount;
+
+    // When did we start?
+    private long mStartRealtime;
+
+    // State machine progress
+    private UnifiedRestoreState mState;
+
+    // How are things going?
+    private int mStatus;
+
+    // Done?
+    private boolean mFinished;
+
+    // Key/value: bookkeeping about staged data and files for agent access
+    private File mBackupDataName;
+    private File mStageName;
+    private File mSavedStateName;
+    private File mNewStateName;
+    ParcelFileDescriptor mBackupData;
+    ParcelFileDescriptor mNewState;
+
+    private final int mEphemeralOpToken;
+
+    // Invariant: mWakelock is already held, and this task is responsible for
+    // releasing it at the end of the restore operation.
+    public PerformUnifiedRestoreTask(RefactoredBackupManagerService backupManagerService,
+        IBackupTransport transport, IRestoreObserver observer,
+        IBackupManagerMonitor monitor, long restoreSetToken, PackageInfo targetPackage,
+        int pmToken, boolean isFullSystemRestore, String[] filterSet) {
+        this.backupManagerService = backupManagerService;
+        mEphemeralOpToken = backupManagerService.generateRandomIntegerToken();
+        mState = UnifiedRestoreState.INITIAL;
+        mStartRealtime = SystemClock.elapsedRealtime();
+
+        mTransport = transport;
+        mObserver = observer;
+        mMonitor = monitor;
+        mToken = restoreSetToken;
+        mPmToken = pmToken;
+        mTargetPackage = targetPackage;
+        mIsSystemRestore = isFullSystemRestore;
+        mFinished = false;
+        mDidLaunch = false;
+
+        if (targetPackage != null) {
+            // Single package restore
+            mAcceptSet = new ArrayList<>();
+            mAcceptSet.add(targetPackage);
+        } else {
+            // Everything possible, or a target set
+            if (filterSet == null) {
+                // We want everything and a pony
+                List<PackageInfo> apps =
+                    PackageManagerBackupAgent.getStorableApplications(
+                        backupManagerService.mPackageManager);
+                filterSet = packagesToNames(apps);
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.i(RefactoredBackupManagerService.TAG, "Full restore; asking about " + filterSet.length + " apps");
+                }
+            }
+
+            mAcceptSet = new ArrayList<>(filterSet.length);
+
+            // Pro tem, we insist on moving the settings provider package to last place.
+            // Keep track of whether it's in the list, and bump it down if so.  We also
+            // want to do the system package itself first if it's called for.
+            boolean hasSystem = false;
+            boolean hasSettings = false;
+            for (int i = 0; i < filterSet.length; i++) {
+                try {
+                    PackageInfo info = backupManagerService.mPackageManager.getPackageInfo(filterSet[i], 0);
+                    if ("android".equals(info.packageName)) {
+                        hasSystem = true;
+                        continue;
+                    }
+                    if (RefactoredBackupManagerService.SETTINGS_PACKAGE.equals(info.packageName)) {
+                        hasSettings = true;
+                        continue;
+                    }
+
+                    if (RefactoredBackupManagerService.appIsEligibleForBackup(info.applicationInfo)) {
+                        mAcceptSet.add(info);
+                    }
+                } catch (NameNotFoundException e) {
+                    // requested package name doesn't exist; ignore it
+                }
+            }
+            if (hasSystem) {
+                try {
+                    mAcceptSet.add(0, backupManagerService.mPackageManager.getPackageInfo("android", 0));
+                } catch (NameNotFoundException e) {
+                    // won't happen; we know a priori that it's valid
+                }
+            }
+            if (hasSettings) {
+                try {
+                    mAcceptSet.add(backupManagerService.mPackageManager.getPackageInfo(
+                        RefactoredBackupManagerService.SETTINGS_PACKAGE, 0));
+                } catch (NameNotFoundException e) {
+                    // this one is always valid too
+                }
+            }
+        }
+
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+            Slog.v(RefactoredBackupManagerService.TAG, "Restore; accept set size is " + mAcceptSet.size());
+            for (PackageInfo info : mAcceptSet) {
+                Slog.v(RefactoredBackupManagerService.TAG, "   " + info.packageName);
+            }
+        }
+    }
+
+    private String[] packagesToNames(List<PackageInfo> apps) {
+        final int N = apps.size();
+        String[] names = new String[N];
+        for (int i = 0; i < N; i++) {
+            names[i] = apps.get(i).packageName;
+        }
+        return names;
+    }
+
+    // Execute one tick of whatever state machine the task implements
+    @Override
+    public void execute() {
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+          Slog.v(RefactoredBackupManagerService.TAG, "*** Executing restore step " + mState);
+        }
+        switch (mState) {
+            case INITIAL:
+                startRestore();
+                break;
+
+            case RUNNING_QUEUE:
+                dispatchNextRestore();
+                break;
+
+            case RESTORE_KEYVALUE:
+                restoreKeyValue();
+                break;
+
+            case RESTORE_FULL:
+                restoreFull();
+                break;
+
+            case RESTORE_FINISHED:
+                restoreFinished();
+                break;
+
+            case FINAL:
+                if (!mFinished) {
+                  finalizeRestore();
+                } else {
+                    Slog.e(RefactoredBackupManagerService.TAG, "Duplicate finish");
+                }
+                mFinished = true;
+                break;
+        }
+    }
+
+    /*
+     * SKETCH OF OPERATION
+     *
+     * create one of these PerformUnifiedRestoreTask objects, telling it which
+     * dataset & transport to address, and then parameters within the restore
+     * operation: single target package vs many, etc.
+     *
+     * 1. transport.startRestore(token, list-of-packages).  If we need @pm@  it is
+     * always placed first and the settings provider always placed last [for now].
+     *
+     * 1a [if we needed @pm@ then nextRestorePackage() and restore the PMBA inline]
+     *
+     *   [ state change => RUNNING_QUEUE ]
+     *
+     * NOW ITERATE:
+     *
+     * { 3. t.nextRestorePackage()
+     *   4. does the metadata for this package allow us to restore it?
+     *      does the on-disk app permit us to restore it? [re-check allowBackup etc]
+     *   5. is this a key/value dataset?  => key/value agent restore
+     *       [ state change => RESTORE_KEYVALUE ]
+     *       5a. spin up agent
+     *       5b. t.getRestoreData() to stage it properly
+     *       5c. call into agent to perform restore
+     *       5d. tear down agent
+     *       [ state change => RUNNING_QUEUE ]
+     *
+     *   6. else it's a stream dataset:
+     *       [ state change => RESTORE_FULL ]
+     *       6a. instantiate the engine for a stream restore: engine handles agent lifecycles
+     *       6b. spin off engine runner on separate thread
+     *       6c. ITERATE getNextFullRestoreDataChunk() and copy data to engine runner socket
+     *       [ state change => RUNNING_QUEUE ]
+     * }
+     *
+     *   [ state change => FINAL ]
+     *
+     * 7. t.finishRestore(), release wakelock, etc.
+     *
+     *
+     */
+
+    // state INITIAL : set up for the restore and read the metadata if necessary
+    private void startRestore() {
+        sendStartRestore(mAcceptSet.size());
+
+        // If we're starting a full-system restore, set up to begin widget ID remapping
+        if (mIsSystemRestore) {
+            // TODO: http://b/22388012
+            AppWidgetBackupBridge.restoreStarting(UserHandle.USER_SYSTEM);
+        }
+
+        try {
+            String transportDir = mTransport.transportDirName();
+            mStateDir = new File(backupManagerService.mBaseStateDir, transportDir);
+
+            // Fetch the current metadata from the dataset first
+            PackageInfo pmPackage = new PackageInfo();
+            pmPackage.packageName = RefactoredBackupManagerService.PACKAGE_MANAGER_SENTINEL;
+            mAcceptSet.add(0, pmPackage);
+
+            PackageInfo[] packages = mAcceptSet.toArray(new PackageInfo[0]);
+            mStatus = mTransport.startRestore(mToken, packages);
+            if (mStatus != BackupTransport.TRANSPORT_OK) {
+                Slog.e(RefactoredBackupManagerService.TAG, "Transport error " + mStatus + "; no restore possible");
+                mStatus = BackupTransport.TRANSPORT_ERROR;
+                executeNextState(UnifiedRestoreState.FINAL);
+                return;
+            }
+
+            RestoreDescription desc = mTransport.nextRestorePackage();
+            if (desc == null) {
+                Slog.e(RefactoredBackupManagerService.TAG, "No restore metadata available; halting");
+                mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                    BackupManagerMonitor.LOG_EVENT_ID_NO_RESTORE_METADATA_AVAILABLE,
+                    mCurrentPackage,
+                    BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
+                mStatus = BackupTransport.TRANSPORT_ERROR;
+                executeNextState(UnifiedRestoreState.FINAL);
+                return;
+            }
+            if (!RefactoredBackupManagerService.PACKAGE_MANAGER_SENTINEL.equals(desc.getPackageName())) {
+                Slog.e(RefactoredBackupManagerService.TAG, "Required package metadata but got "
+                    + desc.getPackageName());
+                mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                    BackupManagerMonitor.LOG_EVENT_ID_NO_PM_METADATA_RECEIVED,
+                    mCurrentPackage,
+                    BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
+                mStatus = BackupTransport.TRANSPORT_ERROR;
+                executeNextState(UnifiedRestoreState.FINAL);
+                return;
+            }
+
+            // Pull the Package Manager metadata from the restore set first
+            mCurrentPackage = new PackageInfo();
+            mCurrentPackage.packageName = RefactoredBackupManagerService.PACKAGE_MANAGER_SENTINEL;
+            mPmAgent = new PackageManagerBackupAgent(backupManagerService.mPackageManager, null);
+            mAgent = IBackupAgent.Stub.asInterface(mPmAgent.onBind());
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.v(RefactoredBackupManagerService.TAG, "initiating restore for PMBA");
+            }
+            initiateOneRestore(mCurrentPackage, 0);
+            // The PM agent called operationComplete() already, because our invocation
+            // of it is process-local and therefore synchronous.  That means that the
+            // next-state message (RUNNING_QUEUE) is already enqueued.  Only if we're
+            // unable to proceed with running the queue do we remove that pending
+            // message and jump straight to the FINAL state.  Because this was
+            // synchronous we also know that we should cancel the pending timeout
+            // message.
+            backupManagerService.mBackupHandler.removeMessages(
+                RefactoredBackupManagerService.MSG_RESTORE_OPERATION_TIMEOUT);
+
+            // Verify that the backup set includes metadata.  If not, we can't do
+            // signature/version verification etc, so we simply do not proceed with
+            // the restore operation.
+            if (!mPmAgent.hasMetadata()) {
+                Slog.e(RefactoredBackupManagerService.TAG, "PM agent has no metadata, so not restoring");
+                mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                    BackupManagerMonitor.LOG_EVENT_ID_PM_AGENT_HAS_NO_METADATA,
+                    mCurrentPackage,
+                    BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
+                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
+                    RefactoredBackupManagerService.PACKAGE_MANAGER_SENTINEL,
+                    "Package manager restore metadata missing");
+                mStatus = BackupTransport.TRANSPORT_ERROR;
+                backupManagerService.mBackupHandler.removeMessages(
+                    RefactoredBackupManagerService.MSG_BACKUP_RESTORE_STEP, this);
+                executeNextState(UnifiedRestoreState.FINAL);
+                return;
+            }
+
+            // Success; cache the metadata and continue as expected with the
+            // next state already enqueued
+
+        } catch (Exception e) {
+            // If we lost the transport at any time, halt
+            Slog.e(RefactoredBackupManagerService.TAG, "Unable to contact transport for restore: " + e.getMessage());
+            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                BackupManagerMonitor.LOG_EVENT_ID_LOST_TRANSPORT,
+                null,
+                BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT, null);
+            mStatus = BackupTransport.TRANSPORT_ERROR;
+            backupManagerService.mBackupHandler.removeMessages(
+                RefactoredBackupManagerService.MSG_BACKUP_RESTORE_STEP, this);
+            executeNextState(UnifiedRestoreState.FINAL);
+            return;
+        }
+    }
+
+    // state RUNNING_QUEUE : figure out what the next thing to be restored is,
+    // and fire the appropriate next step
+    private void dispatchNextRestore() {
+        UnifiedRestoreState nextState = UnifiedRestoreState.FINAL;
+        try {
+            mRestoreDescription = mTransport.nextRestorePackage();
+            final String pkgName = (mRestoreDescription != null)
+                ? mRestoreDescription.getPackageName() : null;
+            if (pkgName == null) {
+                Slog.e(RefactoredBackupManagerService.TAG, "Failure getting next package name");
+                EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
+                nextState = UnifiedRestoreState.FINAL;
+                return;
+            } else if (mRestoreDescription == RestoreDescription.NO_MORE_PACKAGES) {
+                // Yay we've reached the end cleanly
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.v(RefactoredBackupManagerService.TAG, "No more packages; finishing restore");
+                }
+                int millis = (int) (SystemClock.elapsedRealtime() - mStartRealtime);
+                EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, mCount, millis);
+                nextState = UnifiedRestoreState.FINAL;
+                return;
+            }
+
+            if (RefactoredBackupManagerService.DEBUG) {
+                Slog.i(RefactoredBackupManagerService.TAG, "Next restore package: " + mRestoreDescription);
+            }
+            sendOnRestorePackage(pkgName);
+
+            Metadata metaInfo = mPmAgent.getRestoredMetadata(pkgName);
+            if (metaInfo == null) {
+                Slog.e(RefactoredBackupManagerService.TAG, "No metadata for " + pkgName);
+                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, pkgName,
+                    "Package metadata missing");
+                nextState = UnifiedRestoreState.RUNNING_QUEUE;
+                return;
+            }
+
+            try {
+                mCurrentPackage = backupManagerService.mPackageManager.getPackageInfo(
+                    pkgName, PackageManager.GET_SIGNATURES);
+            } catch (NameNotFoundException e) {
+                // Whoops, we thought we could restore this package but it
+                // turns out not to be present.  Skip it.
+                Slog.e(RefactoredBackupManagerService.TAG, "Package not present: " + pkgName);
+                mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                    BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_NOT_PRESENT,
+                    mCurrentPackage,
+                    BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                    null);
+                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, pkgName,
+                    "Package missing on device");
+                nextState = UnifiedRestoreState.RUNNING_QUEUE;
+                return;
+            }
+
+            if (metaInfo.versionCode > mCurrentPackage.versionCode) {
+                // Data is from a "newer" version of the app than we have currently
+                // installed.  If the app has not declared that it is prepared to
+                // handle this case, we do not attempt the restore.
+                if ((mCurrentPackage.applicationInfo.flags
+                    & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) == 0) {
+                    String message = "Source version " + metaInfo.versionCode
+                        + " > installed version " + mCurrentPackage.versionCode;
+                    Slog.w(RefactoredBackupManagerService.TAG, "Package " + pkgName + ": " + message);
+                    Bundle monitoringExtras = backupManagerService.putMonitoringExtra(null,
+                        BackupManagerMonitor.EXTRA_LOG_RESTORE_VERSION,
+                        metaInfo.versionCode);
+                    monitoringExtras = backupManagerService.putMonitoringExtra(monitoringExtras,
+                        BackupManagerMonitor.EXTRA_LOG_RESTORE_ANYWAY, false);
+                    mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                        BackupManagerMonitor.LOG_EVENT_ID_RESTORE_VERSION_HIGHER,
+                        mCurrentPackage,
+                        BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                        monitoringExtras);
+                    EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
+                        pkgName, message);
+                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
+                    return;
+                } else {
+                    if (RefactoredBackupManagerService.DEBUG) {
+                      Slog.v(RefactoredBackupManagerService.TAG, "Source version " + metaInfo.versionCode
+                          + " > installed version " + mCurrentPackage.versionCode
+                          + " but restoreAnyVersion");
+                    }
+                    Bundle monitoringExtras = backupManagerService.putMonitoringExtra(null,
+                        BackupManagerMonitor.EXTRA_LOG_RESTORE_VERSION,
+                        metaInfo.versionCode);
+                    monitoringExtras = backupManagerService.putMonitoringExtra(monitoringExtras,
+                        BackupManagerMonitor.EXTRA_LOG_RESTORE_ANYWAY, true);
+                    mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                        BackupManagerMonitor.LOG_EVENT_ID_RESTORE_VERSION_HIGHER,
+                        mCurrentPackage,
+                        BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
+                        monitoringExtras);
+                }
+            }
+
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+              Slog.v(RefactoredBackupManagerService.TAG, "Package " + pkgName
+                  + " restore version [" + metaInfo.versionCode
+                  + "] is compatible with installed version ["
+                  + mCurrentPackage.versionCode + "]");
+            }
+
+            // Reset per-package preconditions and fire the appropriate next state
+            mWidgetData = null;
+            final int type = mRestoreDescription.getDataType();
+            if (type == RestoreDescription.TYPE_KEY_VALUE) {
+                nextState = UnifiedRestoreState.RESTORE_KEYVALUE;
+            } else if (type == RestoreDescription.TYPE_FULL_STREAM) {
+                nextState = UnifiedRestoreState.RESTORE_FULL;
+            } else {
+                // Unknown restore type; ignore this package and move on
+                Slog.e(RefactoredBackupManagerService.TAG, "Unrecognized restore type " + type);
+                nextState = UnifiedRestoreState.RUNNING_QUEUE;
+                return;
+            }
+        } catch (Exception e) {
+            Slog.e(RefactoredBackupManagerService.TAG, "Can't get next restore target from transport; halting: "
+                + e.getMessage());
+            EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
+            nextState = UnifiedRestoreState.FINAL;
+            return;
+        } finally {
+            executeNextState(nextState);
+        }
+    }
+
+    // state RESTORE_KEYVALUE : restore one package via key/value API set
+    private void restoreKeyValue() {
+        // Initiating the restore will pass responsibility for the state machine's
+        // progress to the agent callback, so we do not always execute the
+        // next state here.
+        final String packageName = mCurrentPackage.packageName;
+        // Validate some semantic requirements that apply in this way
+        // only to the key/value restore API flow
+        if (mCurrentPackage.applicationInfo.backupAgentName == null
+            || "".equals(mCurrentPackage.applicationInfo.backupAgentName)) {
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.i(RefactoredBackupManagerService.TAG, "Data exists for package " + packageName
+                    + " but app has no agent; skipping");
+            }
+            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                BackupManagerMonitor.LOG_EVENT_ID_APP_HAS_NO_AGENT, mCurrentPackage,
+                BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
+            EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
+                "Package has no agent");
+            executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
+            return;
+        }
+
+        Metadata metaInfo = mPmAgent.getRestoredMetadata(packageName);
+        if (!BackupUtils.signaturesMatch(metaInfo.sigHashes, mCurrentPackage)) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Signature mismatch restoring " + packageName);
+            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                BackupManagerMonitor.LOG_EVENT_ID_SIGNATURE_MISMATCH, mCurrentPackage,
+                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
+            EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
+                "Signature mismatch");
+            executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
+            return;
+        }
+
+        // Good to go!  Set up and bind the agent...
+        mAgent = backupManagerService.bindToAgentSynchronous(
+            mCurrentPackage.applicationInfo,
+            ApplicationThreadConstants.BACKUP_MODE_INCREMENTAL);
+        if (mAgent == null) {
+            Slog.w(RefactoredBackupManagerService.TAG, "Can't find backup agent for " + packageName);
+            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                BackupManagerMonitor.LOG_EVENT_ID_CANT_FIND_AGENT, mCurrentPackage,
+                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
+            EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
+                "Restore agent missing");
+            executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
+            return;
+        }
+
+        // Whatever happens next, we've launched the target app now; remember that.
+        mDidLaunch = true;
+
+        // And then finally start the restore on this agent
+        try {
+            initiateOneRestore(mCurrentPackage, metaInfo.versionCode);
+            ++mCount;
+        } catch (Exception e) {
+            Slog.e(RefactoredBackupManagerService.TAG, "Error when attempting restore: " + e.toString());
+            keyValueAgentErrorCleanup();
+            executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
+        }
+    }
+
+    // Guts of a key/value restore operation
+    void initiateOneRestore(PackageInfo app, int appVersionCode) {
+        final String packageName = app.packageName;
+
+        if (RefactoredBackupManagerService.DEBUG) {
+          Slog.d(RefactoredBackupManagerService.TAG, "initiateOneRestore packageName=" + packageName);
+        }
+
+        // !!! TODO: get the dirs from the transport
+        mBackupDataName = new File(backupManagerService.mDataDir, packageName + ".restore");
+        mStageName = new File(backupManagerService.mDataDir, packageName + ".stage");
+        mNewStateName = new File(mStateDir, packageName + ".new");
+        mSavedStateName = new File(mStateDir, packageName);
+
+        // don't stage the 'android' package where the wallpaper data lives.  this is
+        // an optimization: we know there's no widget data hosted/published by that
+        // package, and this way we avoid doing a spurious copy of MB-sized wallpaper
+        // data following the download.
+        boolean staging = !packageName.equals("android");
+        ParcelFileDescriptor stage;
+        File downloadFile = (staging) ? mStageName : mBackupDataName;
+
+        try {
+            // Run the transport's restore pass
+            stage = ParcelFileDescriptor.open(downloadFile,
+                ParcelFileDescriptor.MODE_READ_WRITE |
+                    ParcelFileDescriptor.MODE_CREATE |
+                    ParcelFileDescriptor.MODE_TRUNCATE);
+
+            if (mTransport.getRestoreData(stage) != BackupTransport.TRANSPORT_OK) {
+                // Transport-level failure, so we wind everything up and
+                // terminate the restore operation.
+                Slog.e(RefactoredBackupManagerService.TAG, "Error getting restore data for " + packageName);
+                EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
+                stage.close();
+                downloadFile.delete();
+                executeNextState(UnifiedRestoreState.FINAL);
+                return;
+            }
+
+            // We have the data from the transport. Now we extract and strip
+            // any per-package metadata (typically widget-related information)
+            // if appropriate
+            if (staging) {
+                stage.close();
+                stage = ParcelFileDescriptor.open(downloadFile,
+                    ParcelFileDescriptor.MODE_READ_ONLY);
+
+                mBackupData = ParcelFileDescriptor.open(mBackupDataName,
+                    ParcelFileDescriptor.MODE_READ_WRITE |
+                        ParcelFileDescriptor.MODE_CREATE |
+                        ParcelFileDescriptor.MODE_TRUNCATE);
+
+                BackupDataInput in = new BackupDataInput(stage.getFileDescriptor());
+                BackupDataOutput out = new BackupDataOutput(mBackupData.getFileDescriptor());
+                byte[] buffer = new byte[8192]; // will grow when needed
+                while (in.readNextHeader()) {
+                    final String key = in.getKey();
+                    final int size = in.getDataSize();
+
+                    // is this a special key?
+                    if (key.equals(RefactoredBackupManagerService.KEY_WIDGET_STATE)) {
+                        if (RefactoredBackupManagerService.DEBUG) {
+                            Slog.i(
+                                RefactoredBackupManagerService.TAG, "Restoring widget state for " + packageName);
+                        }
+                        mWidgetData = new byte[size];
+                        in.readEntityData(mWidgetData, 0, size);
+                    } else {
+                        if (size > buffer.length) {
+                            buffer = new byte[size];
+                        }
+                        in.readEntityData(buffer, 0, size);
+                        out.writeEntityHeader(key, size);
+                        out.writeEntityData(buffer, size);
+                    }
+                }
+
+                mBackupData.close();
+            }
+
+            // Okay, we have the data.  Now have the agent do the restore.
+            stage.close();
+
+            mBackupData = ParcelFileDescriptor.open(mBackupDataName,
+                ParcelFileDescriptor.MODE_READ_ONLY);
+
+            mNewState = ParcelFileDescriptor.open(mNewStateName,
+                ParcelFileDescriptor.MODE_READ_WRITE |
+                    ParcelFileDescriptor.MODE_CREATE |
+                    ParcelFileDescriptor.MODE_TRUNCATE);
+
+            // Kick off the restore, checking for hung agents.  The timeout or
+            // the operationComplete() callback will schedule the next step,
+            // so we do not do that here.
+            backupManagerService
+                .prepareOperationTimeout(mEphemeralOpToken, RefactoredBackupManagerService.TIMEOUT_RESTORE_INTERVAL,
+                    this, RefactoredBackupManagerService.OP_TYPE_RESTORE_WAIT);
+            mAgent.doRestore(mBackupData, appVersionCode, mNewState,
+                mEphemeralOpToken, backupManagerService.mBackupManagerBinder);
+        } catch (Exception e) {
+            Slog.e(RefactoredBackupManagerService.TAG, "Unable to call app for restore: " + packageName, e);
+            EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
+                packageName, e.toString());
+            keyValueAgentErrorCleanup();    // clears any pending timeout messages as well
+
+            // After a restore failure we go back to running the queue.  If there
+            // are no more packages to be restored that will be handled by the
+            // next step.
+            executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
+        }
+    }
+
+    // state RESTORE_FULL : restore one package via streaming engine
+    private void restoreFull() {
+        // None of this can run on the work looper here, so we spin asynchronous
+        // work like this:
+        //
+        //   StreamFeederThread: read data from mTransport.getNextFullRestoreDataChunk()
+        //                       write it into the pipe to the engine
+        //   EngineThread: FullRestoreEngine thread communicating with the target app
+        //
+        // When finished, StreamFeederThread executes next state as appropriate on the
+        // backup looper, and the overall unified restore task resumes
+        try {
+            StreamFeederThread feeder = new StreamFeederThread();
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                Slog.i(RefactoredBackupManagerService.TAG, "Spinning threads for stream restore of "
+                    + mCurrentPackage.packageName);
+            }
+            new Thread(feeder, "unified-stream-feeder").start();
+
+            // At this point the feeder is responsible for advancing the restore
+            // state, so we're done here.
+        } catch (IOException e) {
+            // Unable to instantiate the feeder thread -- we need to bail on the
+            // current target.  We haven't asked the transport for data yet, though,
+            // so we can do that simply by going back to running the restore queue.
+            Slog.e(RefactoredBackupManagerService.TAG, "Unable to construct pipes for stream restore!");
+            executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
+        }
+    }
+
+    // state RESTORE_FINISHED : provide the "no more data" signpost callback at the end
+    private void restoreFinished() {
+        try {
+            backupManagerService
+                .prepareOperationTimeout(mEphemeralOpToken, RefactoredBackupManagerService.TIMEOUT_RESTORE_FINISHED_INTERVAL, this,
+                    RefactoredBackupManagerService.OP_TYPE_RESTORE_WAIT);
+            mAgent.doRestoreFinished(mEphemeralOpToken, backupManagerService.mBackupManagerBinder);
+            // If we get this far, the callback or timeout will schedule the
+            // next restore state, so we're done
+        } catch (Exception e) {
+            final String packageName = mCurrentPackage.packageName;
+            Slog.e(RefactoredBackupManagerService.TAG, "Unable to finalize restore of " + packageName);
+            EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
+                packageName, e.toString());
+            keyValueAgentErrorCleanup();
+            executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
+        }
+    }
+
+    class StreamFeederThread extends RestoreEngine implements Runnable, BackupRestoreTask {
+
+      final String TAG = "StreamFeederThread";
+        FullRestoreEngine mEngine;
+        EngineThread mEngineThread;
+
+        // pipe through which we read data from the transport. [0] read, [1] write
+        ParcelFileDescriptor[] mTransportPipes;
+
+        // pipe through which the engine will read data.  [0] read, [1] write
+        ParcelFileDescriptor[] mEnginePipes;
+
+        private final int mEphemeralOpToken;
+
+        public StreamFeederThread() throws IOException {
+            mEphemeralOpToken = backupManagerService.generateRandomIntegerToken();
+            mTransportPipes = ParcelFileDescriptor.createPipe();
+            mEnginePipes = ParcelFileDescriptor.createPipe();
+            setRunning(true);
+        }
+
+        @Override
+        public void run() {
+            UnifiedRestoreState nextState = UnifiedRestoreState.RUNNING_QUEUE;
+            int status = BackupTransport.TRANSPORT_OK;
+
+            EventLog.writeEvent(EventLogTags.FULL_RESTORE_PACKAGE,
+                mCurrentPackage.packageName);
+
+            mEngine = new FullRestoreEngine(backupManagerService, this, null,
+                mMonitor, mCurrentPackage, false, false, mEphemeralOpToken);
+            mEngineThread = new EngineThread(mEngine, mEnginePipes[0]);
+
+            ParcelFileDescriptor eWriteEnd = mEnginePipes[1];
+            ParcelFileDescriptor tReadEnd = mTransportPipes[0];
+            ParcelFileDescriptor tWriteEnd = mTransportPipes[1];
+
+            int bufferSize = 32 * 1024;
+            byte[] buffer = new byte[bufferSize];
+            FileOutputStream engineOut = new FileOutputStream(eWriteEnd.getFileDescriptor());
+            FileInputStream transportIn = new FileInputStream(tReadEnd.getFileDescriptor());
+
+            // spin up the engine and start moving data to it
+            new Thread(mEngineThread, "unified-restore-engine").start();
+
+            try {
+                while (status == BackupTransport.TRANSPORT_OK) {
+                    // have the transport write some of the restoring data to us
+                    int result = mTransport.getNextFullRestoreDataChunk(tWriteEnd);
+                    if (result > 0) {
+                        // The transport wrote this many bytes of restore data to the
+                        // pipe, so pass it along to the engine.
+                        if (RefactoredBackupManagerService.MORE_DEBUG) {
+                            Slog.v(RefactoredBackupManagerService.TAG, "  <- transport provided chunk size " + result);
+                        }
+                        if (result > bufferSize) {
+                            bufferSize = result;
+                            buffer = new byte[bufferSize];
+                        }
+                        int toCopy = result;
+                        while (toCopy > 0) {
+                            int n = transportIn.read(buffer, 0, toCopy);
+                            engineOut.write(buffer, 0, n);
+                            toCopy -= n;
+                            if (RefactoredBackupManagerService.MORE_DEBUG) {
+                                Slog.v(RefactoredBackupManagerService.TAG, "  -> wrote " + n + " to engine, left=" + toCopy);
+                            }
+                        }
+                    } else if (result == BackupTransport.NO_MORE_DATA) {
+                        // Clean finish.  Wind up and we're done!
+                        if (RefactoredBackupManagerService.MORE_DEBUG) {
+                            Slog.i(RefactoredBackupManagerService.TAG, "Got clean full-restore EOF for "
+                                + mCurrentPackage.packageName);
+                        }
+                        status = BackupTransport.TRANSPORT_OK;
+                        break;
+                    } else {
+                        // Transport reported some sort of failure; the fall-through
+                        // handling will deal properly with that.
+                        Slog.e(RefactoredBackupManagerService.TAG, "Error " + result + " streaming restore for "
+                            + mCurrentPackage.packageName);
+                        EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
+                        status = result;
+                    }
+                }
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                  Slog.v(RefactoredBackupManagerService.TAG, "Done copying to engine, falling through");
+                }
+            } catch (IOException e) {
+                // We lost our ability to communicate via the pipes.  That's worrying
+                // but potentially recoverable; abandon this package's restore but
+                // carry on with the next restore target.
+                Slog.e(RefactoredBackupManagerService.TAG, "Unable to route data for restore");
+                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
+                    mCurrentPackage.packageName, "I/O error on pipes");
+                status = BackupTransport.AGENT_ERROR;
+            } catch (Exception e) {
+                // The transport threw; terminate the whole operation.  Closing
+                // the sockets will wake up the engine and it will then tidy up the
+                // remote end.
+                Slog.e(RefactoredBackupManagerService.TAG, "Transport failed during restore: " + e.getMessage());
+                EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
+                status = BackupTransport.TRANSPORT_ERROR;
+            } finally {
+                // Close the transport pipes and *our* end of the engine pipe,
+                // but leave the engine thread's end open so that it properly
+                // hits EOF and winds up its operations.
+                IoUtils.closeQuietly(mEnginePipes[1]);
+                IoUtils.closeQuietly(mTransportPipes[0]);
+                IoUtils.closeQuietly(mTransportPipes[1]);
+
+                // Don't proceed until the engine has wound up operations
+                mEngineThread.waitForResult();
+
+                // Now we're really done with this one too
+                IoUtils.closeQuietly(mEnginePipes[0]);
+
+                // In all cases we want to remember whether we launched
+                // the target app as part of our work so far.
+                mDidLaunch = (mEngine.getAgent() != null);
+
+                // If we hit a transport-level error, we are done with everything;
+                // if we hit an agent error we just go back to running the queue.
+                if (status == BackupTransport.TRANSPORT_OK) {
+                    // Clean finish means we issue the restore-finished callback
+                    nextState = UnifiedRestoreState.RESTORE_FINISHED;
+
+                    // the engine bound the target's agent, so recover that binding
+                    // to use for the callback.
+                    mAgent = mEngine.getAgent();
+
+                    // and the restored widget data, if any
+                    mWidgetData = mEngine.getWidgetData();
+                } else {
+                    // Something went wrong somewhere.  Whether it was at the transport
+                    // level is immaterial; we need to tell the transport to bail
+                    try {
+                        mTransport.abortFullRestore();
+                    } catch (Exception e) {
+                        // transport itself is dead; make sure we handle this as a
+                        // fatal error
+                        Slog.e(RefactoredBackupManagerService.TAG, "Transport threw from abortFullRestore: " + e.getMessage());
+                        status = BackupTransport.TRANSPORT_ERROR;
+                    }
+
+                    // We also need to wipe the current target's data, as it's probably
+                    // in an incoherent state.
+                    backupManagerService.clearApplicationDataSynchronous(mCurrentPackage.packageName);
+
+                    // Schedule the next state based on the nature of our failure
+                    if (status == BackupTransport.TRANSPORT_ERROR) {
+                        nextState = UnifiedRestoreState.FINAL;
+                    } else {
+                        nextState = UnifiedRestoreState.RUNNING_QUEUE;
+                    }
+                }
+                executeNextState(nextState);
+                setRunning(false);
+            }
+        }
+
+        // BackupRestoreTask interface, specifically for timeout handling
+
+        @Override
+        public void execute() { /* intentionally empty */ }
+
+        @Override
+        public void operationComplete(long result) { /* intentionally empty */ }
+
+        // The app has timed out handling a restoring file
+        @Override
+        public void handleCancel(boolean cancelAll) {
+            backupManagerService.removeOperation(mEphemeralOpToken);
+            if (RefactoredBackupManagerService.DEBUG) {
+                Slog.w(RefactoredBackupManagerService.TAG, "Full-data restore target timed out; shutting down");
+            }
+
+            mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+                BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_TIMEOUT,
+                mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
+            mEngineThread.handleTimeout();
+
+            IoUtils.closeQuietly(mEnginePipes[1]);
+            mEnginePipes[1] = null;
+            IoUtils.closeQuietly(mEnginePipes[0]);
+            mEnginePipes[0] = null;
+        }
+    }
+
+    class EngineThread implements Runnable {
+
+      FullRestoreEngine mEngine;
+        FileInputStream mEngineStream;
+
+        EngineThread(FullRestoreEngine engine, ParcelFileDescriptor engineSocket) {
+            mEngine = engine;
+            engine.setRunning(true);
+            // We *do* want this FileInputStream to own the underlying fd, so that
+            // when we are finished with it, it closes this end of the pipe in a way
+            // that signals its other end.
+            mEngineStream = new FileInputStream(engineSocket.getFileDescriptor(), true);
+        }
+
+        public boolean isRunning() {
+            return mEngine.isRunning();
+        }
+
+        public int waitForResult() {
+            return mEngine.waitForResult();
+        }
+
+        @Override
+        public void run() {
+            try {
+                while (mEngine.isRunning()) {
+                    // Tell it to be sure to leave the agent instance up after finishing
+                    mEngine.restoreOneFile(mEngineStream, false);
+                }
+            } finally {
+                // Because mEngineStream adopted its underlying FD, this also
+                // closes this end of the pipe.
+                IoUtils.closeQuietly(mEngineStream);
+            }
+        }
+
+        public void handleTimeout() {
+            IoUtils.closeQuietly(mEngineStream);
+            mEngine.handleTimeout();
+        }
+    }
+
+    // state FINAL : tear everything down and we're done.
+    private void finalizeRestore() {
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+          Slog.d(RefactoredBackupManagerService.TAG, "finishing restore mObserver=" + mObserver);
+        }
+
+        try {
+            mTransport.finishRestore();
+        } catch (Exception e) {
+            Slog.e(RefactoredBackupManagerService.TAG, "Error finishing restore", e);
+        }
+
+        // Tell the observer we're done
+        if (mObserver != null) {
+            try {
+                mObserver.restoreFinished(mStatus);
+            } catch (RemoteException e) {
+                Slog.d(RefactoredBackupManagerService.TAG, "Restore observer died at restoreFinished");
+            }
+        }
+
+        // Clear any ongoing session timeout.
+        backupManagerService.mBackupHandler.removeMessages(
+            RefactoredBackupManagerService.MSG_RESTORE_SESSION_TIMEOUT);
+
+        // If we have a PM token, we must under all circumstances be sure to
+        // handshake when we've finished.
+        if (mPmToken > 0) {
+            if (RefactoredBackupManagerService.MORE_DEBUG) {
+              Slog.v(RefactoredBackupManagerService.TAG, "finishing PM token " + mPmToken);
+            }
+            try {
+                backupManagerService.mPackageManagerBinder.finishPackageInstall(mPmToken, mDidLaunch);
+            } catch (RemoteException e) { /* can't happen */ }
+        } else {
+            // We were invoked via an active restore session, not by the Package
+            // Manager, so start up the session timeout again.
+            backupManagerService.mBackupHandler.sendEmptyMessageDelayed(
+                RefactoredBackupManagerService.MSG_RESTORE_SESSION_TIMEOUT,
+                RefactoredBackupManagerService.TIMEOUT_RESTORE_INTERVAL);
+        }
+
+        // Kick off any work that may be needed regarding app widget restores
+        // TODO: http://b/22388012
+        AppWidgetBackupBridge.restoreFinished(UserHandle.USER_SYSTEM);
+
+        // If this was a full-system restore, record the ancestral
+        // dataset information
+        if (mIsSystemRestore && mPmAgent != null) {
+            backupManagerService.mAncestralPackages = mPmAgent.getRestoredPackages();
+            backupManagerService.mAncestralToken = mToken;
+            backupManagerService.writeRestoreTokens();
+        }
+
+        // done; we can finally release the wakelock and be legitimately done.
+        Slog.i(RefactoredBackupManagerService.TAG, "Restore complete.");
+
+        synchronized (backupManagerService.mPendingRestores) {
+            if (backupManagerService.mPendingRestores.size() > 0) {
+                if (RefactoredBackupManagerService.DEBUG) {
+                    Slog.d(RefactoredBackupManagerService.TAG, "Starting next pending restore.");
+                }
+                PerformUnifiedRestoreTask task = backupManagerService.mPendingRestores.remove();
+                backupManagerService.mBackupHandler.sendMessage(
+                    backupManagerService.mBackupHandler.obtainMessage(
+                        RefactoredBackupManagerService.MSG_BACKUP_RESTORE_STEP, task));
+
+            } else {
+                backupManagerService.mIsRestoreInProgress = false;
+                if (RefactoredBackupManagerService.MORE_DEBUG) {
+                    Slog.d(RefactoredBackupManagerService.TAG, "No pending restores.");
+                }
+            }
+        }
+
+        backupManagerService.mWakelock.release();
+    }
+
+    void keyValueAgentErrorCleanup() {
+        // If the agent fails restore, it might have put the app's data
+        // into an incoherent state.  For consistency we wipe its data
+        // again in this case before continuing with normal teardown
+        backupManagerService.clearApplicationDataSynchronous(mCurrentPackage.packageName);
+        keyValueAgentCleanup();
+    }
+
+    // TODO: clean up naming; this is now used at finish by both k/v and stream restores
+    void keyValueAgentCleanup() {
+        mBackupDataName.delete();
+        mStageName.delete();
+        try {
+          if (mBackupData != null) {
+            mBackupData.close();
+          }
+        } catch (IOException e) {
+        }
+        try {
+          if (mNewState != null) {
+            mNewState.close();
+          }
+        } catch (IOException e) {
+        }
+        mBackupData = mNewState = null;
+
+        // if everything went okay, remember the recorded state now
+        //
+        // !!! TODO: the restored data could be migrated on the server
+        // side into the current dataset.  In that case the new state file
+        // we just created would reflect the data already extant in the
+        // backend, so there'd be nothing more to do.  Until that happens,
+        // however, we need to make sure that we record the data to the
+        // current backend dataset.  (Yes, this means shipping the data over
+        // the wire in both directions.  That's bad, but consistency comes
+        // first, then efficiency.)  Once we introduce server-side data
+        // migration to the newly-restored device's dataset, we will change
+        // the following from a discard of the newly-written state to the
+        // "correct" operation of renaming into the canonical state blob.
+        mNewStateName.delete();                      // TODO: remove; see above comment
+        //mNewStateName.renameTo(mSavedStateName);   // TODO: replace with this
+
+        // If this wasn't the PM pseudopackage, tear down the agent side
+        if (mCurrentPackage.applicationInfo != null) {
+            // unbind and tidy up even on timeout or failure
+            try {
+                backupManagerService.mActivityManager.unbindBackupAgent(mCurrentPackage.applicationInfo);
+
+                // The agent was probably running with a stub Application object,
+                // which isn't a valid run mode for the main app logic.  Shut
+                // down the app so that next time it's launched, it gets the
+                // usual full initialization.  Note that this is only done for
+                // full-system restores: when a single app has requested a restore,
+                // it is explicitly not killed following that operation.
+                //
+                // We execute this kill when these conditions hold:
+                //    1. it's not a system-uid process,
+                //    2. the app did not request its own restore (mTargetPackage == null), and either
+                //    3a. the app is a full-data target (TYPE_FULL_STREAM) or
+                //     b. the app does not state android:killAfterRestore="false" in its manifest
+                final int appFlags = mCurrentPackage.applicationInfo.flags;
+                final boolean killAfterRestore =
+                    (mCurrentPackage.applicationInfo.uid >= Process.FIRST_APPLICATION_UID)
+                        && ((mRestoreDescription.getDataType()
+                        == RestoreDescription.TYPE_FULL_STREAM)
+                        || ((appFlags & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0));
+
+                if (mTargetPackage == null && killAfterRestore) {
+                    if (RefactoredBackupManagerService.DEBUG) {
+                      Slog.d(RefactoredBackupManagerService.TAG, "Restore complete, killing host process of "
+                          + mCurrentPackage.applicationInfo.processName);
+                    }
+                    backupManagerService.mActivityManager.killApplicationProcess(
+                        mCurrentPackage.applicationInfo.processName,
+                        mCurrentPackage.applicationInfo.uid);
+                }
+            } catch (RemoteException e) {
+                // can't happen; we run in the same process as the activity manager
+            }
+        }
+
+        // The caller is responsible for reestablishing the state machine; our
+        // responsibility here is to clear the decks for whatever comes next.
+        backupManagerService.mBackupHandler.removeMessages(
+            RefactoredBackupManagerService.MSG_RESTORE_OPERATION_TIMEOUT, this);
+    }
+
+    @Override
+    public void operationComplete(long unusedResult) {
+        backupManagerService.removeOperation(mEphemeralOpToken);
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+            Slog.i(RefactoredBackupManagerService.TAG, "operationComplete() during restore: target="
+                + mCurrentPackage.packageName
+                + " state=" + mState);
+        }
+
+        final UnifiedRestoreState nextState;
+        switch (mState) {
+            case INITIAL:
+                // We've just (manually) restored the PMBA.  It doesn't need the
+                // additional restore-finished callback so we bypass that and go
+                // directly to running the queue.
+                nextState = UnifiedRestoreState.RUNNING_QUEUE;
+                break;
+
+            case RESTORE_KEYVALUE:
+            case RESTORE_FULL: {
+                // Okay, we've just heard back from the agent that it's done with
+                // the restore itself.  We now have to send the same agent its
+                // doRestoreFinished() callback, so roll into that state.
+                nextState = UnifiedRestoreState.RESTORE_FINISHED;
+                break;
+            }
+
+            case RESTORE_FINISHED: {
+                // Okay, we're done with this package.  Tidy up and go on to the next
+                // app in the queue.
+                int size = (int) mBackupDataName.length();
+                EventLog.writeEvent(EventLogTags.RESTORE_PACKAGE,
+                    mCurrentPackage.packageName, size);
+
+                // Just go back to running the restore queue
+                keyValueAgentCleanup();
+
+                // If there was widget state associated with this app, get the OS to
+                // incorporate it into current bookeeping and then pass that along to
+                // the app as part of the restore-time work.
+                if (mWidgetData != null) {
+                    backupManagerService.restoreWidgetData(mCurrentPackage.packageName, mWidgetData);
+                }
+
+                nextState = UnifiedRestoreState.RUNNING_QUEUE;
+                break;
+            }
+
+            default: {
+                // Some kind of horrible semantic error; we're in an unexpected state.
+                // Back off hard and wind up.
+                Slog.e(
+                    RefactoredBackupManagerService.TAG, "Unexpected restore callback into state " + mState);
+                keyValueAgentErrorCleanup();
+                nextState = UnifiedRestoreState.FINAL;
+                break;
+            }
+        }
+
+        executeNextState(nextState);
+    }
+
+    // A call to agent.doRestore() or agent.doRestoreFinished() has timed out
+    @Override
+    public void handleCancel(boolean cancelAll) {
+        backupManagerService.removeOperation(mEphemeralOpToken);
+        Slog.e(RefactoredBackupManagerService.TAG, "Timeout restoring application " + mCurrentPackage.packageName);
+        mMonitor = RefactoredBackupManagerService.monitorEvent(mMonitor,
+            BackupManagerMonitor.LOG_EVENT_ID_KEY_VALUE_RESTORE_TIMEOUT,
+            mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
+        EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
+            mCurrentPackage.packageName, "restore timeout");
+        // Handle like an agent that threw on invocation: wipe it and go on to the next
+        keyValueAgentErrorCleanup();
+        executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
+    }
+
+    void executeNextState(UnifiedRestoreState nextState) {
+        if (RefactoredBackupManagerService.MORE_DEBUG) {
+          Slog.i(RefactoredBackupManagerService.TAG, " => executing next step on "
+              + this + " nextState=" + nextState);
+        }
+        mState = nextState;
+        Message msg = backupManagerService.mBackupHandler.obtainMessage(
+            RefactoredBackupManagerService.MSG_BACKUP_RESTORE_STEP, this);
+        backupManagerService.mBackupHandler.sendMessage(msg);
+    }
+
+    // restore observer support
+    void sendStartRestore(int numPackages) {
+        if (mObserver != null) {
+            try {
+                mObserver.restoreStarting(numPackages);
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "Restore observer went away: startRestore");
+                mObserver = null;
+            }
+        }
+    }
+
+    void sendOnRestorePackage(String name) {
+        if (mObserver != null) {
+            if (mObserver != null) {
+                try {
+                    mObserver.onUpdate(mCount, name);
+                } catch (RemoteException e) {
+                    Slog.d(RefactoredBackupManagerService.TAG, "Restore observer died in onUpdate");
+                    mObserver = null;
+                }
+            }
+        }
+    }
+
+    void sendEndRestore() {
+        if (mObserver != null) {
+            try {
+                mObserver.restoreFinished(mStatus);
+            } catch (RemoteException e) {
+                Slog.w(RefactoredBackupManagerService.TAG, "Restore observer went away: endRestore");
+                mObserver = null;
+            }
+        }
+    }
+}
diff --git a/services/backup/java/com/android/server/backup/restore/RestoreEngine.java b/services/backup/java/com/android/server/backup/restore/RestoreEngine.java
new file mode 100644
index 0000000..b2fdbb8
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/restore/RestoreEngine.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2017 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.server.backup.restore;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Restore infrastructure.
+ */
+public abstract class RestoreEngine {
+
+    private static final String TAG = "RestoreEngine";
+
+    public static final int SUCCESS = 0;
+    public static final int TARGET_FAILURE = -2;
+    public static final int TRANSPORT_FAILURE = -3;
+
+    private AtomicBoolean mRunning = new AtomicBoolean(false);
+    private AtomicInteger mResult = new AtomicInteger(SUCCESS);
+
+    public boolean isRunning() {
+        return mRunning.get();
+    }
+
+    public void setRunning(boolean stillRunning) {
+        synchronized (mRunning) {
+            mRunning.set(stillRunning);
+            mRunning.notifyAll();
+        }
+    }
+
+    public int waitForResult() {
+        synchronized (mRunning) {
+            while (isRunning()) {
+                try {
+                    mRunning.wait();
+                } catch (InterruptedException e) {
+                }
+            }
+        }
+        return getResult();
+    }
+
+    public int getResult() {
+        return mResult.get();
+    }
+
+    public void setResult(int result) {
+        mResult.set(result);
+    }
+
+    // TODO: abstract restore state and APIs
+}
diff --git a/services/backup/java/com/android/server/backup/restore/RestorePolicy.java b/services/backup/java/com/android/server/backup/restore/RestorePolicy.java
new file mode 100644
index 0000000..db25472
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/restore/RestorePolicy.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2017 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.server.backup.restore;
+
+/**
+ * Full restore from a file/socket.
+ */
+public enum RestorePolicy {
+    IGNORE,
+    ACCEPT,
+    ACCEPT_IF_APK
+}
diff --git a/services/backup/java/com/android/server/backup/restore/UnifiedRestoreState.java b/services/backup/java/com/android/server/backup/restore/UnifiedRestoreState.java
new file mode 100644
index 0000000..f5bff5e
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/restore/UnifiedRestoreState.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2017 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.server.backup.restore;
+
+/**
+ * States of the unified-restore state machine.
+ */
+public enum UnifiedRestoreState {
+    INITIAL,
+    RUNNING_QUEUE,
+    RESTORE_KEYVALUE,
+    RESTORE_FULL,
+    RESTORE_FINISHED,
+    FINAL
+}
diff --git a/services/core/java/com/android/server/BluetoothManagerService.java b/services/core/java/com/android/server/BluetoothManagerService.java
index 6c4895c..7b7d340 100644
--- a/services/core/java/com/android/server/BluetoothManagerService.java
+++ b/services/core/java/com/android/server/BluetoothManagerService.java
@@ -18,6 +18,7 @@
 
 import android.Manifest;
 import android.app.ActivityManager;
+import android.app.AppGlobals;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothProfile;
 import android.bluetooth.IBluetooth;
@@ -37,11 +38,11 @@
 import android.content.IntentFilter;
 import android.content.ServiceConnection;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.IPackageManager;
 import android.content.pm.PackageManager;
 import android.content.pm.UserInfo;
 import android.database.ContentObserver;
 import android.os.Binder;
-import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -50,7 +51,6 @@
 import android.os.Process;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
-import android.os.ServiceManager;
 import android.os.SystemClock;
 import android.os.UserHandle;
 import android.os.UserManager;
@@ -61,15 +61,15 @@
 import android.util.Slog;
 
 import com.android.internal.util.DumpUtils;
-import com.android.server.pm.PackageManagerService;
+import com.android.server.pm.UserRestrictionsUtils;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 
 class BluetoothManagerService extends IBluetoothManager.Stub {
@@ -120,7 +120,6 @@
     private static final int MESSAGE_ADD_PROXY_DELAYED = 400;
     private static final int MESSAGE_BIND_PROFILE_SERVICE = 401;
 
-    private static final int MAX_SAVE_RETRIES = 3;
     private static final int MAX_ERROR_RESTART_RETRIES = 6;
 
     // Bluetooth persisted setting is off
@@ -223,22 +222,25 @@
         @Override
         public void onUserRestrictionsChanged(int userId, Bundle newRestrictions,
                 Bundle prevRestrictions) {
-            if (!newRestrictions.containsKey(UserManager.DISALLOW_BLUETOOTH)
-                    && !prevRestrictions.containsKey(UserManager.DISALLOW_BLUETOOTH)) {
-                // The relevant restriction has not changed - do nothing.
-                return;
+            if (!UserRestrictionsUtils.restrictionsChanged(prevRestrictions, newRestrictions,
+                    UserManager.DISALLOW_BLUETOOTH, UserManager.DISALLOW_BLUETOOTH_SHARING)) {
+                return; // No relevant changes, nothing to do.
             }
-            final boolean bluetoothDisallowed =
-                    newRestrictions.getBoolean(UserManager.DISALLOW_BLUETOOTH);
-            if ((mEnable || mEnableExternal) && bluetoothDisallowed) {
+
+            final boolean disallowed = newRestrictions.getBoolean(UserManager.DISALLOW_BLUETOOTH);
+
+            // DISALLOW_BLUETOOTH is a global restriction that can only be set by DO or PO on the
+            // system user, so we only look at the system user.
+            if (userId == UserHandle.USER_SYSTEM && disallowed && (mEnable || mEnableExternal)) {
                 try {
-                    disable(null, true);
+                    disable(null /* packageName */, true /* persist */);
                 } catch (RemoteException e) {
-                    Slog.w(TAG, "Exception when disabling Bluetooth from UserRestrictionsListener",
-                            e);
+                    Slog.w(TAG, "Exception when disabling Bluetooth", e);
                 }
             }
-            updateOppLauncherComponentState(bluetoothDisallowed);
+            final boolean sharingDisallowed = disallowed
+                    || newRestrictions.getBoolean(UserManager.DISALLOW_BLUETOOTH_SHARING);
+            updateOppLauncherComponentState(userId, sharingDisallowed);
         }
     };
 
@@ -983,11 +985,6 @@
                 LocalServices.getService(UserManagerInternal.class);
         userManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener);
         final boolean isBluetoothDisallowed = isBluetoothDisallowed();
-        PackageManagerService packageManagerService =
-                (PackageManagerService) ServiceManager.getService("package");
-        if (packageManagerService != null && !packageManagerService.isOnlyCoreApps()) {
-            updateOppLauncherComponentState(isBluetoothDisallowed);
-        }
         if (isBluetoothDisallowed) {
             return;
         }
@@ -2063,21 +2060,21 @@
 
     /**
      * Disables BluetoothOppLauncherActivity component, so the Bluetooth sharing option is not
-     * offered to the user if Bluetooth is disallowed. Puts the component to its default state if
-     * Bluetooth is not disallowed.
+     * offered to the user if Bluetooth or sharing is disallowed. Puts the component to its default
+     * state if Bluetooth is not disallowed.
      *
-     * @param bluetoothDisallowed whether the {@link UserManager.DISALLOW_BLUETOOTH} user
-     * restriction was set.
+     * @param userId user to disable bluetooth sharing for.
+     * @param bluetoothSharingDisallowed whether bluetooth sharing is disallowed.
      */
-    private void updateOppLauncherComponentState(boolean bluetoothDisallowed) {
+    private void updateOppLauncherComponentState(int userId, boolean bluetoothSharingDisallowed) {
         final ComponentName oppLauncherComponent = new ComponentName("com.android.bluetooth",
                 "com.android.bluetooth.opp.BluetoothOppLauncherActivity");
-        final int newState = bluetoothDisallowed
+        final int newState = bluetoothSharingDisallowed
                 ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                 : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
         try {
-            mContext.getPackageManager()
-                    .setComponentEnabledSetting(oppLauncherComponent, newState, 0);
+            final IPackageManager imp = AppGlobals.getPackageManager();
+            imp.setComponentEnabledSetting(oppLauncherComponent, newState, 0 /* flags */, userId);
         } catch (Exception e) {
             // The component was not found, do nothing.
         }
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 35654d7..82d5439 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -23761,24 +23761,6 @@
     }
 
     @Override
-    public boolean canBypassWorkChallenge(PendingIntent intent) throws RemoteException {
-        final int userId = intent.getCreatorUserHandle().getIdentifier();
-        if (!mUserController.isUserRunningLocked(userId, ActivityManager.FLAG_AND_LOCKED)) {
-            return false;
-        }
-        IIntentSender target = intent.getTarget();
-        if (!(target instanceof PendingIntentRecord)) {
-            return false;
-        }
-        final PendingIntentRecord record = (PendingIntentRecord) target;
-        final ResolveInfo rInfo = mStackSupervisor.resolveIntent(record.key.requestIntent,
-                record.key.requestResolvedType, userId, PackageManager.MATCH_DIRECT_BOOT_AWARE);
-        // For direct boot aware activities, they can be shown without triggering a work challenge
-        // before the profile user is unlocked.
-        return rInfo != null && rInfo.activityInfo != null;
-    }
-
-    @Override
     public void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback)
             throws RemoteException {
         final long callingId = Binder.clearCallingIdentity();
diff --git a/services/core/java/com/android/server/am/ActivityRecord.java b/services/core/java/com/android/server/am/ActivityRecord.java
index 95d7158..cfbd2b5 100644
--- a/services/core/java/com/android/server/am/ActivityRecord.java
+++ b/services/core/java/com/android/server/am/ActivityRecord.java
@@ -1561,8 +1561,8 @@
         mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
     }
 
-    void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
-        mWindowContainerController.notifyAppResumed(wasStopped, allowSavedSurface);
+    void notifyAppResumed(boolean wasStopped) {
+        mWindowContainerController.notifyAppResumed(wasStopped);
     }
 
     void notifyUnknownVisibilityLaunched() {
@@ -2112,7 +2112,8 @@
                 service.compatibilityInfoForPackageLocked(info.applicationInfo);
         final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
                 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
-                prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning());
+                prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning(),
+                allowTaskSnapshot());
         if (shown) {
             mStartingWindowState = STARTING_WINDOW_SHOWN;
         }
@@ -2552,7 +2553,7 @@
         preserveWindowOnDeferredRelaunch = false;
     }
 
-    boolean isProcessRunning() {
+    private boolean isProcessRunning() {
         ProcessRecord proc = app;
         if (proc == null) {
             proc = service.mProcessNames.get(processName, info.applicationInfo.uid);
@@ -2560,6 +2561,26 @@
         return proc != null && proc.thread != null;
     }
 
+    /**
+     * @return Whether a task snapshot starting window may be shown.
+     */
+    private boolean allowTaskSnapshot() {
+        if (newIntents == null) {
+            return true;
+        }
+
+        // Restrict task snapshot starting window to launcher start, or there is no intent at all
+        // (eg. task being brought to front). If the intent is something else, likely the app is
+        // going to show some specific page or view, instead of what's left last time.
+        for (int i = newIntents.size() - 1; i >= 0; i--) {
+            final Intent intent = newIntents.get(i);
+            if (intent != null && !ActivityRecord.isMainIntent(intent)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
         out.attribute(null, ATTR_ID, String.valueOf(createTime));
         out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java
index 2d2b34e..be694ed 100644
--- a/services/core/java/com/android/server/am/ActivityStack.java
+++ b/services/core/java/com/android/server/am/ActivityStack.java
@@ -2531,26 +2531,14 @@
                     }
                 }
 
-                boolean allowSavedSurface = true;
                 if (next.newIntents != null) {
-                    // Restrict saved surface to launcher start, or there is no intent at all
-                    // (eg. task being brought to front). If the intent is something else,
-                    // likely the app is going to show some specific page or view, instead of
-                    // what's left last time.
-                    for (int i = next.newIntents.size() - 1; i >= 0; i--) {
-                        final Intent intent = next.newIntents.get(i);
-                        if (intent != null && !ActivityRecord.isMainIntent(intent)) {
-                            allowSavedSurface = false;
-                            break;
-                        }
-                    }
                     next.app.thread.scheduleNewIntent(
                             next.newIntents, next.appToken, false /* andPause */);
                 }
 
                 // Well the app will no longer be stopped.
                 // Clear app token stopped state in window manager if needed.
-                next.notifyAppResumed(next.stopped, allowSavedSurface);
+                next.notifyAppResumed(next.stopped);
 
                 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
                         System.identityHashCode(next), next.getTask().taskId,
diff --git a/services/core/java/com/android/server/am/ActivityStartInterceptor.java b/services/core/java/com/android/server/am/ActivityStartInterceptor.java
index cafc4f0..b91c7b1 100644
--- a/services/core/java/com/android/server/am/ActivityStartInterceptor.java
+++ b/services/core/java/com/android/server/am/ActivityStartInterceptor.java
@@ -210,11 +210,7 @@
         if (!mService.mUserController.shouldConfirmCredentials(userId)) {
             return null;
         }
-        // Allow direct boot aware activity to be displayed before the user is unlocked.
-        if (aInfo.directBootAware && mService.mUserController.isUserRunningLocked(userId,
-                ActivityManager.FLAG_AND_LOCKED)) {
-            return null;
-        }
+        // TODO(b/28935539): should allow certain activities to bypass work challenge
         final IIntentSender target = mService.getIntentSenderLocked(
                 INTENT_SENDER_ACTIVITY, callingPackage,
                 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
diff --git a/services/core/java/com/android/server/audio/PlaybackActivityMonitor.java b/services/core/java/com/android/server/audio/PlaybackActivityMonitor.java
index 51aa4f8..9eda929 100644
--- a/services/core/java/com/android/server/audio/PlaybackActivityMonitor.java
+++ b/services/core/java/com/android/server/audio/PlaybackActivityMonitor.java
@@ -57,7 +57,7 @@
                 .setCurve(new float[] { 0.f, 1.f } /* times */,
                     new float[] { 1.f, 0.2f } /* volumes */)
                 .setOptionFlags(VolumeShaper.Configuration.OPTION_FLAG_CLOCK_TIME)
-                .setDurationMs(MediaFocusControl.getFocusRampTimeMs(
+                .setDurationMillis(MediaFocusControl.getFocusRampTimeMs(
                     AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
                     new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION)
                             .build()))
diff --git a/services/core/java/com/android/server/connectivity/NetworkMonitor.java b/services/core/java/com/android/server/connectivity/NetworkMonitor.java
index 9ec316e..3e53aca 100644
--- a/services/core/java/com/android/server/connectivity/NetworkMonitor.java
+++ b/services/core/java/com/android/server/connectivity/NetworkMonitor.java
@@ -259,6 +259,12 @@
     // This variable is set before transitioning to the mCaptivePortalState.
     private CaptivePortalProbeResult mLastPortalProbeResult = CaptivePortalProbeResult.FAILED;
 
+    // Configuration values for captive portal detection probes.
+    private final String mCaptivePortalUserAgent;
+    private final URL mCaptivePortalHttpsUrl;
+    private final URL mCaptivePortalHttpUrl;
+    private final URL mCaptivePortalFallbackUrl;
+
     public NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo,
             NetworkRequest defaultRequest) {
         this(context, handler, networkAgentInfo, defaultRequest, new IpConnectivityLog());
@@ -293,6 +299,11 @@
         mUseHttps = Settings.Global.getInt(mContext.getContentResolver(),
                 Settings.Global.CAPTIVE_PORTAL_USE_HTTPS, 1) == 1;
 
+        mCaptivePortalUserAgent = getCaptivePortalUserAgent(context);
+        mCaptivePortalHttpsUrl = makeURL(getCaptivePortalServerHttpsUrl(context));
+        mCaptivePortalHttpUrl = makeURL(getCaptivePortalServerHttpUrl(context));
+        mCaptivePortalFallbackUrl = makeURL(getCaptivePortalFallbackUrl(context));
+
         start();
     }
 
@@ -676,7 +687,10 @@
             return new CaptivePortalProbeResult(204);
         }
 
-        URL pacUrl = null, httpsUrl = null, httpUrl = null, fallbackUrl = null;
+        URL pacUrl = null;
+        URL httpsUrl = mCaptivePortalHttpsUrl;
+        URL httpUrl = mCaptivePortalHttpUrl;
+        URL fallbackUrl = mCaptivePortalFallbackUrl;
 
         // On networks with a PAC instead of fetching a URL that should result in a 204
         // response, we instead simply fetch the PAC script.  This is done for a few reasons:
@@ -703,13 +717,8 @@
             }
         }
 
-        if (pacUrl == null) {
-            httpsUrl = makeURL(getCaptivePortalServerHttpsUrl(mContext));
-            httpUrl = makeURL(getCaptivePortalServerHttpUrl(mContext));
-            fallbackUrl = makeURL(getCaptivePortalFallbackUrl(mContext));
-            if (httpUrl == null || httpsUrl == null) {
-                return CaptivePortalProbeResult.FAILED;
-            }
+        if ((pacUrl == null) && (httpUrl == null || httpsUrl == null)) {
+            return CaptivePortalProbeResult.FAILED;
         }
 
         long startTime = SystemClock.elapsedRealtime();
@@ -790,9 +799,8 @@
             urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
             urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
             urlConnection.setUseCaches(false);
-            final String userAgent = getCaptivePortalUserAgent(mContext);
-            if (userAgent != null) {
-                urlConnection.setRequestProperty("User-Agent", userAgent);
+            if (mCaptivePortalUserAgent != null) {
+                urlConnection.setRequestProperty("User-Agent", mCaptivePortalUserAgent);
             }
             // cannot read request header after connection
             String requestHeader = urlConnection.getRequestProperties().toString();
diff --git a/services/core/java/com/android/server/fingerprint/EnumerateClient.java b/services/core/java/com/android/server/fingerprint/EnumerateClient.java
index 34f245f..1b8b89c 100644
--- a/services/core/java/com/android/server/fingerprint/EnumerateClient.java
+++ b/services/core/java/com/android/server/fingerprint/EnumerateClient.java
@@ -58,7 +58,7 @@
     public int stop(boolean initiatedByClient) {
         IBiometricsFingerprint daemon = getFingerprintDaemon();
         if (daemon == null) {
-            Slog.w(TAG, "stopAuthentication: no fingerprint HAL!");
+            Slog.w(TAG, "stopEnumeration: no fingerprint HAL!");
             return ERROR_ESRCH;
         }
         try {
@@ -102,12 +102,12 @@
     @Override
     public boolean onEnrollResult(int fingerId, int groupId, int rem) {
         if (DEBUG) Slog.w(TAG, "onEnrollResult() called for enumerate!");
-        return true; // Invalid for Remove
+        return true; // Invalid for Enumerate.
     }
 
     @Override
     public boolean onRemoved(int fingerId, int groupId, int remaining) {
         if (DEBUG) Slog.w(TAG, "onRemoved() called for enumerate!");
-        return true; // Invalid for Authenticate
+        return true; // Invalid for Enumerate.
     }
 }
diff --git a/services/core/java/com/android/server/fingerprint/FingerprintService.java b/services/core/java/com/android/server/fingerprint/FingerprintService.java
index 85f7056..9b984c1 100644
--- a/services/core/java/com/android/server/fingerprint/FingerprintService.java
+++ b/services/core/java/com/android/server/fingerprint/FingerprintService.java
@@ -85,6 +85,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.LinkedList;
 
 /**
  * A service to manage multiple clients that want to access the fingerprint HAL API.
@@ -96,6 +97,7 @@
 public class FingerprintService extends SystemService implements IHwBinder.DeathRecipient {
     static final String TAG = "FingerprintService";
     static final boolean DEBUG = true;
+    private static final boolean CLEANUP_UNUSED_FP = false;
     private static final String FP_DATA_DIR = "fpdata";
     private static final int MSG_USER_SWITCHING = 10;
     private static final String ACTION_LOCKOUT_RESET =
@@ -134,6 +136,20 @@
     private ClientMonitor mPendingClient;
     private PerformanceStats mPerformanceStats;
 
+
+    private IBinder mToken = new Binder(); // used for internal FingerprintService enumeration
+    private LinkedList<Integer> mEnumeratingUserIds = new LinkedList<>();
+    private ArrayList<UserFingerprint> mUnknownFingerprints = new ArrayList<>(); // hw finterprints
+
+    private class UserFingerprint {
+        Fingerprint f;
+        int userId;
+        public UserFingerprint(Fingerprint f, int userId) {
+            this.f = f;
+            this.userId = userId;
+        }
+    }
+
     // Normal fingerprint authentications are tracked by mPerformanceMap.
     private HashMap<Integer, PerformanceStats> mPerformanceMap = new HashMap<>();
 
@@ -185,6 +201,7 @@
                     + (mCurrentClient != null ? mCurrentClient.getOwnerString() : "null")
                     + " failed to respond to cancel, starting client "
                     + (mPendingClient != null ? mPendingClient.getOwnerString() : "null"));
+
             mCurrentClient = null;
             startClient(mPendingClient, false);
         }
@@ -239,6 +256,7 @@
             if (mHalDeviceId != 0) {
                 loadAuthenticatorIds();
                 updateActiveGroup(ActivityManager.getCurrentUser(), null);
+                doFingerprintCleanup(ActivityManager.getCurrentUser());
             } else {
                 Slog.w(TAG, "Failed to open Fingerprint HAL!");
                 MetricsLogger.count(mContext, "fingerprintd_openhal_error", 1);
@@ -253,7 +271,6 @@
         // This operation can be expensive, so keep track of the elapsed time. Might need to move to
         // background if it takes too long.
         long t = System.currentTimeMillis();
-
         mAuthenticatorIds.clear();
         for (UserInfo user : UserManager.get(mContext).getUsers(true /* excludeDying */)) {
             int userId = getUserOrWorkProfileId(null, user.id);
@@ -268,14 +285,88 @@
         }
     }
 
+    private void doFingerprintCleanup(int userId) {
+        if (CLEANUP_UNUSED_FP) {
+            resetEnumerateState();
+            mEnumeratingUserIds.push(userId);
+            enumerateNextUser();
+        }
+    }
+
+    private void resetEnumerateState() {
+        if (DEBUG) Slog.v(TAG, "Enumerate cleaning up");
+        mEnumeratingUserIds.clear();
+        mUnknownFingerprints.clear();
+    }
+
+    private void enumerateNextUser() {
+        int nextUser = mEnumeratingUserIds.getFirst();
+        updateActiveGroup(nextUser, null);
+        boolean restricted = !hasPermission(MANAGE_FINGERPRINT);
+
+        if (DEBUG) Slog.v(TAG, "Enumerating user id " + nextUser + " of "
+                + mEnumeratingUserIds.size() + " remaining users");
+
+        startEnumerate(mToken, nextUser, null, restricted, true /* internal */);
+    }
+
+    // Remove unknown fingerprints from hardware
+    private void cleanupUnknownFingerprints() {
+        if (!mUnknownFingerprints.isEmpty()) {
+            Slog.w(TAG, "unknown fingerprint size: " + mUnknownFingerprints.size());
+            UserFingerprint uf = mUnknownFingerprints.get(0);
+            mUnknownFingerprints.remove(uf);
+            boolean restricted = !hasPermission(MANAGE_FINGERPRINT);
+            updateActiveGroup(uf.userId, null);
+            startRemove(mToken, uf.f.getFingerId(), uf.f.getGroupId(), uf.userId, null,
+                    restricted, true /* internal */);
+        } else {
+            resetEnumerateState();
+        }
+    }
+
     protected void handleEnumerate(long deviceId, int fingerId, int groupId, int remaining) {
-        if (DEBUG) Slog.w(TAG, "Enumerate: fid=" + fingerId + ", gid="
-                + groupId + "rem=" + remaining);
-        // TODO: coordinate names with framework
+        if (DEBUG) Slog.w(TAG, "Enumerate: fid=" + fingerId
+                + ", gid=" + groupId
+                + ", dev=" + deviceId
+                + ", rem=" + remaining);
+
+        ClientMonitor client = mCurrentClient;
+
+        if ( !(client instanceof InternalRemovalClient) && !(client instanceof EnumerateClient) ) {
+            return;
+        }
+        client.onEnumerationResult(fingerId, groupId, remaining);
+
+        // All fingerprints in hardware for this user were enumerated
+        if (remaining == 0) {
+            mEnumeratingUserIds.poll();
+
+            if (client instanceof InternalEnumerateClient) {
+                List<Fingerprint> enrolled = ((InternalEnumerateClient) client).getEnumeratedList();
+                Slog.w(TAG, "Added " + enrolled.size() + " enumerated fingerprints for deletion");
+                for (Fingerprint f : enrolled) {
+                    mUnknownFingerprints.add(new UserFingerprint(f, client.getTargetUserId()));
+                }
+            }
+
+            removeClient(client);
+
+            if (!mEnumeratingUserIds.isEmpty()) {
+                enumerateNextUser();
+            } else if (client instanceof InternalEnumerateClient) {
+                if (DEBUG) Slog.v(TAG, "Finished enumerating all users");
+                // This will start a chain of InternalRemovalClients
+                cleanupUnknownFingerprints();
+            }
+        }
     }
 
     protected void handleError(long deviceId, int error, int vendorCode) {
         ClientMonitor client = mCurrentClient;
+        if (client instanceof InternalRemovalClient || client instanceof InternalEnumerateClient) {
+            resetEnumerateState();
+        }
         if (client != null && client.onError(error, vendorCode)) {
             removeClient(client);
         }
@@ -301,10 +392,20 @@
     }
 
     protected void handleRemoved(long deviceId, int fingerId, int groupId, int remaining) {
+        if (DEBUG) Slog.w(TAG, "Removed: fid=" + fingerId
+                + ", gid=" + groupId
+                + ", dev=" + deviceId
+                + ", rem=" + remaining);
+
         ClientMonitor client = mCurrentClient;
         if (client != null && client.onRemoved(fingerId, groupId, remaining)) {
             removeClient(client);
         }
+        if (client instanceof InternalRemovalClient && !mUnknownFingerprints.isEmpty()) {
+            cleanupUnknownFingerprints();
+        } else if (client instanceof InternalRemovalClient){
+            resetEnumerateState();
+        }
     }
 
     protected void handleAuthenticated(long deviceId, int fingerId, int groupId,
@@ -355,6 +456,7 @@
 
     void handleUserSwitching(int userId) {
         updateActiveGroup(userId, null);
+        doFingerprintCleanup(userId);
     }
 
     private void removeClient(ClientMonitor client) {
@@ -431,7 +533,15 @@
         ClientMonitor currentClient = mCurrentClient;
         if (currentClient != null) {
             if (DEBUG) Slog.v(TAG, "request stop current client " + currentClient.getOwnerString());
-            currentClient.stop(initiatedByClient);
+            if (currentClient instanceof InternalEnumerateClient ||
+                    currentClient instanceof InternalRemovalClient) {
+                // This condition means we're currently running internal diagnostics to
+                // remove extra fingerprints in the hardware and/or the software
+                // TODO: design an escape hatch in case client never finishes
+            }
+            else {
+                currentClient.stop(initiatedByClient);
+            }
             mPendingClient = newClient;
             mHandler.removeCallbacks(mResetClientState);
             mHandler.postDelayed(mResetClientState, CANCEL_TIMEOUT_LIMIT);
@@ -448,47 +558,86 @@
     }
 
     void startRemove(IBinder token, int fingerId, int groupId, int userId,
-            IFingerprintServiceReceiver receiver, boolean restricted) {
+            IFingerprintServiceReceiver receiver, boolean restricted, boolean internal) {
         IBiometricsFingerprint daemon = getFingerprintDaemon();
         if (daemon == null) {
             Slog.w(TAG, "startRemove: no fingerprint HAL!");
             return;
         }
-        RemovalClient client = new RemovalClient(getContext(), mHalDeviceId, token,
-                receiver, fingerId, groupId, userId, restricted, token.toString()) {
-            @Override
-            public void notifyUserActivity() {
-                FingerprintService.this.userActivity();
-            }
 
-            @Override
-            public IBiometricsFingerprint getFingerprintDaemon() {
-                return FingerprintService.this.getFingerprintDaemon();
-            }
-        };
-        startClient(client, true);
+        if (internal) {
+            Context context = getContext();
+            InternalRemovalClient client = new InternalRemovalClient(context, mHalDeviceId,
+                    token, receiver, fingerId, groupId, userId, restricted,
+                    context.getOpPackageName()) {
+                @Override
+                public void notifyUserActivity() {
+
+                }
+                @Override
+                public IBiometricsFingerprint getFingerprintDaemon() {
+                    return FingerprintService.this.getFingerprintDaemon();
+                }
+            };
+            startClient(client, true);
+        }
+        else {
+            RemovalClient client = new RemovalClient(getContext(), mHalDeviceId, token,
+                    receiver, fingerId, groupId, userId, restricted, token.toString()) {
+                @Override
+                public void notifyUserActivity() {
+                    FingerprintService.this.userActivity();
+                }
+
+                @Override
+                public IBiometricsFingerprint getFingerprintDaemon() {
+                    return FingerprintService.this.getFingerprintDaemon();
+                }
+            };
+            startClient(client, true);
+        }
     }
 
     void startEnumerate(IBinder token, int userId,
-        IFingerprintServiceReceiver receiver, boolean restricted) {
+        IFingerprintServiceReceiver receiver, boolean restricted, boolean internal) {
         IBiometricsFingerprint daemon = getFingerprintDaemon();
         if (daemon == null) {
             Slog.w(TAG, "startEnumerate: no fingerprint HAL!");
             return;
         }
-        EnumerateClient client = new EnumerateClient(getContext(), mHalDeviceId, token,
-            receiver, userId, userId, restricted, token.toString()) {
-            @Override
-            public void notifyUserActivity() {
-                FingerprintService.this.userActivity();
-            }
+        if (internal) {
+            List<Fingerprint> enrolledList = getEnrolledFingerprints(userId);
+            Context context = getContext();
+            InternalEnumerateClient client = new InternalEnumerateClient(context, mHalDeviceId,
+                    token, receiver, userId, userId, restricted, context.getOpPackageName(),
+                    enrolledList) {
+                @Override
+                public void notifyUserActivity() {
 
-            @Override
-            public IBiometricsFingerprint getFingerprintDaemon() {
-                return FingerprintService.this.getFingerprintDaemon();
-            }
-        };
-        startClient(client, true);
+                }
+
+                @Override
+                public IBiometricsFingerprint getFingerprintDaemon() {
+                    return FingerprintService.this.getFingerprintDaemon();
+                }
+            };
+            startClient(client, true);
+        }
+        else {
+            EnumerateClient client = new EnumerateClient(getContext(), mHalDeviceId, token,
+                    receiver, userId, userId, restricted, token.toString()) {
+                @Override
+                public void notifyUserActivity() {
+                    FingerprintService.this.userActivity();
+                }
+
+                @Override
+                public IBiometricsFingerprint getFingerprintDaemon() {
+                    return FingerprintService.this.getFingerprintDaemon();
+                }
+            };
+            startClient(client, true);
+        }
     }
 
     public List<Fingerprint> getEnrolledFingerprints(int userId) {
@@ -978,11 +1127,13 @@
             mHandler.post(new Runnable() {
                 @Override
                 public void run() {
-                    startRemove(token, fingerId, groupId, userId, receiver, restricted);
+                    startRemove(token, fingerId, groupId, userId, receiver,
+                            restricted, false /* internal */);
                 }
             });
         }
 
+        @Override // Binder call
         public void enumerate(final IBinder token, final int userId,
             final IFingerprintServiceReceiver receiver) {
             checkPermission(MANAGE_FINGERPRINT); // TODO: Maybe have another permission
@@ -990,7 +1141,7 @@
             mHandler.post(new Runnable() {
                 @Override
                 public void run() {
-                    startEnumerate(token, userId, receiver, restricted);
+                    startEnumerate(token, userId, receiver, restricted, false /* internal */);
                 }
             });
         }
diff --git a/services/core/java/com/android/server/fingerprint/InternalEnumerateClient.java b/services/core/java/com/android/server/fingerprint/InternalEnumerateClient.java
new file mode 100644
index 0000000..f4d2596
--- /dev/null
+++ b/services/core/java/com/android/server/fingerprint/InternalEnumerateClient.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2017 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.server.fingerprint;
+
+import android.content.Context;
+import android.hardware.fingerprint.Fingerprint;
+import android.hardware.fingerprint.IFingerprintServiceReceiver;
+import android.os.IBinder;
+import android.util.Slog;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * An internal class to help clean up unknown fingerprints in the hardware and software
+ */
+public abstract class InternalEnumerateClient extends EnumerateClient {
+
+    private List<Fingerprint> mEnrolledList;
+    private List<Fingerprint> mEnumeratedList = new ArrayList<>(); // list of fp to delete
+
+    public InternalEnumerateClient(Context context, long halDeviceId, IBinder token,
+            IFingerprintServiceReceiver receiver, int groupId, int userId,
+            boolean restricted, String owner, List<Fingerprint> enrolledList) {
+
+        super(context, halDeviceId, token, receiver, userId, groupId, restricted, owner);
+        mEnrolledList = enrolledList;
+    }
+
+    private void handleEnumeratedFingerprint(int fingerId, int groupId, int remaining) {
+
+        boolean matched = false;
+        for (int i=0; i<mEnrolledList.size(); i++) {
+            if (mEnrolledList.get(i).getFingerId() == fingerId) {
+                mEnrolledList.remove(i);
+                matched = true;
+                Slog.e(TAG, "Matched fingerprint fid=" + fingerId);
+                break;
+            }
+        }
+
+        // fingerId 0 means no fingerprints are in hardware
+        if (!matched && fingerId != 0) {
+            Fingerprint fingerprint = new Fingerprint("", groupId, fingerId, getHalDeviceId());
+            mEnumeratedList.add(fingerprint);
+        }
+    }
+
+    private void doFingerprintCleanup() {
+
+        if (mEnrolledList == null) {
+            return;
+        }
+
+        for (Fingerprint f : mEnrolledList) {
+            Slog.e(TAG, "Internal Enumerate: Removing dangling enrolled fingerprint: "
+                    + f.getName() + " " + f.getFingerId() + " " + f.getGroupId()
+                    + " " + f.getDeviceId());
+
+            FingerprintUtils.getInstance().removeFingerprintIdForUser(getContext(),
+                    f.getFingerId(), getTargetUserId());
+        }
+        mEnrolledList.clear();
+    }
+
+    public List<Fingerprint> getEnumeratedList() {
+        return mEnumeratedList;
+    }
+
+    @Override
+    public boolean onEnumerationResult(int fingerId, int groupId, int remaining) {
+
+        handleEnumeratedFingerprint(fingerId, groupId, remaining);
+        if (remaining == 0) {
+            doFingerprintCleanup();
+        }
+
+        return fingerId == 0; // done when id hits 0
+    }
+
+}
diff --git a/services/core/java/com/android/server/fingerprint/InternalRemovalClient.java b/services/core/java/com/android/server/fingerprint/InternalRemovalClient.java
new file mode 100644
index 0000000..19f61fe
--- /dev/null
+++ b/services/core/java/com/android/server/fingerprint/InternalRemovalClient.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2017 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.server.fingerprint;
+
+import android.content.Context;
+import android.os.IBinder;
+import android.hardware.fingerprint.IFingerprintServiceReceiver;
+import com.android.server.fingerprint.RemovalClient;
+
+public abstract class InternalRemovalClient extends RemovalClient {
+
+    public InternalRemovalClient(Context context, long halDeviceId, IBinder token,
+            IFingerprintServiceReceiver receiver, int fingerId, int groupId, int userId,
+            boolean restricted, String owner) {
+
+        super(context, halDeviceId, token, receiver, fingerId, groupId, userId, restricted, owner);
+
+    }
+}
diff --git a/services/core/java/com/android/server/fingerprint/RemovalClient.java b/services/core/java/com/android/server/fingerprint/RemovalClient.java
index ab1b9728..6610634 100644
--- a/services/core/java/com/android/server/fingerprint/RemovalClient.java
+++ b/services/core/java/com/android/server/fingerprint/RemovalClient.java
@@ -59,12 +59,23 @@
 
     @Override
     public int stop(boolean initiatedByClient) {
-        // We don't actually stop remove, but inform the client that the cancel operation succeeded
-        // so we can start the next operation.
-        if (initiatedByClient) {
-            onError(FingerprintManager.FINGERPRINT_ERROR_CANCELED, 0 /* vendorCode */);
+        IBiometricsFingerprint daemon = getFingerprintDaemon();
+        if (daemon == null) {
+            Slog.w(TAG, "stopRemoval: no fingerprint HAL!");
+            return ERROR_ESRCH;
         }
-        return 0;
+        try {
+            final int result = daemon.cancel();
+            if (result != 0) {
+                Slog.w(TAG, "stopRemoval failed, result=" + result);
+                return result;
+            }
+            if (DEBUG) Slog.w(TAG, "client " + getOwnerString() + " is no longer removing");
+        } catch (RemoteException e) {
+            Slog.e(TAG, "stopRemoval failed", e);
+            return ERROR_ESRCH;
+        }
+        return 0; // success
     }
 
     /*
diff --git a/services/core/java/com/android/server/job/JobCompletedListener.java b/services/core/java/com/android/server/job/JobCompletedListener.java
index 655abd7..34ba753 100644
--- a/services/core/java/com/android/server/job/JobCompletedListener.java
+++ b/services/core/java/com/android/server/job/JobCompletedListener.java
@@ -27,5 +27,5 @@
      * Callback for when a job is completed.
      * @param needsReschedule Whether the implementing class should reschedule this job.
      */
-    void onJobCompleted(JobStatus jobStatus, boolean needsReschedule);
+    void onJobCompletedLocked(JobStatus jobStatus, boolean needsReschedule);
 }
diff --git a/services/core/java/com/android/server/job/JobSchedulerService.java b/services/core/java/com/android/server/job/JobSchedulerService.java
index cd3ba4c..a0d0d77 100644
--- a/services/core/java/com/android/server/job/JobSchedulerService.java
+++ b/services/core/java/com/android/server/job/JobSchedulerService.java
@@ -37,6 +37,7 @@
 import android.app.job.JobScheduler;
 import android.app.job.JobService;
 import android.app.job.IJobScheduler;
+import android.app.job.JobWorkItem;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.ContentResolver;
@@ -582,20 +583,8 @@
         mStartedUsers = ArrayUtils.removeInt(mStartedUsers, userHandle);
     }
 
-    /**
-     * Entry point from client to schedule the provided job.
-     * This cancels the job if it's already been scheduled, and replaces it with the one provided.
-     * @param job JobInfo object containing execution parameters
-     * @param uId The package identifier of the application this job is for.
-     * @return Result of this operation. See <code>JobScheduler#RESULT_*</code> return codes.
-     */
-    public int schedule(JobInfo job, int uId) {
-        return scheduleAsPackage(job, uId, null, -1, null);
-    }
-
-    public int scheduleAsPackage(JobInfo job, int uId, String packageName, int userId,
-            String tag) {
-        JobStatus jobStatus = JobStatus.createFromJobInfo(job, uId, packageName, userId, tag);
+    public int scheduleAsPackage(JobInfo job, JobWorkItem work, int uId, String packageName,
+            int userId, String tag) {
         try {
             if (ActivityManager.getService().isAppStartModeDisabled(uId,
                     job.getService().getPackageName())) {
@@ -605,9 +594,20 @@
             }
         } catch (RemoteException e) {
         }
-        if (DEBUG) Slog.d(TAG, "SCHEDULE: " + jobStatus.toShortString());
-        JobStatus toCancel;
         synchronized (mLock) {
+            final JobStatus toCancel = mJobs.getJobByUidAndJobId(uId, job.getId());
+
+            if (work != null && toCancel != null) {
+                // Fast path: we are adding work to an existing job, and the JobInfo is not
+                // changing.  We can just directly enqueue this work in to the job.
+                if (toCancel.getJob().equals(job)) {
+                    toCancel.enqueueWorkLocked(work);
+                    return JobScheduler.RESULT_SUCCESS;
+                }
+            }
+
+            JobStatus jobStatus = JobStatus.createFromJobInfo(job, uId, packageName, userId, tag);
+            if (DEBUG) Slog.d(TAG, "SCHEDULE: " + jobStatus.toShortString());
             // Jobs on behalf of others don't apply to the per-app job cap
             if (ENFORCE_MAX_JOBS && packageName == null) {
                 if (mJobs.countJobsForUid(uId) > MAX_JOBS_PER_APP) {
@@ -618,15 +618,18 @@
             }
 
             // This may throw a SecurityException.
-            jobStatus.prepare(ActivityManager.getService());
+            jobStatus.prepareLocked(ActivityManager.getService());
 
-            toCancel = mJobs.getJobByUidAndJobId(uId, job.getId());
             if (toCancel != null) {
                 cancelJobImpl(toCancel, jobStatus);
             }
-            startTrackingJob(jobStatus, toCancel);
+            if (work != null) {
+                // If work has been supplied, enqueue it into the new job.
+                jobStatus.enqueueWorkLocked(work);
+            }
+            startTrackingJobLocked(jobStatus, toCancel);
+            mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
         }
-        mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
         return JobScheduler.RESULT_SUCCESS;
     }
 
@@ -715,17 +718,17 @@
     }
 
     private void cancelJobImpl(JobStatus cancelled, JobStatus incomingJob) {
-        if (DEBUG) Slog.d(TAG, "CANCEL: " + cancelled.toShortString());
-        cancelled.unprepare(ActivityManager.getService());
-        stopTrackingJob(cancelled, incomingJob, true /* writeBack */);
         synchronized (mLock) {
+            if (DEBUG) Slog.d(TAG, "CANCEL: " + cancelled.toShortString());
+            cancelled.unprepareLocked(ActivityManager.getService());
+            stopTrackingJobLocked(cancelled, incomingJob, true /* writeBack */);
             // Remove from pending queue.
             if (mPendingJobs.remove(cancelled)) {
                 mJobPackageTracker.noteNonpending(cancelled);
             }
             // Cancel if running.
             stopJobOnServiceContextLocked(cancelled, JobParameters.REASON_CANCELED);
-            reportActive();
+            reportActiveLocked();
         }
     }
 
@@ -773,7 +776,7 @@
         }
     }
 
-    void reportActive() {
+    void reportActiveLocked() {
         // active is true if pending queue contains jobs OR some job is running.
         boolean active = mPendingJobs.size() > 0;
         if (mPendingJobs.size() <= 0) {
@@ -895,20 +898,18 @@
      * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know
      * about.
      */
-    private void startTrackingJob(JobStatus jobStatus, JobStatus lastJob) {
-        synchronized (mLock) {
-            if (!jobStatus.isPrepared()) {
-                Slog.wtf(TAG, "Not yet prepared when started tracking: " + jobStatus);
-            }
-            final boolean update = mJobs.add(jobStatus);
-            if (mReadyToRock) {
-                for (int i = 0; i < mControllers.size(); i++) {
-                    StateController controller = mControllers.get(i);
-                    if (update) {
-                        controller.maybeStopTrackingJobLocked(jobStatus, null, true);
-                    }
-                    controller.maybeStartTrackingJobLocked(jobStatus, lastJob);
+    private void startTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob) {
+        if (!jobStatus.isPreparedLocked()) {
+            Slog.wtf(TAG, "Not yet prepared when started tracking: " + jobStatus);
+        }
+        final boolean update = mJobs.add(jobStatus);
+        if (mReadyToRock) {
+            for (int i = 0; i < mControllers.size(); i++) {
+                StateController controller = mControllers.get(i);
+                if (update) {
+                    controller.maybeStopTrackingJobLocked(jobStatus, null, true);
                 }
+                controller.maybeStartTrackingJobLocked(jobStatus, lastJob);
             }
         }
     }
@@ -917,19 +918,20 @@
      * Called when we want to remove a JobStatus object that we've finished executing. Returns the
      * object removed.
      */
-    private boolean stopTrackingJob(JobStatus jobStatus, JobStatus incomingJob,
+    private boolean stopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob,
             boolean writeBack) {
-        synchronized (mLock) {
-            // Remove from store as well as controllers.
-            final boolean removed = mJobs.remove(jobStatus, writeBack);
-            if (removed && mReadyToRock) {
-                for (int i=0; i<mControllers.size(); i++) {
-                    StateController controller = mControllers.get(i);
-                    controller.maybeStopTrackingJobLocked(jobStatus, incomingJob, false);
-                }
+        // Deal with any remaining work items in the old job.
+        jobStatus.stopTrackingJobLocked(incomingJob);
+
+        // Remove from store as well as controllers.
+        final boolean removed = mJobs.remove(jobStatus, writeBack);
+        if (removed && mReadyToRock) {
+            for (int i=0; i<mControllers.size(); i++) {
+                StateController controller = mControllers.get(i);
+                controller.maybeStopTrackingJobLocked(jobStatus, incomingJob, false);
             }
-            return removed;
         }
+        return removed;
     }
 
     private boolean stopJobOnServiceContextLocked(JobStatus job, int reason) {
@@ -990,7 +992,7 @@
      *
      * @see JobHandler#maybeQueueReadyJobsForExecutionLockedH
      */
-    private JobStatus getRescheduleJobForFailure(JobStatus failureToReschedule) {
+    private JobStatus getRescheduleJobForFailureLocked(JobStatus failureToReschedule) {
         final long elapsedNowMillis = SystemClock.elapsedRealtime();
         final JobInfo job = failureToReschedule.getJob();
 
@@ -1017,7 +1019,7 @@
                 JobStatus.NO_LATEST_RUNTIME, backoffAttempts);
         for (int ic=0; ic<mControllers.size(); ic++) {
             StateController controller = mControllers.get(ic);
-            controller.rescheduleForFailure(newJob, failureToReschedule);
+            controller.rescheduleForFailureLocked(newJob, failureToReschedule);
         }
         return newJob;
     }
@@ -1065,13 +1067,13 @@
      * @param needsReschedule Whether the implementing class should reschedule this job.
      */
     @Override
-    public void onJobCompleted(JobStatus jobStatus, boolean needsReschedule) {
+    public void onJobCompletedLocked(JobStatus jobStatus, boolean needsReschedule) {
         if (DEBUG) {
             Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule);
         }
         // Do not write back immediately if this is a periodic job. The job may get lost if system
         // shuts down before it is added back.
-        if (!stopTrackingJob(jobStatus, null, !jobStatus.getJob().isPeriodic())) {
+        if (!stopTrackingJobLocked(jobStatus, null, !jobStatus.getJob().isPeriodic())) {
             if (DEBUG) {
                 Slog.d(TAG, "Could not find job to remove. Was job removed while executing?");
             }
@@ -1085,24 +1087,24 @@
         // the old job after scheduling the new one, but since we have no lock held here
         // that may cause ordering problems if the app removes jobStatus while in here.
         if (needsReschedule) {
-            JobStatus rescheduled = getRescheduleJobForFailure(jobStatus);
+            JobStatus rescheduled = getRescheduleJobForFailureLocked(jobStatus);
             try {
-                rescheduled.prepare(ActivityManager.getService());
+                rescheduled.prepareLocked(ActivityManager.getService());
             } catch (SecurityException e) {
                 Slog.w(TAG, "Unable to regrant job permissions for " + rescheduled);
             }
-            startTrackingJob(rescheduled, jobStatus);
+            startTrackingJobLocked(rescheduled, jobStatus);
         } else if (jobStatus.getJob().isPeriodic()) {
             JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus);
             try {
-                rescheduledPeriodic.prepare(ActivityManager.getService());
+                rescheduledPeriodic.prepareLocked(ActivityManager.getService());
             } catch (SecurityException e) {
                 Slog.w(TAG, "Unable to regrant job permissions for " + rescheduledPeriodic);
             }
-            startTrackingJob(rescheduledPeriodic, jobStatus);
+            startTrackingJobLocked(rescheduledPeriodic, jobStatus);
         }
-        jobStatus.unprepare(ActivityManager.getService());
-        reportActive();
+        jobStatus.unprepareLocked(ActivityManager.getService());
+        reportActiveLocked();
         mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget();
     }
 
@@ -1410,7 +1412,7 @@
                     Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs.");
                 }
                 assignJobsToContextsLocked();
-                reportActive();
+                reportActiveLocked();
             }
         }
     }
@@ -1698,7 +1700,37 @@
 
             long ident = Binder.clearCallingIdentity();
             try {
-                return JobSchedulerService.this.schedule(job, uid);
+                return JobSchedulerService.this.scheduleAsPackage(job, null, uid, null, -1, null);
+            } finally {
+                Binder.restoreCallingIdentity(ident);
+            }
+        }
+
+        // IJobScheduler implementation
+        @Override
+        public int enqueue(JobInfo job, JobWorkItem work) throws RemoteException {
+            if (DEBUG) {
+                Slog.d(TAG, "Enqueueing job: " + job.toString() + " work: " + work);
+            }
+            final int pid = Binder.getCallingPid();
+            final int uid = Binder.getCallingUid();
+
+            enforceValidJobRequest(uid, job);
+            if (job.isPersisted()) {
+                throw new IllegalArgumentException("Can't enqueue work for persisted jobs");
+            }
+            if (work == null) {
+                throw new NullPointerException("work is null");
+            }
+
+            if ((job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0) {
+                getContext().enforceCallingOrSelfPermission(
+                        android.Manifest.permission.CONNECTIVITY_INTERNAL, TAG);
+            }
+
+            long ident = Binder.clearCallingIdentity();
+            try {
+                return JobSchedulerService.this.scheduleAsPackage(job, work, uid, null, -1, null);
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }
@@ -1731,7 +1763,7 @@
 
             long ident = Binder.clearCallingIdentity();
             try {
-                return JobSchedulerService.this.scheduleAsPackage(job, callerUid,
+                return JobSchedulerService.this.scheduleAsPackage(job, null, callerUid,
                         packageName, userId, tag);
             } finally {
                 Binder.restoreCallingIdentity(ident);
diff --git a/services/core/java/com/android/server/job/JobServiceContext.java b/services/core/java/com/android/server/job/JobServiceContext.java
index 728ed72d..c7ef0e2 100644
--- a/services/core/java/com/android/server/job/JobServiceContext.java
+++ b/services/core/java/com/android/server/job/JobServiceContext.java
@@ -21,11 +21,11 @@
 import android.app.job.JobParameters;
 import android.app.job.IJobCallback;
 import android.app.job.IJobService;
+import android.app.job.JobWorkItem;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.ServiceConnection;
-import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.os.Binder;
 import android.os.Handler;
@@ -195,7 +195,7 @@
             mExecutionStartTimeElapsed = SystemClock.elapsedRealtime();
 
             mVerb = VERB_BINDING;
-            scheduleOpTimeOut();
+            scheduleOpTimeOutLocked();
             final Intent intent = new Intent().setComponent(job.getServiceComponent());
             boolean binding = mContext.bindServiceAsUser(intent, this,
                     Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND,
@@ -208,7 +208,7 @@
                 mParams = null;
                 mExecutionStartTimeElapsed = 0L;
                 mVerb = VERB_FINISHED;
-                removeOpTimeOut();
+                removeOpTimeOutLocked();
                 return false;
             }
             try {
@@ -297,6 +297,38 @@
         mCallbackHandler.obtainMessage(MSG_CALLBACK, jobId, ongoing ? 1 : 0).sendToTarget();
     }
 
+    @Override
+    public JobWorkItem dequeueWork(int jobId) {
+        if (!verifyCallingUid()) {
+            throw new SecurityException("Bad calling uid: " + Binder.getCallingUid());
+        }
+        JobWorkItem work = null;
+        boolean stillWorking = false;
+        synchronized (mLock) {
+            if (mRunningJob != null) {
+                work = mRunningJob.dequeueWorkLocked();
+                stillWorking = mRunningJob.hasExecutingWorkLocked();
+            }
+        }
+        if (work == null && !stillWorking) {
+            jobFinished(jobId, false);
+        }
+        return work;
+    }
+
+    @Override
+    public boolean completeWork(int jobId, int workId) {
+        if (!verifyCallingUid()) {
+            throw new SecurityException("Bad calling uid: " + Binder.getCallingUid());
+        }
+        synchronized (mLock) {
+            if (mRunningJob != null) {
+                return mRunningJob.completeWorkLocked(workId);
+            }
+            return false;
+        }
+    }
+
     /**
      * We acquire/release a wakelock on onServiceConnected/unbindService. This mirrors the work
      * we intend to send to the client - we stop sending work when the service is unbound so until
@@ -378,46 +410,18 @@
         public void handleMessage(Message message) {
             switch (message.what) {
                 case MSG_SERVICE_BOUND:
-                    removeOpTimeOut();
-                    handleServiceBoundH();
+                    doServiceBound();
                     break;
                 case MSG_CALLBACK:
-                    if (DEBUG) {
-                        Slog.d(TAG, "MSG_CALLBACK of : " + mRunningJob
-                                + " v:" + VERB_STRINGS[mVerb]);
-                    }
-                    removeOpTimeOut();
-
-                    if (mVerb == VERB_STARTING) {
-                        final boolean workOngoing = message.arg2 == 1;
-                        handleStartedH(workOngoing);
-                    } else if (mVerb == VERB_EXECUTING ||
-                            mVerb == VERB_STOPPING) {
-                        final boolean reschedule = message.arg2 == 1;
-                        handleFinishedH(reschedule);
-                    } else {
-                        if (DEBUG) {
-                            Slog.d(TAG, "Unrecognised callback: " + mRunningJob);
-                        }
-                    }
+                    doCallback(message.arg2);
                     break;
                 case MSG_CANCEL:
-                    if (mVerb == VERB_FINISHED) {
-                        if (DEBUG) {
-                            Slog.d(TAG,
-                                   "Trying to process cancel for torn-down context, ignoring.");
-                        }
-                        return;
-                    }
-                    mParams.setStopReason(message.arg1);
-                    if (message.arg1 == JobParameters.REASON_PREEMPT) {
-                        mPreferredUid = mRunningJob != null ? mRunningJob.getUid() :
-                                NO_PREFERRED_UID;
-                    }
-                    handleCancelH();
+                    doCancel(message.arg1);
                     break;
                 case MSG_TIMEOUT:
-                    handleOpTimeoutH();
+                    synchronized (mLock) {
+                        handleOpTimeoutH();
+                    }
                     break;
                 case MSG_SHUTDOWN_EXECUTION:
                     closeAndCleanupJobH(true /* needsReschedule */);
@@ -427,6 +431,55 @@
             }
         }
 
+        void doServiceBound() {
+            synchronized (mLock) {
+                removeOpTimeOutLocked();
+                handleServiceBoundH();
+            }
+        }
+
+        void doCallback(int arg2) {
+            synchronized (mLock) {
+                if (DEBUG) {
+                    Slog.d(TAG, "MSG_CALLBACK of : " + mRunningJob
+                            + " v:" + VERB_STRINGS[mVerb]);
+                }
+                removeOpTimeOutLocked();
+
+                if (mVerb == VERB_STARTING) {
+                    final boolean workOngoing = arg2 == 1;
+                    handleStartedH(workOngoing);
+                } else if (mVerb == VERB_EXECUTING ||
+                        mVerb == VERB_STOPPING) {
+                    final boolean reschedule = arg2 == 1;
+                    handleFinishedH(reschedule);
+                } else {
+                    if (DEBUG) {
+                        Slog.d(TAG, "Unrecognised callback: " + mRunningJob);
+                    }
+                }
+            }
+        }
+
+        void doCancel(int arg1) {
+            synchronized (mLock) {
+                if (mVerb == VERB_FINISHED) {
+                    if (DEBUG) {
+                        Slog.d(TAG,
+                                "Trying to process cancel for torn-down context, ignoring.");
+                    }
+                    return;
+                }
+                mParams.setStopReason(arg1);
+                if (arg1 == JobParameters.REASON_PREEMPT) {
+                    mPreferredUid = mRunningJob != null ? mRunningJob.getUid() :
+                            NO_PREFERRED_UID;
+                }
+                handleCancelH();
+            }
+
+        }
+
         /** Start the job on the service. */
         private void handleServiceBoundH() {
             if (DEBUG) {
@@ -448,7 +501,7 @@
             }
             try {
                 mVerb = VERB_STARTING;
-                scheduleOpTimeOut();
+                scheduleOpTimeOutLocked();
                 service.startJob(mParams);
             } catch (Exception e) {
                 // We catch 'Exception' because client-app malice or bugs might induce a wide
@@ -483,7 +536,7 @@
                         handleCancelH();
                         return;
                     }
-                    scheduleOpTimeOut();
+                    scheduleOpTimeOutLocked();
                     break;
                 default:
                     Slog.e(TAG, "Handling started job but job wasn't starting! Was "
@@ -587,7 +640,7 @@
          * VERB_STOPPING.
          */
         private void sendStopMessageH() {
-            removeOpTimeOut();
+            removeOpTimeOutLocked();
             if (mVerb != VERB_EXECUTING) {
                 Slog.e(TAG, "Sending onStopJob for a job that isn't started. " + mRunningJob);
                 closeAndCleanupJobH(false /* reschedule */);
@@ -595,7 +648,7 @@
             }
             try {
                 mVerb = VERB_STOPPING;
-                scheduleOpTimeOut();
+                scheduleOpTimeOutLocked();
                 service.stopJob(mParams);
             } catch (RemoteException e) {
                 Slog.e(TAG, "Error sending onStopJob to client.", e);
@@ -635,13 +688,13 @@
                 mCancelled.set(false);
                 service = null;
                 mAvailable = true;
+                removeOpTimeOutLocked();
+                removeMessages(MSG_CALLBACK);
+                removeMessages(MSG_SERVICE_BOUND);
+                removeMessages(MSG_CANCEL);
+                removeMessages(MSG_SHUTDOWN_EXECUTION);
+                mCompletedListener.onJobCompletedLocked(completedJob, reschedule);
             }
-            removeOpTimeOut();
-            removeMessages(MSG_CALLBACK);
-            removeMessages(MSG_SERVICE_BOUND);
-            removeMessages(MSG_CANCEL);
-            removeMessages(MSG_SHUTDOWN_EXECUTION);
-            mCompletedListener.onJobCompleted(completedJob, reschedule);
         }
     }
 
@@ -650,8 +703,8 @@
      * we haven't received a response in a certain amount of time, we want to give up and carry
      * on with life.
      */
-    private void scheduleOpTimeOut() {
-        removeOpTimeOut();
+    private void scheduleOpTimeOutLocked() {
+        removeOpTimeOutLocked();
 
         final long timeoutMillis;
         switch (mVerb) {
@@ -678,7 +731,7 @@
     }
 
 
-    private void removeOpTimeOut() {
+    private void removeOpTimeOutLocked() {
         mCallbackHandler.removeMessages(MSG_TIMEOUT);
     }
 }
diff --git a/services/core/java/com/android/server/job/JobStore.java b/services/core/java/com/android/server/job/JobStore.java
index c0264df..fcc0827 100644
--- a/services/core/java/com/android/server/job/JobStore.java
+++ b/services/core/java/com/android/server/job/JobStore.java
@@ -454,7 +454,7 @@
                         IActivityManager am = ActivityManager.getService();
                         for (int i=0; i<jobs.size(); i++) {
                             JobStatus js = jobs.get(i);
-                            js.prepare(am);
+                            js.prepareLocked(am);
                             this.jobSet.add(js);
                         }
                     }
diff --git a/services/core/java/com/android/server/job/controllers/ContentObserverController.java b/services/core/java/com/android/server/job/controllers/ContentObserverController.java
index 5d209fc..29f0e2c 100644
--- a/services/core/java/com/android/server/job/controllers/ContentObserverController.java
+++ b/services/core/java/com/android/server/job/controllers/ContentObserverController.java
@@ -164,7 +164,7 @@
                             && taskStatus.contentObserverJobInstance.mChangedAuthorities != null) {
                         // We are stopping this job, but it is going to be replaced by this given
                         // incoming job.  We want to propagate our state over to it, so we don't
-                        // lose any content changes that had happend since the last one started.
+                        // lose any content changes that had happened since the last one started.
                         // If there is a previous job associated with the new job, propagate over
                         // any pending content URI trigger reports.
                         if (incomingJob.contentObserverJobInstance == null) {
@@ -195,16 +195,14 @@
     }
 
     @Override
-    public void rescheduleForFailure(JobStatus newJob, JobStatus failureToReschedule) {
+    public void rescheduleForFailureLocked(JobStatus newJob, JobStatus failureToReschedule) {
         if (failureToReschedule.hasContentTriggerConstraint()
                 && newJob.hasContentTriggerConstraint()) {
-            synchronized (mLock) {
-                // Our job has failed, and we are scheduling a new job for it.
-                // Copy the last reported content changes in to the new job, so when
-                // we schedule the new one we will pick them up and report them again.
-                newJob.changedAuthorities = failureToReschedule.changedAuthorities;
-                newJob.changedUris = failureToReschedule.changedUris;
-            }
+            // Our job has failed, and we are scheduling a new job for it.
+            // Copy the last reported content changes in to the new job, so when
+            // we schedule the new one we will pick them up and report them again.
+            newJob.changedAuthorities = failureToReschedule.changedAuthorities;
+            newJob.changedUris = failureToReschedule.changedUris;
         }
     }
 
diff --git a/services/core/java/com/android/server/job/controllers/JobStatus.java b/services/core/java/com/android/server/job/controllers/JobStatus.java
index d27d0e5..4cdce5f 100644
--- a/services/core/java/com/android/server/job/controllers/JobStatus.java
+++ b/services/core/java/com/android/server/job/controllers/JobStatus.java
@@ -19,6 +19,7 @@
 import android.app.AppGlobals;
 import android.app.IActivityManager;
 import android.app.job.JobInfo;
+import android.app.job.JobWorkItem;
 import android.content.ClipData;
 import android.content.ComponentName;
 import android.content.ContentProvider;
@@ -35,6 +36,7 @@
 import android.util.TimeUtils;
 
 import java.io.PrintWriter;
+import java.util.ArrayList;
 import java.util.Arrays;
 
 /**
@@ -126,6 +128,14 @@
 
     public int lastEvaluatedPriority;
 
+    // If non-null, this is work that has been enqueued for the job.
+    public ArrayList<JobWorkItem> pendingWork;
+
+    // If non-null, this is work that is currently being executed.
+    public ArrayList<JobWorkItem> executingWork;
+
+    public int nextPendingWorkId = 1;
+
     // Used by shell commands
     public int overrideState = 0;
 
@@ -256,7 +266,59 @@
                 earliestRunTimeElapsedMillis, latestRunTimeElapsedMillis);
     }
 
-    public void prepare(IActivityManager am) {
+    public void enqueueWorkLocked(JobWorkItem work) {
+        if (pendingWork == null) {
+            pendingWork = new ArrayList<>();
+        }
+        work.setWorkId(nextPendingWorkId);
+        nextPendingWorkId++;
+        pendingWork.add(work);
+    }
+
+    public JobWorkItem dequeueWorkLocked() {
+        if (pendingWork != null && pendingWork.size() > 0) {
+            JobWorkItem work = pendingWork.remove(0);
+            if (work != null) {
+                if (executingWork == null) {
+                    executingWork = new ArrayList<>();
+                }
+                executingWork.add(work);
+            }
+            return work;
+        }
+        return null;
+    }
+
+    public boolean hasExecutingWorkLocked() {
+        return executingWork != null && executingWork.size() > 0;
+    }
+
+    public boolean completeWorkLocked(int workId) {
+        if (executingWork != null) {
+            final int N = executingWork.size();
+            for (int i = 0; i < N; i++) {
+                if (executingWork.get(i).getWorkId() == workId) {
+                    executingWork.remove(i);
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    public void stopTrackingJobLocked(JobStatus incomingJob) {
+        if (incomingJob != null) {
+            // We are replacing with a new job -- transfer the work!
+            incomingJob.pendingWork = pendingWork;
+            pendingWork = null;
+            incomingJob.nextPendingWorkId = nextPendingWorkId;
+        } else {
+            // We are completely stopping the job...  need to clean up work.
+            // XXX remove perms when that is impl.
+        }
+    }
+
+    public void prepareLocked(IActivityManager am) {
         if (prepared) {
             Slog.wtf(TAG, "Already prepared: " + this);
             return;
@@ -271,7 +333,7 @@
         }
     }
 
-    public void unprepare(IActivityManager am) {
+    public void unprepareLocked(IActivityManager am) {
         if (!prepared) {
             Slog.wtf(TAG, "Hasn't been prepared: " + this);
             return;
@@ -288,7 +350,7 @@
         }
     }
 
-    public boolean isPrepared() {
+    public boolean isPreparedLocked() {
         return prepared;
     }
 
@@ -854,6 +916,22 @@
                 }
             }
         }
+        if (pendingWork != null && pendingWork.size() > 0) {
+            pw.print(prefix); pw.println("Pending work:");
+            for (int i = 0; i < pendingWork.size(); i++) {
+                JobWorkItem work = pendingWork.get(i);
+                pw.print(prefix); pw.print("  #"); pw.print(i); pw.print(": #");
+                pw.print(work.getWorkId()); pw.print(" "); pw.println(work.getIntent());
+            }
+        }
+        if (executingWork != null && executingWork.size() > 0) {
+            pw.print(prefix); pw.println("Executing work:");
+            for (int i = 0; i < executingWork.size(); i++) {
+                JobWorkItem work = executingWork.get(i);
+                pw.print(prefix); pw.print("  #"); pw.print(i); pw.print(": #");
+                pw.print(work.getWorkId()); pw.print(" "); pw.println(work.getIntent());
+            }
+        }
         pw.print(prefix); pw.print("Earliest run time: ");
         pw.println(formatRunTime(earliestRunTimeElapsedMillis, NO_EARLIEST_RUNTIME));
         pw.print(prefix); pw.print("Latest run time: ");
diff --git a/services/core/java/com/android/server/job/controllers/StateController.java b/services/core/java/com/android/server/job/controllers/StateController.java
index 1721fb9..497faab 100644
--- a/services/core/java/com/android/server/job/controllers/StateController.java
+++ b/services/core/java/com/android/server/job/controllers/StateController.java
@@ -61,7 +61,7 @@
     /**
      * Called when a new job is being created to reschedule an old failed job.
      */
-    public void rescheduleForFailure(JobStatus newJob, JobStatus failureToReschedule) {
+    public void rescheduleForFailureLocked(JobStatus newJob, JobStatus failureToReschedule) {
     }
 
     public abstract void dumpControllerStateLocked(PrintWriter pw, int filterUid);
diff --git a/services/core/java/com/android/server/pm/UserRestrictionsUtils.java b/services/core/java/com/android/server/pm/UserRestrictionsUtils.java
index 84381fe..c6667a7 100644
--- a/services/core/java/com/android/server/pm/UserRestrictionsUtils.java
+++ b/services/core/java/com/android/server/pm/UserRestrictionsUtils.java
@@ -74,6 +74,7 @@
             UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
             UserManager.DISALLOW_CONFIG_BLUETOOTH,
             UserManager.DISALLOW_BLUETOOTH,
+            UserManager.DISALLOW_BLUETOOTH_SHARING,
             UserManager.DISALLOW_USB_FILE_TRANSFER,
             UserManager.DISALLOW_CONFIG_CREDENTIALS,
             UserManager.DISALLOW_REMOVE_USER,
@@ -155,6 +156,7 @@
      */
     private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
             UserManager.DISALLOW_ADJUST_VOLUME,
+            UserManager.DISALLOW_BLUETOOTH_SHARING,
             UserManager.DISALLOW_RUN_IN_BACKGROUND,
             UserManager.DISALLOW_UNMUTE_MICROPHONE,
             UserManager.DISALLOW_UNMUTE_DEVICE
@@ -167,6 +169,17 @@
             UserManager.DISALLOW_ADD_MANAGED_PROFILE
     );
 
+    /**
+     * User restrictions that default to {@code true} for managed profile owners.
+     *
+     * NB: {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is also set by default but it is
+     * not set to existing profile owners unless they used to have INSTALL_NON_MARKET_APPS disabled
+     * in settings. So it is handled separately.
+     */
+    private static final Set<String> DEFAULT_ENABLED_FOR_MANAGED_PROFILES = Sets.newArraySet(
+            UserManager.DISALLOW_BLUETOOTH_SHARING
+    );
+
     /*
      * Special user restrictions that are always applied to all users no matter who sets them.
      */
@@ -308,6 +321,13 @@
     }
 
     /**
+     * Returns the user restrictions that default to {@code true} for managed profile owners.
+     */
+    public static @NonNull Set<String> getDefaultEnabledForManagedProfiles() {
+        return DEFAULT_ENABLED_FOR_MANAGED_PROFILES;
+    }
+
+    /**
      * Takes restrictions that can be set by device owner, and sort them into what should be applied
      * globally and what should be applied only on the current user.
      */
@@ -544,8 +564,8 @@
     public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions,
             SparseArray<Bundle> destRestrictions) {
         for (int i = 0; i < srcRestrictions.size(); i++) {
-            int key = srcRestrictions.keyAt(i);
-            Bundle from = srcRestrictions.valueAt(i);
+            final int key = srcRestrictions.keyAt(i);
+            final Bundle from = srcRestrictions.valueAt(i);
             if (contains(from, restrictionKey)) {
                 from.remove(restrictionKey);
                 Bundle to = destRestrictions.get(key);
@@ -562,4 +582,24 @@
             }
         }
     }
+
+    /**
+     * Returns whether restrictions differ between two bundles.
+     * @param oldRestrictions old bundle of restrictions.
+     * @param newRestrictions new bundle of restrictions
+     * @param restrictions restrictions of interest, if empty, all restrictions are checked.
+     */
+    public static boolean restrictionsChanged(Bundle oldRestrictions, Bundle newRestrictions,
+            String... restrictions) {
+        if (restrictions.length == 0) {
+            return areEqual(oldRestrictions, newRestrictions);
+        }
+        for (final String restriction : restrictions) {
+            if (oldRestrictions.getBoolean(restriction, false) !=
+                    newRestrictions.getBoolean(restriction, false)) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
diff --git a/services/core/java/com/android/server/radio/RadioService.java b/services/core/java/com/android/server/radio/RadioService.java
new file mode 100644
index 0000000..327e98f
--- /dev/null
+++ b/services/core/java/com/android/server/radio/RadioService.java
@@ -0,0 +1,56 @@
+/**
+ * Copyright (C) 2017 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.server.radio;
+
+import android.content.Context;
+import android.hardware.radio.IRadioService;
+import android.hardware.radio.ITuner;
+import android.hardware.radio.RadioManager;
+import android.util.Slog;
+
+import com.android.server.SystemService;
+
+public class RadioService extends SystemService {
+    // TODO(b/36863239): rename to RadioService when native service goes away
+    private static final String TAG = "RadioServiceJava";
+
+    public RadioService(Context context) {
+        super(context);
+    }
+
+    @Override
+    public void onStart() {
+        publishBinderService(Context.RADIO_SERVICE, new RadioServiceImpl());
+        Slog.v(TAG, "RadioService started");
+    }
+
+    private static class RadioServiceImpl extends IRadioService.Stub {
+        @Override
+        public ITuner openTuner() {
+            Slog.d(TAG, "openTuner()");
+            return new TunerImpl();
+        }
+    }
+
+    private static class TunerImpl extends ITuner.Stub {
+        @Override
+        public int getProgramInformation(RadioManager.ProgramInfo[] infoOut) {
+            Slog.d(TAG, "getProgramInformation()");
+            return RadioManager.STATUS_INVALID_OPERATION;
+        }
+    }
+}
diff --git a/services/core/java/com/android/server/search/SearchManagerService.java b/services/core/java/com/android/server/search/SearchManagerService.java
index 8c31731..8969771 100644
--- a/services/core/java/com/android/server/search/SearchManagerService.java
+++ b/services/core/java/com/android/server/search/SearchManagerService.java
@@ -18,7 +18,6 @@
 
 import android.app.ActivityManager;
 import android.app.AppGlobals;
-import android.app.AppOpsManager;
 import android.app.IActivityManager;
 import android.app.ISearchManager;
 import android.app.SearchManager;
@@ -29,7 +28,6 @@
 import android.content.Intent;
 import android.content.pm.IPackageManager;
 import android.content.pm.PackageManager;
-import android.content.pm.ParceledListSlice;
 import android.content.pm.ResolveInfo;
 import android.database.ContentObserver;
 import android.os.Binder;
@@ -39,8 +37,6 @@
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.provider.Settings;
-import android.speech.RecognitionService;
-import android.text.TextUtils;
 import android.util.Log;
 import android.util.SparseArray;
 
@@ -276,52 +272,6 @@
         }
     }
 
-    private boolean isDefaultRecognizerPackage(String packageName) {
-        ResolveInfo resolveInfo = mContext.getPackageManager().resolveService(
-                new Intent(RecognitionService.SERVICE_INTERFACE),
-                PackageManager.GET_META_DATA);
-        if (resolveInfo == null || resolveInfo.serviceInfo == null) {
-            Log.w(TAG, "Unable to resolve default voice recognition service.");
-            return false;
-        }
-        if (!TextUtils.isEmpty(packageName) && TextUtils.equals(packageName,
-                resolveInfo.serviceInfo.packageName)) {
-            return true;
-        }
-        return false;
-    }
-
-    private ComponentName getLegacyAssistReceiverComponent(int userHandle) {
-        try {
-            userHandle = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
-                    Binder.getCallingUid(), userHandle, true, false,
-                    "getLegacyAssistReceiverComponent", null);
-            IPackageManager pm = AppGlobals.getPackageManager();
-            Intent assistIntent = new Intent(Intent.ACTION_ASSIST);
-            ParceledListSlice<ResolveInfo> infoParceledList =
-                    pm.queryIntentReceivers(assistIntent,
-                            assistIntent.resolveTypeIfNeeded(mContext.getContentResolver()),
-                            PackageManager.MATCH_DEFAULT_ONLY, userHandle);
-            if (infoParceledList != null) {
-                List<ResolveInfo> infoList = infoParceledList.getList();
-                if (infoList != null && infoList.size() > 0) {
-                    if (isDefaultRecognizerPackage(
-                            infoList.get(0).activityInfo.applicationInfo.packageName)) {
-                        return new ComponentName(
-                                infoList.get(0).activityInfo.applicationInfo.packageName,
-                                infoList.get(0).activityInfo.name);
-                    }
-                }
-            }
-        } catch (RemoteException re) {
-            // Local call
-            Log.e(TAG, "RemoteException in getLegacyAssistReceiverComponent: " + re);
-        } catch (Exception e) {
-            Log.e(TAG, "Exception in getLegacyAssistReceiverComponent: " + e);
-        }
-        return null;
-    }
-
     private ComponentName getLegacyAssistComponent(int userHandle) {
         try {
             userHandle = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
@@ -348,7 +298,7 @@
 
     @Override
     public boolean launchLegacyAssist(String hint, int userHandle, Bundle args) {
-        ComponentName comp = getLegacyAssistReceiverComponent(userHandle);
+        ComponentName comp = getLegacyAssistComponent(userHandle);
         if (comp == null) {
             return false;
         }
@@ -356,13 +306,9 @@
         try {
             Intent intent = new Intent(Intent.ACTION_ASSIST);
             intent.setComponent(comp);
-            if (args != null) {
-                intent.putExtras(args);
-            }
             IActivityManager am = ActivityManager.getService();
-            return am.broadcastIntent(null, intent, null, null, 0, null, null, null,
-                    AppOpsManager.OP_NONE, null, false, false,
-                    userHandle) == ActivityManager.BROADCAST_SUCCESS;
+            return am.launchAssistIntent(intent, ActivityManager.ASSIST_CONTEXT_BASIC, hint,
+                    userHandle, args);
         } catch (RemoteException e) {
         } finally {
             Binder.restoreCallingIdentity(ident);
diff --git a/services/core/java/com/android/server/storage/DiskStatsLoggingService.java b/services/core/java/com/android/server/storage/DiskStatsLoggingService.java
index 4035ade..1783dcc 100644
--- a/services/core/java/com/android/server/storage/DiskStatsLoggingService.java
+++ b/services/core/java/com/android/server/storage/DiskStatsLoggingService.java
@@ -73,7 +73,6 @@
         final int userId = UserHandle.myUserId();
         UserEnvironment environment = new UserEnvironment(userId);
         LogRunnable task = new LogRunnable();
-        task.setRootDirectory(environment.getExternalStorageDirectory());
         task.setDownloadsDirectory(
                 environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
         task.setSystemSize(FileCollector.getSystemSize(this));
@@ -127,14 +126,10 @@
         private JobParameters mParams;
         private AppCollector mCollector;
         private File mOutputFile;
-        private File mRootDirectory;
         private File mDownloadsDirectory;
+        private Context mContext;
         private long mSystemSize;
 
-        public void setRootDirectory(File file) {
-            mRootDirectory = file;
-        }
-
         public void setDownloadsDirectory(File file) {
             mDownloadsDirectory = file;
         }
@@ -151,14 +146,25 @@
             mSystemSize = size;
         }
 
+        public void setContext(Context context) {
+            mContext = context;
+        }
+
         public void setJobService(JobService jobService, JobParameters params) {
             mJobService = jobService;
             mParams = params;
         }
 
         public void run() {
-            FileCollector.MeasurementResult mainCategories =
-                    FileCollector.getMeasurementResult(mRootDirectory);
+            FileCollector.MeasurementResult mainCategories;
+            try {
+                mainCategories = FileCollector.getMeasurementResult(mContext);
+            } catch (IllegalStateException e) {
+                // This can occur if installd has an issue.
+                Log.e(TAG, "Error while measuring storage", e);
+                finishJob(true);
+                return;
+            }
             FileCollector.MeasurementResult downloads =
                     FileCollector.getMeasurementResult(mDownloadsDirectory);
 
@@ -168,12 +174,10 @@
                 needsReschedule = false;
                 logToFile(mainCategories, downloads, stats, mSystemSize);
             } else {
-                Log.w("TAG", "Timed out while fetching package stats.");
+                Log.w(TAG, "Timed out while fetching package stats.");
             }
 
-            if (mJobService != null) {
-                mJobService.jobFinished(mParams, needsReschedule);
-            }
+            finishJob(needsReschedule);
         }
 
         private void logToFile(MeasurementResult mainCategories, MeasurementResult downloads,
@@ -187,5 +191,11 @@
                 Log.e(TAG, "Exception while writing opportunistic disk file cache.", e);
             }
         }
+
+        private void finishJob(boolean needsReschedule) {
+            if (mJobService != null) {
+                mJobService.jobFinished(mParams, needsReschedule);
+            }
+        }
     }
 }
\ No newline at end of file
diff --git a/services/core/java/com/android/server/storage/FileCollector.java b/services/core/java/com/android/server/storage/FileCollector.java
index 90f9f139..04f25a4 100644
--- a/services/core/java/com/android/server/storage/FileCollector.java
+++ b/services/core/java/com/android/server/storage/FileCollector.java
@@ -17,8 +17,11 @@
 package com.android.server.storage;
 
 import android.annotation.IntDef;
+import android.app.usage.ExternalStorageStats;
+import android.app.usage.StorageStatsManager;
 import android.content.Context;
 import android.content.pm.PackageManager;
+import android.os.UserHandle;
 import android.os.storage.StorageManager;
 import android.os.storage.VolumeInfo;
 import android.util.ArrayMap;
@@ -154,8 +157,29 @@
     }
 
     /**
+     * Returns the file categorization result for the primary internal storage UUID.
+     *
+     * @param context
+     */
+    public static MeasurementResult getMeasurementResult(Context context) {
+        MeasurementResult result = new MeasurementResult();
+        StorageStatsManager ssm =
+                (StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE);
+        ExternalStorageStats stats =
+                ssm.queryExternalStatsForUser(
+                        StorageManager.UUID_PRIVATE_INTERNAL, UserHandle.of(context.getUserId()));
+        result.imagesSize = stats.getImageBytes();
+        result.videosSize = stats.getVideoBytes();
+        result.audioSize = stats.getAudioBytes();
+        result.miscSize =
+                stats.getTotalBytes() - result.imagesSize - result.videosSize - result.audioSize;
+        return result;
+    }
+
+    /**
      * Returns the size of a system for a given context. This is done by finding the difference
      * between the shared data and the total primary storage size.
+     *
      * @param context Context to use to get storage information.
      */
     public static long getSystemSize(Context context) {
diff --git a/services/core/java/com/android/server/wm/AppWindowContainerController.java b/services/core/java/com/android/server/wm/AppWindowContainerController.java
index e26914e..0b90bad 100644
--- a/services/core/java/com/android/server/wm/AppWindowContainerController.java
+++ b/services/core/java/com/android/server/wm/AppWindowContainerController.java
@@ -449,7 +449,8 @@
 
     public boolean addStartingWindow(String pkg, int theme, CompatibilityInfo compatInfo,
             CharSequence nonLocalizedLabel, int labelRes, int icon, int logo, int windowFlags,
-            IBinder transferFrom, boolean newTask, boolean taskSwitch, boolean processRunning) {
+            IBinder transferFrom, boolean newTask, boolean taskSwitch, boolean processRunning,
+            boolean allowTaskSnapshot) {
         synchronized(mWindowMap) {
             if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "setAppStartingWindow: token=" + mToken
                     + " pkg=" + pkg + " transferFrom=" + transferFrom);
@@ -469,7 +470,8 @@
                 return false;
             }
 
-            final int type = getStartingWindowType(newTask, taskSwitch, processRunning);
+            final int type = getStartingWindowType(newTask, taskSwitch, processRunning,
+                    allowTaskSnapshot);
 
             if (type == STARTING_WINDOW_TYPE_SNAPSHOT) {
                 return createSnapshot();
@@ -539,10 +541,11 @@
         return true;
     }
 
-    private int getStartingWindowType(boolean newTask, boolean taskSwitch, boolean processRunning) {
+    private int getStartingWindowType(boolean newTask, boolean taskSwitch, boolean processRunning,
+            boolean allowTaskSnapshot) {
         if (newTask || !processRunning) {
             return STARTING_WINDOW_TYPE_SPLASH_SCREEN;
-        } else if (taskSwitch) {
+        } else if (taskSwitch && allowTaskSnapshot) {
             return STARTING_WINDOW_TYPE_SNAPSHOT;
         } else {
             return STARTING_WINDOW_TYPE_NONE;
@@ -612,13 +615,13 @@
         }
     }
 
-    public void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
+    public void notifyAppResumed(boolean wasStopped) {
         synchronized(mWindowMap) {
             if (mContainer == null) {
                 Slog.w(TAG_WM, "Attempted to notify resumed of non-existing app token: " + mToken);
                 return;
             }
-            mContainer.notifyAppResumed(wasStopped, allowSavedSurface);
+            mContainer.notifyAppResumed(wasStopped);
         }
     }
 
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index a8664a5..f4f6b63 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -582,16 +582,13 @@
      * Notify that the app is now resumed, and it was not stopped before, perform a clean
      * up of the surfaces
      */
-    void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
+    void notifyAppResumed(boolean wasStopped) {
         if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
-                + " allowSavedSurface=" + allowSavedSurface + " " + this);
+                + " " + this);
         mAppStopped = false;
         if (!wasStopped) {
             destroySurfaces(true /*cleanupOnResume*/);
         }
-        if (!allowSavedSurface) {
-            destroySavedSurfaces();
-        }
     }
 
     /**
diff --git a/services/core/java/com/android/server/wm/TaskSnapshotController.java b/services/core/java/com/android/server/wm/TaskSnapshotController.java
index 48b01f4..fbb826d 100644
--- a/services/core/java/com/android/server/wm/TaskSnapshotController.java
+++ b/services/core/java/com/android/server/wm/TaskSnapshotController.java
@@ -162,12 +162,15 @@
         if (top == null) {
             return null;
         }
+        final WindowState mainWindow = top.findMainWindow();
+        if (mainWindow == null) {
+            return null;
+        }
         final GraphicBuffer buffer = top.mDisplayContent.screenshotApplicationsToBuffer(top.token,
                 -1, -1, false, 1.0f, false, true);
         if (buffer == null) {
             return null;
         }
-        final WindowState mainWindow = top.findMainWindow();
         return new TaskSnapshot(buffer, top.getConfiguration().orientation,
                 minRect(mainWindow.mContentInsets, mainWindow.mStableInsets), false /* reduced */,
                 1f /* scale */);
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
index 1f07486..58646fa2 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
@@ -1917,7 +1917,7 @@
             setDeviceOwnerSystemPropertyLocked();
             findOwnerComponentIfNecessaryLocked();
             migrateUserRestrictionsIfNecessaryLocked();
-            setDefaultEnabledUserRestrictionsIfNecessaryLocked();
+            maybeSetDefaultDeviceOwnerUserRestrictionsLocked();
 
             // TODO PO may not have a class name either due to b/17652534.  Address that too.
 
@@ -1925,36 +1925,80 @@
         }
     }
 
-    private void setDefaultEnabledUserRestrictionsIfNecessaryLocked() {
+    /** Apply default restrictions that haven't been applied to device owners yet. */
+    private void maybeSetDefaultDeviceOwnerUserRestrictionsLocked() {
         final ActiveAdmin deviceOwner = getDeviceOwnerAdminLocked();
-        if (deviceOwner != null
-                && !UserRestrictionsUtils.getDefaultEnabledForDeviceOwner().equals(
-                        deviceOwner.defaultEnabledRestrictionsAlreadySet)) {
-            Slog.i(LOG_TAG,"New user restrictions need to be set by default for the device owner");
+        if (deviceOwner != null) {
+            maybeSetDefaultRestrictionsForAdminLocked(mOwners.getDeviceOwnerUserId(),
+                    deviceOwner, UserRestrictionsUtils.getDefaultEnabledForDeviceOwner());
+        }
+    }
 
-            if (VERBOSE_LOG) {
-                Slog.d(LOG_TAG,"Default enabled restrictions for DO: "
-                        + UserRestrictionsUtils.getDefaultEnabledForDeviceOwner()
-                        + ". Restrictions already enabled: "
-                        + deviceOwner.defaultEnabledRestrictionsAlreadySet);
-            }
-
-            Set<String> restrictionsToSet = new ArraySet<>(
-                    UserRestrictionsUtils.getDefaultEnabledForDeviceOwner());
-            restrictionsToSet.removeAll(deviceOwner.defaultEnabledRestrictionsAlreadySet);
-            if (!restrictionsToSet.isEmpty()) {
-                for (String restriction : restrictionsToSet) {
-                    deviceOwner.ensureUserRestrictions().putBoolean(restriction, true);
+    /** Apply default restrictions that haven't been applied to profile owners yet. */
+    private void maybeSetDefaultProfileOwnerUserRestrictions() {
+        synchronized (this) {
+            for (final int userId : mOwners.getProfileOwnerKeys()) {
+                final ActiveAdmin profileOwner = getProfileOwnerAdminLocked(userId);
+                // The following restrictions used to be applied to managed profiles by different
+                // means (via Settings or by disabling components). Now they are proper user
+                // restrictions so we apply them to managed profile owners. Non-managed secondary
+                // users didn't have those restrictions so we skip them to keep existing behavior.
+                if (profileOwner == null || !mUserManager.isManagedProfile(userId)) {
+                    continue;
                 }
-                deviceOwner.defaultEnabledRestrictionsAlreadySet.addAll(restrictionsToSet);
-                Slog.i(LOG_TAG,
-                        "Enabled the following restrictions by default: " + restrictionsToSet);
-
-                saveUserRestrictionsLocked(mOwners.getDeviceOwnerUserId());
+                maybeSetDefaultRestrictionsForAdminLocked(userId, profileOwner,
+                        UserRestrictionsUtils.getDefaultEnabledForManagedProfiles());
+                ensureUnknownSourcesRestrictionForProfileOwnerLocked(
+                        userId, profileOwner, false /* newOwner */);
             }
         }
     }
 
+    /**
+     * Checks whether {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} should be added to the
+     * set of restrictions for this profile owner.
+     */
+    private void ensureUnknownSourcesRestrictionForProfileOwnerLocked(int userId,
+            ActiveAdmin profileOwner, boolean newOwner) {
+        if (newOwner || mInjector.settingsSecureGetIntForUser(
+                Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, 0, userId) != 0) {
+            profileOwner.ensureUserRestrictions().putBoolean(
+                    UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true);
+            saveUserRestrictionsLocked(userId);
+            mInjector.settingsSecurePutIntForUser(
+                    Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, 0, userId);
+        }
+    }
+
+    /**
+     * Apply default restrictions that haven't been applied to a given admin yet.
+     */
+    private void maybeSetDefaultRestrictionsForAdminLocked(
+            int userId, ActiveAdmin admin, Set<String> defaultRestrictions) {
+        if (defaultRestrictions.equals(admin.defaultEnabledRestrictionsAlreadySet)) {
+            return; // The same set of default restrictions has been already applied.
+        }
+        Slog.i(LOG_TAG, "New user restrictions need to be set by default for user " + userId);
+
+        if (VERBOSE_LOG) {
+            Slog.d(LOG_TAG,"Default enabled restrictions: "
+                    + defaultRestrictions
+                    + ". Restrictions already enabled: "
+                    + admin.defaultEnabledRestrictionsAlreadySet);
+        }
+
+        final Set<String> restrictionsToSet = new ArraySet<>(defaultRestrictions);
+        restrictionsToSet.removeAll(admin.defaultEnabledRestrictionsAlreadySet);
+        if (!restrictionsToSet.isEmpty()) {
+            for (final String restriction : restrictionsToSet) {
+                admin.ensureUserRestrictions().putBoolean(restriction, true);
+            }
+            admin.defaultEnabledRestrictionsAlreadySet.addAll(restrictionsToSet);
+            Slog.i(LOG_TAG, "Enabled the following restrictions by default: " + restrictionsToSet);
+            saveUserRestrictionsLocked(userId);
+        }
+    }
+
     private void setDeviceOwnerSystemPropertyLocked() {
         final boolean deviceProvisioned =
                 mInjector.settingsGlobalGetInt(Settings.Global.DEVICE_PROVISIONED, 0) != 0;
@@ -2941,41 +2985,11 @@
         }
     }
 
-    private void ensureUnknownSourcesRestrictionForProfileOwners() {
-        synchronized (this) {
-            for (int userId : mOwners.getProfileOwnerKeys()) {
-                if (!mUserManager.isManagedProfile(userId) ||
-                        mInjector.settingsSecureGetIntForUser(
-                        Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, 0, userId) == 0) {
-                    continue;
-                }
-                setUserRestrictionOnBehalfOfProfileOwnerLocked(
-                        UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, userId);
-                mInjector.settingsSecurePutIntForUser(
-                        Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, 0, userId);
-            }
-        }
-    }
-
-    private void setUserRestrictionOnBehalfOfProfileOwnerLocked(String userRestrictionKey,
-            int userId) {
-        if (UserRestrictionsUtils.isValidRestriction(userRestrictionKey) &&
-                UserRestrictionsUtils.canProfileOwnerChange(userRestrictionKey, userId)) {
-            ActiveAdmin profileOwner = getProfileOwnerAdminLocked(userId);
-            if (profileOwner == null) {
-                return;
-            }
-            Bundle restrictions = profileOwner.ensureUserRestrictions();
-            restrictions.putBoolean(userRestrictionKey, true);
-            saveUserRestrictionsLocked(userId);
-        }
-    }
-
     private void onLockSettingsReady() {
         getUserData(UserHandle.USER_SYSTEM);
         loadOwners();
         cleanUpOldUsers();
-        ensureUnknownSourcesRestrictionForProfileOwners();
+        maybeSetDefaultProfileOwnerUserRestrictions();
         handleStartUser(UserHandle.USER_SYSTEM);
 
         // Register an observer for watching for user setup complete and settings changes.
@@ -6727,8 +6741,8 @@
         synchronized (this) {
             enforceCanSetProfileOwnerLocked(who, userHandle, hasIncompatibleAccountsOrNonAdb);
 
-            if (getActiveAdminUncheckedLocked(who, userHandle) == null
-                    || getUserData(userHandle).mRemovingAdmins.contains(who)) {
+            final ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle);
+            if (admin == null || getUserData(userHandle).mRemovingAdmins.contains(who)) {
                 throw new IllegalArgumentException("Not active admin: " + who);
             }
 
@@ -6744,10 +6758,10 @@
             final long id = mInjector.binderClearCallingIdentity();
             try {
                 if (mUserManager.isManagedProfile(userHandle)) {
-                    setUserRestrictionOnBehalfOfProfileOwnerLocked(
-                            UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, userHandle);
-                    mInjector.settingsSecurePutIntForUser(
-                            Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, 0, userHandle);
+                    maybeSetDefaultRestrictionsForAdminLocked(userHandle, admin,
+                            UserRestrictionsUtils.getDefaultEnabledForManagedProfiles());
+                    ensureUnknownSourcesRestrictionForProfileOwnerLocked(userHandle, admin,
+                            true /* newOwner */);
                 }
             } finally {
                 mInjector.binderRestoreCallingIdentity(id);
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index b1d2f20..40ef6b5 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -96,6 +96,7 @@
 import com.android.server.policy.PhoneWindowManager;
 import com.android.server.power.PowerManagerService;
 import com.android.server.power.ShutdownThread;
+import com.android.server.radio.RadioService;
 import com.android.server.restrictions.RestrictionsManagerService;
 import com.android.server.retaildemo.RetailDemoModeService;
 import com.android.server.security.KeyAttestationApplicationIdProviderService;
@@ -713,6 +714,8 @@
         boolean disableVrManager = SystemProperties.getBoolean("config.disable_vrmanager", false);
         boolean disableCameraService = SystemProperties.getBoolean("config.disable_cameraservice",
                 false);
+        // TODO(b/36863239): Remove when transitioned from native service.
+        boolean enableRadioService = SystemProperties.getBoolean("config.enable_java_radio", false);
 
         boolean isEmulator = SystemProperties.get("ro.kernel.qemu").equals("1");
 
@@ -1213,6 +1216,13 @@
             mSystemServiceManager.startService(AudioService.Lifecycle.class);
             traceEnd();
 
+            if (enableRadioService &&
+                    mPackageManager.hasSystemFeature(PackageManager.FEATURE_RADIO)) {
+                traceBeginAndSlog("StartRadioService");
+                mSystemServiceManager.startService(RadioService.class);
+                traceEnd();
+            }
+
             if (!disableNonCoreServices) {
                 traceBeginAndSlog("StartDockObserver");
                 mSystemServiceManager.startService(DockObserver.class);
diff --git a/services/tests/servicestests/assets/DevicePolicyManagerServiceMigrationTest3/profile_device_policies.xml b/services/tests/servicestests/assets/DevicePolicyManagerServiceMigrationTest3/profile_device_policies.xml
new file mode 100644
index 0000000..b162785
--- /dev/null
+++ b/services/tests/servicestests/assets/DevicePolicyManagerServiceMigrationTest3/profile_device_policies.xml
@@ -0,0 +1,8 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
+<policies setup-complete="true" provisioning-state="3">
+<admin name="com.android.frameworks.servicestests/com.android.server.devicepolicy.DummyDeviceAdmins$Admin1">
+    <policies flags="991" />
+    <strong-auth-unlock-timeout value="0" />
+    <organization-color value="-16738680" />
+</admin>
+</policies>
diff --git a/services/tests/servicestests/assets/DevicePolicyManagerServiceMigrationTest3/profile_owner.xml b/services/tests/servicestests/assets/DevicePolicyManagerServiceMigrationTest3/profile_owner.xml
new file mode 100644
index 0000000..7440424
--- /dev/null
+++ b/services/tests/servicestests/assets/DevicePolicyManagerServiceMigrationTest3/profile_owner.xml
@@ -0,0 +1,8 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
+<root>
+    <profile-owner
+        package="com.android.frameworks.servicestests"
+        name="com.android.frameworks.servicestests"
+        component="com.android.frameworks.servicestests/com.android.server.devicepolicy.DummyDeviceAdmins$Admin1"
+        userRestrictionsMigrated="true" />
+</root>
diff --git a/services/tests/servicestests/assets/DevicePolicyManagerServiceMigrationTest3/system_device_policies.xml b/services/tests/servicestests/assets/DevicePolicyManagerServiceMigrationTest3/system_device_policies.xml
new file mode 100644
index 0000000..5f9a871
--- /dev/null
+++ b/services/tests/servicestests/assets/DevicePolicyManagerServiceMigrationTest3/system_device_policies.xml
@@ -0,0 +1,3 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
+<policies setup-complete="true" provisioning-state="3">
+</policies>
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerServiceMigrationTest.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerServiceMigrationTest.java
index b440095..be1d07b 100644
--- a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerServiceMigrationTest.java
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerServiceMigrationTest.java
@@ -15,31 +15,28 @@
  */
 package com.android.server.devicepolicy;
 
-import com.android.server.LocalServices;
-import com.android.server.SystemService;
-import com.android.server.devicepolicy.DevicePolicyManagerServiceTestable.OwnersTestable;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.when;
 
-import android.app.admin.DevicePolicyManager;
 import android.app.admin.DevicePolicyManagerInternal;
 import android.content.pm.PackageManager;
 import android.content.pm.UserInfo;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.os.UserManager;
-import android.util.Pair;
+import android.provider.Settings;
 
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
+import com.android.server.LocalServices;
+import com.android.server.SystemService;
+import com.android.server.devicepolicy.DevicePolicyManagerServiceTestable.OwnersTestable;
 
 import java.io.File;
 import java.util.HashMap;
 import java.util.Map;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyInt;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.when;
+import java.util.Set;
 
 public class DevicePolicyManagerServiceMigrationTest extends DpmTestBase {
     private DpmMockContext mContext;
@@ -57,7 +54,7 @@
     public void testMigration() throws Exception {
         final File user10dir = mMockContext.addUser(10, 0);
         final File user11dir = mMockContext.addUser(11, UserInfo.FLAG_MANAGED_PROFILE);
-        final File user12dir = mMockContext.addUser(12, 0);
+        mMockContext.addUser(12, 0);
 
         setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
         setUpPackageManagerForAdmin(admin2, UserHandle.getUid(10, 123));
@@ -109,16 +106,13 @@
 
         final Map<Integer, Bundle> newBaseRestrictions = new HashMap<>();
 
-        doAnswer(new Answer<Void>() {
-            @Override
-            public Void answer(InvocationOnMock invocation) throws Throwable {
-                Integer userId = (Integer) invocation.getArguments()[0];
-                Bundle bundle = (Bundle) invocation.getArguments()[1];
+        doAnswer(invocation -> {
+            Integer userId = (Integer) invocation.getArguments()[0];
+            Bundle bundle = (Bundle) invocation.getArguments()[1];
 
-                newBaseRestrictions.put(userId, bundle);
+            newBaseRestrictions.put(userId, bundle);
 
-                return null;
-            }
+            return null;
         }).when(mContext.userManagerInternal).setBaseUserRestrictionsByDpmsForMigration(
                 anyInt(), any(Bundle.class));
 
@@ -225,16 +219,13 @@
 
         final Map<Integer, Bundle> newBaseRestrictions = new HashMap<>();
 
-        doAnswer(new Answer<Void>() {
-            @Override
-            public Void answer(InvocationOnMock invocation) throws Throwable {
-                Integer userId = (Integer) invocation.getArguments()[0];
-                Bundle bundle = (Bundle) invocation.getArguments()[1];
+        doAnswer(invocation -> {
+            Integer userId = (Integer) invocation.getArguments()[0];
+            Bundle bundle = (Bundle) invocation.getArguments()[1];
 
-                newBaseRestrictions.put(userId, bundle);
+            newBaseRestrictions.put(userId, bundle);
 
-                return null;
-            }
+            return null;
         }).when(mContext.userManagerInternal).setBaseUserRestrictionsByDpmsForMigration(
                 anyInt(), any(Bundle.class));
 
@@ -278,4 +269,63 @@
                 ),
                 dpms.getProfileOwnerAdminLocked(UserHandle.USER_SYSTEM).ensureUserRestrictions());
     }
+
+    // Test setting default restrictions for managed profile.
+    public void testMigration3_managedProfileOwner() throws Exception {
+        // Create a managed profile user.
+        final File user10dir = mMockContext.addUser(10, UserInfo.FLAG_MANAGED_PROFILE);
+        // Profile owner package for managed profile user.
+        setUpPackageManagerForAdmin(admin1, UserHandle.getUid(10, 123));
+        // Set up fake UserManager to make it look like a managed profile.
+        when(mMockContext.userManager.isManagedProfile(eq(10))).thenReturn(true);
+        // Set up fake Settings to make it look like INSTALL_NON_MARKET_APPS was reversed.
+        when(mMockContext.settings.settingsSecureGetIntForUser(
+                eq(Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED),
+                eq(0), eq(10))).thenReturn(1);
+        // Write policy and owners files.
+        DpmTestUtils.writeToFile(
+                (new File(mContext.systemUserDataDir, "device_policies.xml")).getAbsoluteFile(),
+                DpmTestUtils.readAsset(mRealTestContext,
+                        "DevicePolicyManagerServiceMigrationTest3/system_device_policies.xml"));
+        DpmTestUtils.writeToFile(
+                (new File(user10dir, "device_policies.xml")).getAbsoluteFile(),
+                DpmTestUtils.readAsset(mRealTestContext,
+                        "DevicePolicyManagerServiceMigrationTest3/profile_device_policies.xml"));
+        DpmTestUtils.writeToFile(
+                (new File(user10dir, "profile_owner.xml")).getAbsoluteFile(),
+                DpmTestUtils.readAsset(mRealTestContext,
+                        "DevicePolicyManagerServiceMigrationTest3/profile_owner.xml"));
+
+        final DevicePolicyManagerServiceTestable dpms;
+
+        // Initialize DPM/DPMS and let it migrate the persisted information.
+        // (Need clearCallingIdentity() to pass permission checks.)
+        final long ident = mContext.binder.clearCallingIdentity();
+        try {
+            LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class);
+
+            dpms = new DevicePolicyManagerServiceTestable(mContext, dataDir);
+
+            dpms.systemReady(SystemService.PHASE_LOCK_SETTINGS_READY);
+            dpms.systemReady(SystemService.PHASE_BOOT_COMPLETED);
+        } finally {
+            mContext.binder.restoreCallingIdentity(ident);
+        }
+
+        assertFalse(dpms.mOwners.hasDeviceOwner());
+        assertTrue(dpms.mOwners.hasProfileOwner(10));
+
+        // Check that default restrictions were applied.
+        DpmTestUtils.assertRestrictions(
+                DpmTestUtils.newRestrictions(
+                        UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
+                        UserManager.DISALLOW_BLUETOOTH_SHARING
+                ),
+                dpms.getProfileOwnerAdminLocked(10).ensureUserRestrictions());
+
+        final Set<String> alreadySet =
+                dpms.getProfileOwnerAdminLocked(10).defaultEnabledRestrictionsAlreadySet;
+        assertEquals(alreadySet.size(), 1);
+        assertTrue(alreadySet.contains(UserManager.DISALLOW_BLUETOOTH_SHARING));
+    }
 }
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerServiceTestable.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerServiceTestable.java
index a6ce1d5..46da3de 100644
--- a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerServiceTestable.java
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerServiceTestable.java
@@ -21,7 +21,6 @@
 import android.app.backup.IBackupManager;
 import android.content.Context;
 import android.content.Intent;
-import android.content.IntentFilter;
 import android.content.pm.IPackageManager;
 import android.content.pm.PackageManagerInternal;
 import android.database.ContentObserver;
@@ -56,17 +55,17 @@
     public static class OwnersTestable extends Owners {
         public static final String LEGACY_FILE = "legacy.xml";
         public static final String DEVICE_OWNER_FILE = "device_owner2.xml";
-        public static final String PROFILE_OWNER_FILE_BASE = "profile_owner.xml";
+        public static final String PROFILE_OWNER_FILE = "profile_owner.xml";
 
         private final File mLegacyFile;
         private final File mDeviceOwnerFile;
-        private final File mProfileOwnerBase;
+        private final File mUsersDataDir;
 
         public OwnersTestable(DpmMockContext context) {
             super(context.userManager, context.userManagerInternal, context.packageManagerInternal);
             mLegacyFile = new File(context.dataDir, LEGACY_FILE);
             mDeviceOwnerFile = new File(context.dataDir, DEVICE_OWNER_FILE);
-            mProfileOwnerBase = new File(context.dataDir, PROFILE_OWNER_FILE_BASE);
+            mUsersDataDir = new File(context.dataDir, "users");
         }
 
         @Override
@@ -81,7 +80,8 @@
 
         @Override
         File getProfileOwnerFileWithTestOverride(int userId) {
-            return new File(mDeviceOwnerFile.getAbsoluteFile() + "-" + userId);
+            final File userDir = new File(mUsersDataDir, String.valueOf(userId));
+            return new File(userDir, PROFILE_OWNER_FILE);
         }
     }
 
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java
index 1ea12d8..23fada4 100644
--- a/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java
@@ -468,7 +468,7 @@
         when(accountManager.getAccountsAsUser(anyInt())).thenReturn(new Account[0]);
 
         // Create a data directory.
-        final File dir = new File(dataDir, "user" + userId);
+        final File dir = new File(dataDir, "users/" + userId);
         DpmTestUtils.clearDir(dir);
 
         when(environment.getUserSystemDirectory(eq(userId))).thenReturn(dir);
diff --git a/services/tests/servicestests/src/com/android/server/storage/DiskStatsLoggingServiceTest.java b/services/tests/servicestests/src/com/android/server/storage/DiskStatsLoggingServiceTest.java
index 3789086..81ce606 100644
--- a/services/tests/servicestests/src/com/android/server/storage/DiskStatsLoggingServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/storage/DiskStatsLoggingServiceTest.java
@@ -20,15 +20,18 @@
 
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyInt;
-import static org.mockito.Mockito.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.isNull;
 import static org.mockito.Mockito.when;
 
 import android.app.job.JobService;
-import android.app.job.JobParameters;
+import android.app.usage.ExternalStorageStats;
+import android.app.usage.StorageStatsManager;
 import android.content.pm.PackageStats;
+import android.os.UserHandle;
 import android.test.AndroidTestCase;
+import android.util.Log;
 
 import com.android.server.storage.DiskStatsLoggingService.LogRunnable;
 
@@ -52,8 +55,10 @@
 public class DiskStatsLoggingServiceTest extends AndroidTestCase {
     @Rule public TemporaryFolder mTemporaryFolder;
     @Rule public TemporaryFolder mDownloads;
-    @Rule public TemporaryFolder mRootDirectory;
     @Mock private AppCollector mCollector;
+    @Mock private JobService mJobService;
+    @Mock private StorageStatsManager mSsm;
+    private ExternalStorageStats mStorageStats;
     private File mInputFile;
 
 
@@ -66,8 +71,10 @@
         mInputFile = mTemporaryFolder.newFile();
         mDownloads = new TemporaryFolder();
         mDownloads.create();
-        mRootDirectory = new TemporaryFolder();
-        mRootDirectory.create();
+        mStorageStats = new ExternalStorageStats();
+        when(mSsm.queryExternalStatsForUser(isNull(String.class), any(UserHandle.class)))
+                .thenReturn(mStorageStats);
+        when(mJobService.getSystemService(anyString())).thenReturn(mSsm);
     }
 
     @Test
@@ -75,9 +82,9 @@
         LogRunnable task = new LogRunnable();
         task.setAppCollector(mCollector);
         task.setDownloadsDirectory(mDownloads.getRoot());
-        task.setRootDirectory(mRootDirectory.getRoot());
         task.setLogOutputFile(mInputFile);
         task.setSystemSize(0L);
+        task.setContext(mJobService);
         task.run();
 
         JSONObject json = getJsonOutput();
@@ -99,10 +106,10 @@
     public void testPopulatedLogTask() throws Exception {
         // Write data to directories.
         writeDataToFile(mDownloads.newFile(), "lol");
-        writeDataToFile(mRootDirectory.newFile("test.jpg"), "1234");
-        writeDataToFile(mRootDirectory.newFile("test.mp4"), "12345");
-        writeDataToFile(mRootDirectory.newFile("test.mp3"), "123456");
-        writeDataToFile(mRootDirectory.newFile("test.whatever"), "1234567");
+        mStorageStats.audioBytes = 6L;
+        mStorageStats.imageBytes = 4L;
+        mStorageStats.videoBytes = 5L;
+        mStorageStats.totalBytes = 22L;
 
         // Write apps.
         ArrayList<PackageStats> apps = new ArrayList<>();
@@ -110,15 +117,16 @@
         testApp.dataSize = 5L;
         testApp.cacheSize = 55L;
         testApp.codeSize = 10L;
+        testApp.userHandle = UserHandle.USER_SYSTEM;
         apps.add(testApp);
-        when(mCollector.getPackageStats(anyInt())).thenReturn(apps);
+        when(mCollector.getPackageStats(anyLong())).thenReturn(apps);
 
         LogRunnable task = new LogRunnable();
         task.setAppCollector(mCollector);
         task.setDownloadsDirectory(mDownloads.getRoot());
-        task.setRootDirectory(mRootDirectory.getRoot());
         task.setLogOutputFile(mInputFile);
         task.setSystemSize(10L);
+        task.setContext(mJobService);
         task.run();
 
         JSONObject json = getJsonOutput();
@@ -143,9 +151,9 @@
         LogRunnable task = new LogRunnable();
         task.setAppCollector(mCollector);
         task.setDownloadsDirectory(mDownloads.getRoot());
-        task.setRootDirectory(mRootDirectory.getRoot());
         task.setLogOutputFile(mInputFile);
         task.setSystemSize(10L);
+        task.setContext(mJobService);
         task.run();
 
         // No exception should be thrown.
diff --git a/services/tests/servicestests/src/com/android/server/wm/AppWindowContainerControllerTests.java b/services/tests/servicestests/src/com/android/server/wm/AppWindowContainerControllerTests.java
index 3c8bf20..d206407 100644
--- a/services/tests/servicestests/src/com/android/server/wm/AppWindowContainerControllerTests.java
+++ b/services/tests/servicestests/src/com/android/server/wm/AppWindowContainerControllerTests.java
@@ -97,7 +97,7 @@
         final WindowTestUtils.TestAppWindowContainerController controller =
                 createAppWindowController();
         controller.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
-                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false);
+                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false, true);
         waitUntilHandlersIdle();
         final AppWindowToken atoken = controller.getAppWindowToken();
         assertHasStartingWindow(atoken);
@@ -113,11 +113,11 @@
         final WindowTestUtils.TestAppWindowContainerController controller2 =
                 createAppWindowController();
         controller1.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
-                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false);
+                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false, true);
         waitUntilHandlersIdle();
         controller2.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
                 android.R.style.Theme, null, "Test", 0, 0, 0, 0, controller1.mToken.asBinder(),
-                true, true, false);
+                true, true, false, true);
         waitUntilHandlersIdle();
         assertNoStartingWindow(controller1.getAppWindowToken());
         assertHasStartingWindow(controller2.getAppWindowToken());
@@ -134,10 +134,10 @@
             // Surprise, ...! Transfer window in the middle of the creation flow.
             controller2.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
                     android.R.style.Theme, null, "Test", 0, 0, 0, 0, controller1.mToken.asBinder(),
-                    true, true, false);
+                    true, true, false, true);
         });
         controller1.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
-                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false);
+                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false, true);
         waitUntilHandlersIdle();
         assertNoStartingWindow(controller1.getAppWindowToken());
         assertHasStartingWindow(controller2.getAppWindowToken());
diff --git a/tests/radio/Android.mk b/tests/radio/Android.mk
new file mode 100644
index 0000000..46bf9cc
--- /dev/null
+++ b/tests/radio/Android.mk
@@ -0,0 +1,33 @@
+# Copyright (C) 2017 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_PACKAGE_NAME := RadioTests
+
+LOCAL_MODULE_TAGS := tests
+# TODO(b/13282254): uncomment when b/13282254 is fixed
+# LOCAL_SDK_VERSION := current
+
+LOCAL_STATIC_JAVA_LIBRARIES := compatibility-device-util android-support-test
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_DEX_PREOPT := false
+LOCAL_PROGUARD_ENABLED := disabled
+
+include $(BUILD_PACKAGE)
diff --git a/tests/radio/AndroidManifest.xml b/tests/radio/AndroidManifest.xml
new file mode 100644
index 0000000..150edbf
--- /dev/null
+++ b/tests/radio/AndroidManifest.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.hardware.radio.tests">
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <instrumentation
+        android:name="android.support.test.runner.AndroidJUnitRunner"
+        android:targetPackage="android.hardware.radio.tests"
+        android:label="Tests for broadcast radio API" >
+    </instrumentation>
+</manifest>
diff --git a/tests/radio/src/android/hardware/radio/tests/RadioTest.java b/tests/radio/src/android/hardware/radio/tests/RadioTest.java
new file mode 100644
index 0000000..47fbe74
--- /dev/null
+++ b/tests/radio/src/android/hardware/radio/tests/RadioTest.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2017 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 android.hardware.radio.tests;
+
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.hardware.radio.RadioManager;
+import android.hardware.radio.RadioTuner;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.*;
+import static org.junit.Assume.*;
+
+/**
+ * A test for broadcast radio API.
+ */
+@RunWith(AndroidJUnit4.class)
+public class RadioTest {
+
+    public final Context mContext = InstrumentationRegistry.getContext();
+
+    private RadioManager mRadioManager;
+    private RadioTuner mRadioTuner;
+    private final List<RadioManager.ModuleProperties> mModules = new ArrayList<>();
+
+    @Before
+    public void setup() {
+        // check if radio is supported and skip the test if it's not
+        PackageManager packageManager = mContext.getPackageManager();
+        boolean isRadioSupported = packageManager.hasSystemFeature(PackageManager.FEATURE_RADIO);
+        assumeTrue(isRadioSupported);
+
+        mRadioManager = (RadioManager)mContext.getSystemService(Context.RADIO_SERVICE);
+        assertNotNull(mRadioManager);
+
+        int status = mRadioManager.listModules(mModules);
+        assertEquals(RadioManager.STATUS_OK, status);
+        assertFalse(mModules.isEmpty());
+    }
+
+    @After
+    public void tearDown() {
+        mRadioManager = null;
+        mModules.clear();
+        if (mRadioTuner != null) {
+            mRadioTuner.close();
+            mRadioTuner = null;
+        }
+    }
+
+    private void openTuner(RadioTuner.Callback callback) {
+        assertNull(mRadioTuner);
+
+        // find FM band and build its config
+        RadioManager.ModuleProperties module = mModules.get(0);
+        RadioManager.FmBandDescriptor fmBandDescriptor = null;
+        for (RadioManager.BandDescriptor band : module.getBands()) {
+            if (band.getType() == RadioManager.BAND_FM) {
+                fmBandDescriptor = (RadioManager.FmBandDescriptor)band;
+                break;
+            }
+        }
+        assertNotNull(fmBandDescriptor);
+        RadioManager.BandConfig fmBandConfig =
+            new RadioManager.FmBandConfig.Builder(fmBandDescriptor).build();
+
+        mRadioTuner = mRadioManager.openTuner(module.getId(), fmBandConfig, true, callback, null);
+        assertNotNull(mRadioTuner);
+    }
+
+    @Test
+    public void testOpenTuner() {
+        openTuner(new RadioTuner.Callback() {});
+    }
+}