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