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