• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 package com.android.managedprovisioning.task;
17 
18 import android.content.Context;
19 import android.os.FileUtils;
20 
21 import com.android.internal.annotations.VisibleForTesting;
22 import com.android.managedprovisioning.analytics.MetricsWriterFactory;
23 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
24 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
25 import com.android.managedprovisioning.common.ProvisionLogger;
26 import com.android.managedprovisioning.common.SettingsFacade;
27 import com.android.managedprovisioning.task.nonrequiredapps.SystemAppsSnapshot;
28 
29 import java.io.File;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32 
33 public class MigrateSystemAppsSnapshotTask extends AbstractProvisioningTask {
34     private static final Pattern XML_FILE_NAME_PATTERN = Pattern.compile("(\\d+)\\.xml");
35 
MigrateSystemAppsSnapshotTask(Context context, Callback callback)36     public MigrateSystemAppsSnapshotTask(Context context, Callback callback) {
37         this(context, callback,
38                 new ProvisioningAnalyticsTracker(
39                         MetricsWriterFactory.getMetricsWriter(context, new SettingsFacade()),
40                         new ManagedProvisioningSharedPreferences(context)));
41     }
42 
43     @VisibleForTesting
MigrateSystemAppsSnapshotTask(Context context, Callback callback, ProvisioningAnalyticsTracker provisioningAnalyticsTracker)44     public MigrateSystemAppsSnapshotTask(Context context, Callback callback,
45             ProvisioningAnalyticsTracker provisioningAnalyticsTracker) {
46         super(context, null, callback, provisioningAnalyticsTracker);
47     }
48 
49     @Override
run(int userId)50     public void run(int userId) {
51         migrateIfNecessary();
52     }
53 
54     /**
55      * Snapshot files are renamed from {user_id}.xml to {user_serial_number}.xml and moved
56      * to the new folder.
57      */
migrateIfNecessary()58     private void migrateIfNecessary() {
59         File legacyFolder = SystemAppsSnapshot.getLegacyFolder(mContext);
60         if (!legacyFolder.exists()) {
61             return;
62         }
63         ProvisionLogger.logi("Found legacy system_apps folder, kick start migration.");
64         SystemAppsSnapshot.getFolder(mContext).mkdirs();
65         File[] files = legacyFolder.listFiles();
66         for (File file : files) {
67             String fileName = file.getName();
68             Matcher matcher = XML_FILE_NAME_PATTERN.matcher(fileName);
69             if (!matcher.find()) {
70                 ProvisionLogger.logw("Found invalid file during migration: " + fileName);
71                 continue;
72             }
73 
74             int userId = Integer.parseInt(matcher.group(1));
75             File destination;
76             try {
77                 destination = SystemAppsSnapshot.getSystemAppsFile(mContext, userId);
78             } catch (IllegalArgumentException ex) {
79                 ProvisionLogger.logi(
80                         "user " + userId + " no longer exists, skip migrating its snapshot file");
81                 continue;
82             }
83             ProvisionLogger.logi(
84                     "Moving " + file.getAbsolutePath() + " to " + destination.getAbsolutePath());
85             boolean success = file.renameTo(destination);
86             if (!success) {
87                 ProvisionLogger.loge("Failed to migrate " + file.getAbsolutePath());
88             }
89         }
90         FileUtils.deleteContentsAndDir(legacyFolder);
91     }
92 
93     @Override
getStatusMsgId()94     public int getStatusMsgId() {
95         // OTA only task, not used.
96         return 0;
97     }
98 }
99