• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.cts.verifier.backup;
18 
19 import com.android.cts.verifier.PassFailButtons;
20 import com.android.cts.verifier.R;
21 
22 import android.app.AlertDialog;
23 import android.app.Dialog;
24 import android.app.backup.BackupManager;
25 import android.app.backup.FileBackupHelper;
26 import android.app.backup.SharedPreferencesBackupHelper;
27 import android.content.Context;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.content.SharedPreferences;
31 import android.os.AsyncTask;
32 import android.os.Bundle;
33 import android.provider.Settings;
34 import android.util.Log;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.View.OnClickListener;
38 import android.view.ViewGroup;
39 import android.view.Window;
40 import android.widget.BaseAdapter;
41 import android.widget.TextView;
42 import android.widget.Toast;
43 
44 import java.io.File;
45 import java.io.FileNotFoundException;
46 import java.io.PrintWriter;
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.Random;
50 import java.util.Scanner;
51 
52 /**
53  * Test for checking whether the BackupManager is working properly. It lists the values of
54  * several preferences and contents of files that should get backed up and restored after
55  * running the backup manager and reinstalling the CTS verifier.
56  */
57 public class BackupTestActivity extends PassFailButtons.ListActivity {
58 
59     private static final String TAG = BackupTestActivity.class.getSimpleName();
60 
61     private static final int INSTRUCTIONS_DIALOG_ID = 1;
62 
63     private static final String TEST_PREFS_1 = "test-prefs-1";
64     private static final String INT_PREF = "int-pref";
65     private static final String BOOL_PREF = "bool-pref";
66 
67     private static final String TEST_PREFS_2 = "test-prefs-2";
68     private static final String FLOAT_PREF = "float-pref";
69     private static final String LONG_PREF = "long-pref";
70     private static final String STRING_PREF = "string-pref";
71 
72     private static final String TEST_FILE_1 = "test-file-1";
73     private static final String TEST_FILE_2 = "test-file-2";
74 
75     private BackupAdapter mAdapter;
76 
77     @Override
onCreate(Bundle savedInstanceState)78     protected void onCreate(Bundle savedInstanceState) {
79         super.onCreate(savedInstanceState);
80         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
81         setContentView(R.layout.bu_main);
82         setPassFailButtonClickListeners();
83         setInfoResources(R.string.backup_test, R.string.backup_info, 0);
84 
85         mAdapter = new BackupAdapter(this);
86         setListAdapter(mAdapter);
87 
88         new LoadBackupItemsTask().execute();
89 
90         findViewById(R.id.generate_button).setOnClickListener(new OnClickListener() {
91             @Override
92             public void onClick(View v) {
93                 new GenerateValuesTask().execute();
94             }
95         });
96     }
97 
getSharedPreferencesBackupHelper(Context context)98     public static SharedPreferencesBackupHelper getSharedPreferencesBackupHelper(Context context) {
99         return new SharedPreferencesBackupHelper(context, TEST_PREFS_1, TEST_PREFS_2);
100     }
101 
getFileBackupHelper(Context context)102     public static FileBackupHelper getFileBackupHelper(Context context) {
103         return new FileBackupHelper(context, TEST_FILE_1, TEST_FILE_2);
104     }
105 
106     class LoadBackupItemsTask extends AsyncTask<Void, Void, List<BackupItem>> {
107 
108         @Override
onPreExecute()109         protected void onPreExecute() {
110             super.onPreExecute();
111             setProgressBarIndeterminateVisibility(true);
112         }
113 
114         @Override
doInBackground(Void... params)115         protected List<BackupItem> doInBackground(Void... params) {
116             List<BackupItem> items = new ArrayList<BackupItem>();
117 
118             items.add(new CategoryBackupItem(R.string.bu_preferences));
119             loadPreferenceGroup1(items);
120             loadPreferenceGroup2(items);
121 
122             items.add(new CategoryBackupItem(R.string.bu_files));
123             loadFile(TEST_FILE_1, items);
124             loadFile(TEST_FILE_2, items);
125 
126             return items;
127         }
128 
loadPreferenceGroup1(List<BackupItem> items)129         private void loadPreferenceGroup1(List<BackupItem> items) {
130             SharedPreferences prefs = getSharedPreferences(TEST_PREFS_1, MODE_PRIVATE);
131 
132             int intValue = prefs.getInt(INT_PREF, 0);
133             items.add(new PreferenceBackupItem(TEST_PREFS_1, INT_PREF, "" + intValue));
134 
135             boolean boolValue = prefs.getBoolean(BOOL_PREF, false);
136             items.add(new PreferenceBackupItem(TEST_PREFS_1, BOOL_PREF, "" + boolValue));
137         }
138 
loadPreferenceGroup2(List<BackupItem> items)139         private void loadPreferenceGroup2(List<BackupItem> items) {
140             SharedPreferences prefs = getSharedPreferences(TEST_PREFS_2, MODE_PRIVATE);
141 
142             float floatValue = prefs.getFloat(FLOAT_PREF, 0.0f);
143             items.add(new PreferenceBackupItem(TEST_PREFS_2, FLOAT_PREF, "" + floatValue));
144 
145             long longValue = prefs.getLong(LONG_PREF, 0L);
146             items.add(new PreferenceBackupItem(TEST_PREFS_2, LONG_PREF, "" + longValue));
147 
148             String stringValue = prefs.getString(STRING_PREF, null);
149             items.add(new PreferenceBackupItem(TEST_PREFS_2, STRING_PREF, stringValue));
150         }
151 
loadFile(String fileName, List<BackupItem> items)152         private void loadFile(String fileName, List<BackupItem> items) {
153             StringBuilder contents = new StringBuilder();
154             Scanner scanner = null;
155             try {
156                 scanner = new Scanner(new File(getFilesDir(), fileName));
157                 while (scanner.hasNext()) {
158                     contents.append(scanner.nextLine());
159                 }
160                 scanner.close();
161             } catch (FileNotFoundException e) {
162                 Log.e(TAG, "Couldn't find test file but this may be fine...", e);
163             } finally {
164                 if (scanner != null) {
165                     scanner.close();
166                 }
167             }
168             items.add(new FileBackupItem(fileName, contents.toString()));
169         }
170 
171         @Override
onPostExecute(List<BackupItem> result)172         protected void onPostExecute(List<BackupItem> result) {
173             super.onPostExecute(result);
174             setProgressBarIndeterminateVisibility(false);
175             mAdapter.clear();
176             mAdapter.addAll(result);
177         }
178     }
179 
180     class GenerateValuesTask extends AsyncTask<Void, Void, Exception> {
181 
182         @Override
doInBackground(Void... params)183         protected Exception doInBackground(Void... params) {
184             Random random = new Random();
185             generatePreferenceGroup1(random);
186             generatePreferenceGroup2(random);
187             try {
188                 generateTestFile(TEST_FILE_1, random);
189                 generateTestFile(TEST_FILE_2, random);
190             } catch (FileNotFoundException e) {
191                 return e;
192             }
193             return null;
194         }
195 
generatePreferenceGroup1(Random random)196         private void generatePreferenceGroup1(Random random) {
197             SharedPreferences prefs = getSharedPreferences(TEST_PREFS_1, MODE_PRIVATE);
198             SharedPreferences.Editor editor = prefs.edit();
199             editor.putInt(INT_PREF, (random.nextInt(100) + 1));
200             editor.putBoolean(BOOL_PREF, random.nextBoolean());
201             editor.commit();
202         }
203 
generatePreferenceGroup2(Random random)204         private void generatePreferenceGroup2(Random random) {
205             SharedPreferences prefs = getSharedPreferences(TEST_PREFS_2, MODE_PRIVATE);
206             SharedPreferences.Editor editor = prefs.edit();
207             editor.putFloat(FLOAT_PREF, random.nextFloat());
208             editor.putLong(LONG_PREF, random.nextLong());
209             editor.putString(STRING_PREF, "Random number: " + (random.nextInt(100) + 1));
210             editor.commit();
211         }
212 
generateTestFile(String fileName, Random random)213         private void generateTestFile(String fileName, Random random)
214                 throws FileNotFoundException {
215             File file = new File(getFilesDir(), fileName);
216             PrintWriter writer = new PrintWriter(file);
217             writer.write("Random number: " + (random.nextInt(100) + 1));
218             writer.close();
219         }
220 
221         @Override
onPostExecute(Exception exception)222         protected void onPostExecute(Exception exception) {
223             super.onPostExecute(exception);
224             if (exception != null) {
225                 Log.e(TAG, "Couldn't generate test data...", exception);
226                 Toast.makeText(BackupTestActivity.this, R.string.bu_generate_error,
227                         Toast.LENGTH_LONG).show();
228             } else {
229                 showDialog(INSTRUCTIONS_DIALOG_ID);
230 
231                 BackupManager backupManager = new BackupManager(BackupTestActivity.this);
232                 backupManager.dataChanged();
233 
234                 new LoadBackupItemsTask().execute();
235             }
236         }
237     }
238 
239     @Override
onCreateDialog(int id, Bundle args)240     public Dialog onCreateDialog(int id, Bundle args) {
241         switch (id) {
242             case INSTRUCTIONS_DIALOG_ID:
243                 return new AlertDialog.Builder(this)
244                     .setIcon(android.R.drawable.ic_dialog_info)
245                     .setTitle(R.string.backup_test)
246                     .setMessage(R.string.bu_instructions)
247                     .setPositiveButton(android.R.string.ok, null)
248                     .setNeutralButton(R.string.bu_settings, new DialogInterface.OnClickListener() {
249                         @Override
250                         public void onClick(DialogInterface dialog, int which) {
251                             startActivity(new Intent(Settings.ACTION_PRIVACY_SETTINGS));
252                         }
253                     })
254                     .create();
255 
256             default:
257                 return super.onCreateDialog(id, args);
258         }
259     }
260 
261     interface BackupItem {
262         int getViewType();
263         View getView(LayoutInflater inflater, int position, View convertView, ViewGroup parent);
264     }
265 
266     static class CategoryBackupItem implements BackupItem {
267 
268         private final int mTitleResId;
269 
270         CategoryBackupItem(int titleResId) {
271             mTitleResId = titleResId;
272         }
273 
274         @Override
275         public int getViewType() {
276             return 0;
277         }
278 
279         @Override
280         public View getView(LayoutInflater inflater, int position, View convertView,
281                 ViewGroup parent) {
282             TextView view = (TextView) convertView;
283             if (convertView == null) {
284                 view = (TextView) inflater.inflate(R.layout.test_category_row, parent, false);
285             }
286             view.setText(mTitleResId);
287             return view;
288         }
289     }
290 
291     static class PreferenceBackupItem implements BackupItem {
292 
293         private final String mGroup;
294 
295         private final String mName;
296 
297         private final String mValue;
298 
299         PreferenceBackupItem(String group, String name, String value) {
300             mGroup = group;
301             mName = name;
302             mValue = value;
303         }
304 
305         @Override
306         public int getViewType() {
307             return 1;
308         }
309 
310         @Override
311         public View getView(LayoutInflater inflater, int position, View convertView,
312                 ViewGroup parent) {
313             TextView view = (TextView) convertView;
314             if (convertView == null) {
315                 view = (TextView) inflater.inflate(R.layout.bu_preference_row, parent, false);
316             }
317             view.setText(mGroup + "/" + mName + " : " + mValue);
318             return view;
319         }
320     }
321 
322     static class FileBackupItem implements BackupItem {
323 
324         private final String mName;
325 
326         private final String mContents;
327 
328         FileBackupItem(String name, String contents) {
329             mName = name;
330             mContents = contents;
331         }
332 
333         @Override
334         public int getViewType() {
335             return 2;
336         }
337 
338         @Override
339         public View getView(LayoutInflater inflater, int position, View convertView,
340                 ViewGroup parent) {
341             TextView view = (TextView) convertView;
342             if (convertView == null) {
343                 view = (TextView) inflater.inflate(R.layout.bu_preference_row, parent, false);
344             }
345             view.setText(mName + " : " + mContents);
346             return view;
347         }
348     }
349 
350     class BackupAdapter extends BaseAdapter {
351 
352         private final LayoutInflater mLayoutInflater;
353 
354         private final List<BackupItem> mItems = new ArrayList<BackupItem>();
355 
356         public BackupAdapter(Context context) {
357             mLayoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
358         }
359 
360         public void clear() {
361             mItems.clear();
362         }
363 
364         public void addAll(List<BackupItem> items) {
365             mItems.addAll(items);
366             notifyDataSetChanged();
367         }
368 
369         @Override
370         public int getCount() {
371             return mItems.size();
372         }
373 
374         @Override
375         public BackupItem getItem(int position) {
376             return mItems.get(position);
377         }
378 
379         @Override
380         public long getItemId(int position) {
381             return position;
382         }
383 
384         @Override
385         public boolean isEnabled(int position) {
386             return false;
387         }
388 
389         @Override
390         public int getViewTypeCount() {
391             return 3;
392         }
393 
394         @Override
395         public int getItemViewType(int position) {
396             return getItem(position).getViewType();
397         }
398 
399         @Override
400         public View getView(int position, View convertView, ViewGroup parent) {
401             return getItem(position).getView(mLayoutInflater, position, convertView, parent);
402         }
403     }
404 }
405