2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.backuptest;
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;
36 import java.io.BufferedReader;
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;
47 public class BackupTestActivity extends ListActivity
49 static final String TAG = "BackupTestActivity";
51 static final String PREF_GROUP_SETTINGS = "settings";
52 static final String PREF_KEY = "pref";
53 static final String FILE_NAME = "file.txt";
55 BackupManager sBm = new BackupManager(this);
57 Test[] mTests = new Test[] {
58 new Test("Show File") {
60 StringBuffer str = new StringBuffer();
61 str.append("Text is:");
62 BufferedReader reader = null;
64 reader = new BufferedReader(new InputStreamReader(openFileInput(FILE_NAME)));
65 while (reader.ready()) {
67 str.append(reader.readLine());
69 } catch (IOException ex) {
70 str.append("ERROR: ");
71 str.append(ex.toString());
73 Log.d(TAG, str.toString());
74 Toast.makeText(BackupTestActivity.this, str, Toast.LENGTH_SHORT).show();
77 new Test("Append to File") {
79 PrintStream output = null;
81 output = new PrintStream(openFileOutput(FILE_NAME, MODE_APPEND));
82 DateFormat formatter = DateFormat.getDateTimeInstance();
83 output.println(formatter.format(new Date()));
85 } catch (IOException ex) {
93 new Test("Clear File") {
95 PrintStream output = null;
97 output = new PrintStream(openFileOutput(FILE_NAME, MODE_PRIVATE));
99 } catch (IOException ex) {
100 if (output != null) {
112 new Test("Show Shared Pref") {
114 SharedPreferences prefs = getSharedPreferences(PREF_GROUP_SETTINGS, MODE_PRIVATE);
115 int val = prefs.getInt(PREF_KEY, 0);
116 String str = "'" + PREF_KEY + "' is " + val;
118 Toast.makeText(BackupTestActivity.this, str, Toast.LENGTH_SHORT).show();
121 new Test("Increment Shared Pref") {
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);
131 new Test("Backup Helpers") {
134 writeFile("a", "a\naa", MODE_PRIVATE);
135 writeFile("empty", "", MODE_PRIVATE);
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);
148 } catch (IOException ex) {
149 throw new RuntimeException(ex);
153 new Test("Restore Helpers") {
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);
169 } catch (IOException ex) {
170 throw new RuntimeException(ex);
176 abstract class Test {
185 public void onCreate(Bundle icicle) {
186 super.onCreate(icicle);
188 String[] labels = new String[mTests.length];
189 for (int i=0; i<mTests.length; i++) {
190 labels[i] = mTests[i].name;
193 setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, labels));
197 public void onListItemClick(ListView l, View v, int position, long id)
199 Test t = mTests[position];
200 Log.d(TAG, "Test: " + t.name);
204 void writeFile(String name, String contents, int mode) {
206 PrintStream out = new PrintStream(openFileOutput(name, mode));
209 } catch (FileNotFoundException ex) {
210 throw new RuntimeException(ex);