• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.tests.rollback.testapp;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.res.Resources;
23 
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.util.Scanner;
29 
30 /**
31  * A broadcast reciever to check for and update user app data version
32  * compatibility.
33  */
34 public class ProcessUserData extends BroadcastReceiver {
35 
36     private static final String TAG = "RollbackTestApp";
37 
38     /**
39      * Exception thrown in case of issue with user data.
40      */
41     public static class UserDataException extends Exception {
UserDataException(String message)42         public UserDataException(String message) {
43             super(message);
44         }
45 
UserDataException(String message, Throwable cause)46         public UserDataException(String message, Throwable cause) {
47            super(message, cause);
48         }
49     }
50 
51     @Override
onReceive(Context context, Intent intent)52     public void onReceive(Context context, Intent intent) {
53         try {
54             processUserData(context);
55             setResultCode(1);
56         } catch (UserDataException e) {
57             setResultCode(0);
58             setResultData(e.getMessage());
59         }
60     }
61 
62     /**
63      * Update the app's user data version to match the app version.
64      *
65      * @param context The application context.
66      * @throws UserDataException in case of problems with app user data.
67      */
processUserData(Context context)68     public void processUserData(Context context) throws UserDataException {
69         Resources res = context.getResources();
70         String packageName = context.getPackageName();
71 
72         int appVersionId = res.getIdentifier("app_version", "integer", packageName);
73         int appVersion = res.getInteger(appVersionId);
74 
75         int splitVersionId = res.getIdentifier("split_version", "integer", packageName);
76         int splitVersion = res.getInteger(splitVersionId);
77 
78         // Make sure the app version and split versions are compatible.
79         if (appVersion != splitVersion) {
80             throw new UserDataException("Split version " + splitVersion
81                     + " does not match app version " + appVersion);
82         }
83 
84         // Read the version of the app's user data and ensure it is compatible
85         // with our version of the application.
86         File versionFile = new File(context.getFilesDir(), "version.txt");
87         try {
88             Scanner s = new Scanner(versionFile);
89             int userDataVersion = s.nextInt();
90             s.close();
91 
92             if (userDataVersion > appVersion) {
93                 throw new UserDataException("User data is from version " + userDataVersion
94                         + ", which is not compatible with this version " + appVersion
95                         + " of the RollbackTestApp");
96             }
97         } catch (FileNotFoundException e) {
98             // No problem. This is a fresh install of the app or the user data
99             // has been wiped.
100         }
101 
102         // Record the current version of the app in the user data.
103         try {
104             PrintWriter pw = new PrintWriter(versionFile);
105             pw.println(appVersion);
106             pw.close();
107         } catch (IOException e) {
108             throw new UserDataException("Unable to write user data.", e);
109         }
110     }
111 }
112