BackupTestActivity.java revision 83248c432ffe2e2a17abbc8e4960c26574b46bca
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.backuptest; 18 19import android.app.ListActivity; 20import android.backup.BackupDataInput; 21import android.backup.BackupDataOutput; 22import android.backup.BackupManager; 23import android.backup.FileBackupHelper; 24import android.backup.FileRestoreHelper; 25import android.backup.RestoreHelperDispatcher; 26import android.content.Intent; 27import android.content.SharedPreferences; 28import android.os.Bundle; 29import android.os.Handler; 30import android.os.ParcelFileDescriptor; 31import android.util.Log; 32import android.view.View; 33import android.widget.ArrayAdapter; 34import android.widget.ListView; 35import android.widget.Toast; 36 37import java.io.BufferedReader; 38import java.io.File; 39import java.io.FileOutputStream; 40import java.io.FileInputStream; 41import java.io.FileNotFoundException; 42import java.io.InputStreamReader; 43import java.io.IOException; 44import java.io.PrintStream; 45import java.text.DateFormat; 46import java.util.Date; 47 48public class BackupTestActivity extends ListActivity 49{ 50 static final String TAG = "BackupTestActivity"; 51 52 static final String PREF_GROUP_SETTINGS = "settings"; 53 static final String PREF_KEY = "pref"; 54 static final String FILE_NAME = "file.txt"; 55 56 Test[] mTests = new Test[] { 57 new Test("Show File") { 58 void run() { 59 StringBuffer str = new StringBuffer(); 60 str.append("Text is:"); 61 BufferedReader reader = null; 62 try { 63 reader = new BufferedReader(new InputStreamReader(openFileInput(FILE_NAME))); 64 while (reader.ready()) { 65 str.append("\n"); 66 str.append(reader.readLine()); 67 } 68 } catch (IOException ex) { 69 str.append("ERROR: "); 70 str.append(ex.toString()); 71 } 72 Log.d(TAG, str.toString()); 73 Toast.makeText(BackupTestActivity.this, str, Toast.LENGTH_SHORT).show(); 74 } 75 }, 76 new Test("Append to File") { 77 void run() { 78 PrintStream output = null; 79 try { 80 output = new PrintStream(openFileOutput(FILE_NAME, MODE_APPEND)); 81 DateFormat formatter = DateFormat.getDateTimeInstance(); 82 output.println(formatter.format(new Date())); 83 output.close(); 84 } catch (IOException ex) { 85 if (output != null) { 86 output.close(); 87 } 88 } 89 BackupManager bm = new BackupManager(BackupTestActivity.this); 90 bm.dataChanged(); 91 } 92 }, 93 new Test("Clear File") { 94 void run() { 95 PrintStream output = null; 96 try { 97 output = new PrintStream(openFileOutput(FILE_NAME, MODE_PRIVATE)); 98 output.close(); 99 } catch (IOException ex) { 100 if (output != null) { 101 output.close(); 102 } 103 } 104 BackupManager bm = new BackupManager(BackupTestActivity.this); 105 bm.dataChanged(); 106 } 107 }, 108 new Test("Poke") { 109 void run() { 110 BackupManager bm = new BackupManager(BackupTestActivity.this); 111 bm.dataChanged(); 112 } 113 }, 114 new Test("Show Shared Pref") { 115 void run() { 116 SharedPreferences prefs = getSharedPreferences(PREF_GROUP_SETTINGS, MODE_PRIVATE); 117 int val = prefs.getInt(PREF_KEY, 0); 118 String str = "'" + PREF_KEY + "' is " + val; 119 Log.d(TAG, str); 120 Toast.makeText(BackupTestActivity.this, str, Toast.LENGTH_SHORT).show(); 121 } 122 }, 123 new Test("Increment Shared Pref") { 124 void run() { 125 SharedPreferences prefs = getSharedPreferences(PREF_GROUP_SETTINGS, MODE_PRIVATE); 126 int val = prefs.getInt(PREF_KEY, 0); 127 SharedPreferences.Editor editor = prefs.edit(); 128 editor.putInt(PREF_KEY, val+1); 129 editor.commit(); 130 BackupManager bm = new BackupManager(BackupTestActivity.this); 131 bm.dataChanged(); 132 } 133 }, 134 new Test("Backup Helpers") { 135 void run() { 136 try { 137 writeFile("a", "a\naa", MODE_PRIVATE); 138 writeFile("empty", "", MODE_PRIVATE); 139 140 ParcelFileDescriptor state = ParcelFileDescriptor.open( 141 new File(getFilesDir(), "state"), 142 ParcelFileDescriptor.MODE_READ_WRITE|ParcelFileDescriptor.MODE_CREATE| 143 ParcelFileDescriptor.MODE_TRUNCATE); 144 FileBackupHelper h = new FileBackupHelper(BackupTestActivity.this, 145 "FileBackupHelper"); 146 FileOutputStream dataFile = openFileOutput("backup_test", MODE_WORLD_READABLE); 147 BackupDataOutput data = new BackupDataOutput(dataFile.getFD()); 148 h.performBackup(null, data, state, new String[] { "a", "empty" }); 149 dataFile.close(); 150 state.close(); 151 } catch (IOException ex) { 152 throw new RuntimeException(ex); 153 } 154 } 155 }, 156 new Test("Restore Helpers") { 157 void run() { 158 try { 159 RestoreHelperDispatcher dispatch = new RestoreHelperDispatcher(); 160 dispatch.addHelper("FileBackupHelper", 161 new FileRestoreHelper(BackupTestActivity.this)); 162 FileInputStream dataFile = openFileInput("backup_test"); 163 BackupDataInput data = new BackupDataInput(dataFile.getFD()); 164 dispatch.dispatch(data); 165 dataFile.close(); 166 } catch (IOException ex) { 167 throw new RuntimeException(ex); 168 } 169 } 170 } 171 }; 172 173 abstract class Test { 174 String name; 175 Test(String n) { 176 name = n; 177 } 178 abstract void run(); 179 } 180 181 @Override 182 public void onCreate(Bundle icicle) { 183 super.onCreate(icicle); 184 185 String[] labels = new String[mTests.length]; 186 for (int i=0; i<mTests.length; i++) { 187 labels[i] = mTests[i].name; 188 } 189 190 setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, labels)); 191 } 192 193 @Override 194 public void onListItemClick(ListView l, View v, int position, long id) 195 { 196 Test t = mTests[position]; 197 Log.d(TAG, "Test: " + t.name); 198 t.run(); 199 } 200 201 void writeFile(String name, String contents, int mode) { 202 try { 203 PrintStream out = new PrintStream(openFileOutput(name, mode)); 204 out.print(contents); 205 out.close(); 206 } catch (FileNotFoundException ex) { 207 throw new RuntimeException(ex); 208 } 209 } 210} 211 212