OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / tests / backup / src / com / android / backuptest / BackupTestActivity.java
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
17 package com.android.backuptest;
18
19 import android.app.ListActivity;
20 import android.app.backup.BackupHelperDispatcher;
21 import android.app.backup.BackupDataInput;
22 import android.app.backup.BackupDataOutput;
23 import android.app.backup.BackupManager;
24 import android.app.backup.FileBackupHelper;
25 import android.content.Intent;
26 import android.content.SharedPreferences;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.ParcelFileDescriptor;
30 import android.util.Log;
31 import android.view.View;
32 import android.widget.ArrayAdapter;
33 import android.widget.ListView;
34 import android.widget.Toast;
35
36 import java.io.BufferedReader;
37 import java.io.File;
38 import java.io.FileOutputStream;
39 import java.io.FileInputStream;
40 import java.io.FileNotFoundException;
41 import java.io.InputStreamReader;
42 import java.io.IOException;
43 import java.io.PrintStream;
44 import java.text.DateFormat;
45 import java.util.Date;
46
47 public class BackupTestActivity extends ListActivity
48 {
49     static final String TAG = "BackupTestActivity";
50
51     static final String PREF_GROUP_SETTINGS = "settings";
52     static final String PREF_KEY = "pref";
53     static final String FILE_NAME = "file.txt";
54
55     BackupManager sBm = new BackupManager(this);
56
57     Test[] mTests = new Test[] {
58         new Test("Show File") {
59             void run() {
60                 StringBuffer str = new StringBuffer();
61                 str.append("Text is:");
62                 BufferedReader reader = null;
63                 try {
64                     reader = new BufferedReader(new InputStreamReader(openFileInput(FILE_NAME)));
65                     while (reader.ready()) {
66                         str.append("\n");
67                         str.append(reader.readLine());
68                     }
69                 } catch (IOException ex) {
70                     str.append("ERROR: ");
71                     str.append(ex.toString());
72                 }
73                 Log.d(TAG, str.toString());
74                 Toast.makeText(BackupTestActivity.this, str, Toast.LENGTH_SHORT).show();
75             }
76         },
77         new Test("Append to File") {
78             void run() {
79                 PrintStream output = null;
80                 try {
81                     output = new PrintStream(openFileOutput(FILE_NAME, MODE_APPEND));
82                     DateFormat formatter = DateFormat.getDateTimeInstance();
83                     output.println(formatter.format(new Date()));
84                     output.close();
85                 } catch (IOException ex) {
86                     if (output != null) {
87                         output.close();
88                     }
89                 }
90                 sBm.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                 sBm.dataChanged();
105             }
106         },
107         new Test("Poke") {
108             void run() {
109                 sBm.dataChanged();
110             }
111         },
112         new Test("Show Shared Pref") {
113             void run() {
114                 SharedPreferences prefs = getSharedPreferences(PREF_GROUP_SETTINGS, MODE_PRIVATE);
115                 int val = prefs.getInt(PREF_KEY, 0);
116                 String str = "'" + PREF_KEY + "' is " + val;
117                 Log.d(TAG, str);
118                 Toast.makeText(BackupTestActivity.this, str, Toast.LENGTH_SHORT).show();
119             }
120         },
121         new Test("Increment Shared Pref") {
122             void run() {
123                 SharedPreferences prefs = getSharedPreferences(PREF_GROUP_SETTINGS, MODE_PRIVATE);
124                 int val = prefs.getInt(PREF_KEY, 0);
125                 SharedPreferences.Editor editor = prefs.edit();
126                 editor.putInt(PREF_KEY, val+1);
127                 editor.commit();
128                 sBm.dataChanged();
129             }
130         },
131         new Test("Backup Helpers") {
132             void run() {
133                 try {
134                     writeFile("a", "a\naa", MODE_PRIVATE);
135                     writeFile("empty", "", MODE_PRIVATE);
136
137                     ParcelFileDescriptor state = ParcelFileDescriptor.open(
138                             new File(getFilesDir(), "state"),
139                             ParcelFileDescriptor.MODE_READ_WRITE|ParcelFileDescriptor.MODE_CREATE|
140                             ParcelFileDescriptor.MODE_TRUNCATE);
141                     FileBackupHelper h = new FileBackupHelper(BackupTestActivity.this,
142                             new String[] { "a", "empty" });
143                     FileOutputStream dataFile = openFileOutput("backup_test", MODE_WORLD_READABLE);
144                     BackupDataOutput data = new BackupDataOutput(dataFile.getFD());
145                     h.performBackup(null, data, state);
146                     dataFile.close();
147                     state.close();
148                 } catch (IOException ex) {
149                     throw new RuntimeException(ex);
150                 }
151             }
152         },
153         new Test("Restore Helpers") {
154             void run() {
155                 try {
156                     BackupHelperDispatcher dispatch = new BackupHelperDispatcher();
157                     dispatch.addHelper("", new FileBackupHelper(BackupTestActivity.this,
158                             new String[] { "a", "empty" }));
159                     FileInputStream dataFile = openFileInput("backup_test");
160                     BackupDataInput data = new BackupDataInput(dataFile.getFD());
161                     ParcelFileDescriptor state = ParcelFileDescriptor.open(
162                             new File(getFilesDir(), "restore_state"),
163                             ParcelFileDescriptor.MODE_READ_WRITE|ParcelFileDescriptor.MODE_CREATE|
164                             ParcelFileDescriptor.MODE_TRUNCATE);
165                     // TODO: a more plausable synthetic stored-data version number
166                     dispatch.performRestore(data, 0, state);
167                     dataFile.close();
168                     state.close();
169                 } catch (IOException ex) {
170                     throw new RuntimeException(ex);
171                 }
172             }
173         }
174     };
175
176     abstract class Test {
177         String name;
178         Test(String n) {
179             name = n;
180         }
181         abstract void run();
182     }
183
184     @Override
185     public void onCreate(Bundle icicle) {
186         super.onCreate(icicle);
187
188         String[] labels = new String[mTests.length];
189         for (int i=0; i<mTests.length; i++) {
190             labels[i] = mTests[i].name;
191         }
192
193         setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, labels));
194     }
195
196     @Override
197     public void onListItemClick(ListView l, View v, int position, long id)
198     {
199         Test t = mTests[position];
200         Log.d(TAG, "Test: " + t.name);
201         t.run();
202     }
203     
204     void writeFile(String name, String contents, int mode) {
205         try {
206             PrintStream out = new PrintStream(openFileOutput(name, mode));
207             out.print(contents);
208             out.close();
209         } catch (FileNotFoundException ex) {
210             throw new RuntimeException(ex);
211         }
212     }
213 }
214