Loading core/java/android/backup/BackupDataInputStream.java +2 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package android.backup; import android.util.Log; import java.io.InputStream; import java.io.IOException; Loading core/java/android/backup/FileRestoreHelper.java 0 → 100644 +41 −0 Original line number Diff line number Diff line /* * Copyright (C) 2009 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.backup; import android.content.Context; import android.util.Log; import java.io.File; /** @hide */ public class FileRestoreHelper extends RestoreHelperBase implements RestoreHelper { private static final String TAG = "FileRestoreHelper"; File mFilesDir; public FileRestoreHelper(Context context) { super(context); mFilesDir = context.getFilesDir(); } public void restoreEntity(BackupDataInputStream data) { Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size()); // TODO: turn this off before ship File f = new File(mFilesDir, data.getKey()); writeFile(f, data); } } core/java/android/backup/RestoreHelper.java +1 −1 Original line number Diff line number Diff line Loading @@ -26,6 +26,6 @@ public interface RestoreHelper { * Do not close the <code>data</code> stream. Do not read more than * <code>dataSize</code> bytes from <code>data</code>. */ public void performRestore(BackupDataInputStream data); public void restoreEntity(BackupDataInputStream data); } core/java/android/backup/RestoreHelperBase.java 0 → 100644 +85 −0 Original line number Diff line number Diff line /* * Copyright (C) 2009 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.backup; import android.content.Context; import android.util.Log; import java.io.InputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; class RestoreHelperBase { private static final String TAG = "RestoreHelperBase"; private static final int BUF_SIZE = 8 * 1024; Context mContext; byte[] mBuf = new byte[BUF_SIZE]; boolean mExceptionLogged; RestoreHelperBase(Context context) { mContext = context; } void writeFile(File f, InputStream in) { boolean success = false; FileOutputStream out = null; try { // Create the enclosing directory. File parent = f.getParentFile(); parent.mkdirs(); // Copy the file. int sum = 0; out = new FileOutputStream(f); byte[] buf = mBuf; int amt; while ((amt = in.read(buf)) > 0) { out.write(buf, 0, amt); sum += amt; } // TODO: Set the permissions of the file. // We're done success = true; out = null; } catch (IOException ex) { // Bail on this entity. Only log one exception per helper object. if (!mExceptionLogged) { Log.e(TAG, "Failed restoring file '" + f + "' for app '" + mContext.getPackageName() + '\'', ex); mExceptionLogged = true; } } finally { if (out != null) { try { out.close(); } catch (IOException ex) { } } if (!success) { // Something didn't work out, delete the file f.delete(); } } } } core/java/android/backup/RestoreHelperDispatcher.java +2 −2 Original line number Diff line number Diff line Loading @@ -21,7 +21,7 @@ import java.util.HashMap; /** @hide */ public class RestoreHelperDispatcher { HashMap<String,RestoreHelper> mHelpers; HashMap<String,RestoreHelper> mHelpers = new HashMap<String,RestoreHelper>(); public void addHelper(String keyPrefix, RestoreHelper helper) { mHelpers.put(keyPrefix, helper); Loading @@ -38,7 +38,7 @@ public class RestoreHelperDispatcher { if (helper != null) { stream.dataSize = input.getDataSize(); stream.key = rawKey.substring(pos+1); helper.performRestore(stream); helper.restoreEntity(stream); } } input.skipEntityData(); // In case they didn't consume the data. Loading Loading
core/java/android/backup/BackupDataInputStream.java +2 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package android.backup; import android.util.Log; import java.io.InputStream; import java.io.IOException; Loading
core/java/android/backup/FileRestoreHelper.java 0 → 100644 +41 −0 Original line number Diff line number Diff line /* * Copyright (C) 2009 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.backup; import android.content.Context; import android.util.Log; import java.io.File; /** @hide */ public class FileRestoreHelper extends RestoreHelperBase implements RestoreHelper { private static final String TAG = "FileRestoreHelper"; File mFilesDir; public FileRestoreHelper(Context context) { super(context); mFilesDir = context.getFilesDir(); } public void restoreEntity(BackupDataInputStream data) { Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size()); // TODO: turn this off before ship File f = new File(mFilesDir, data.getKey()); writeFile(f, data); } }
core/java/android/backup/RestoreHelper.java +1 −1 Original line number Diff line number Diff line Loading @@ -26,6 +26,6 @@ public interface RestoreHelper { * Do not close the <code>data</code> stream. Do not read more than * <code>dataSize</code> bytes from <code>data</code>. */ public void performRestore(BackupDataInputStream data); public void restoreEntity(BackupDataInputStream data); }
core/java/android/backup/RestoreHelperBase.java 0 → 100644 +85 −0 Original line number Diff line number Diff line /* * Copyright (C) 2009 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.backup; import android.content.Context; import android.util.Log; import java.io.InputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; class RestoreHelperBase { private static final String TAG = "RestoreHelperBase"; private static final int BUF_SIZE = 8 * 1024; Context mContext; byte[] mBuf = new byte[BUF_SIZE]; boolean mExceptionLogged; RestoreHelperBase(Context context) { mContext = context; } void writeFile(File f, InputStream in) { boolean success = false; FileOutputStream out = null; try { // Create the enclosing directory. File parent = f.getParentFile(); parent.mkdirs(); // Copy the file. int sum = 0; out = new FileOutputStream(f); byte[] buf = mBuf; int amt; while ((amt = in.read(buf)) > 0) { out.write(buf, 0, amt); sum += amt; } // TODO: Set the permissions of the file. // We're done success = true; out = null; } catch (IOException ex) { // Bail on this entity. Only log one exception per helper object. if (!mExceptionLogged) { Log.e(TAG, "Failed restoring file '" + f + "' for app '" + mContext.getPackageName() + '\'', ex); mExceptionLogged = true; } } finally { if (out != null) { try { out.close(); } catch (IOException ex) { } } if (!success) { // Something didn't work out, delete the file f.delete(); } } } }
core/java/android/backup/RestoreHelperDispatcher.java +2 −2 Original line number Diff line number Diff line Loading @@ -21,7 +21,7 @@ import java.util.HashMap; /** @hide */ public class RestoreHelperDispatcher { HashMap<String,RestoreHelper> mHelpers; HashMap<String,RestoreHelper> mHelpers = new HashMap<String,RestoreHelper>(); public void addHelper(String keyPrefix, RestoreHelper helper) { mHelpers.put(keyPrefix, helper); Loading @@ -38,7 +38,7 @@ public class RestoreHelperDispatcher { if (helper != null) { stream.dataSize = input.getDataSize(); stream.key = rawKey.substring(pos+1); helper.performRestore(stream); helper.restoreEntity(stream); } } input.skipEntityData(); // In case they didn't consume the data. Loading