Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f3e1f66a authored by jinfaw's avatar jinfaw Committed by Linux Build Service Account
Browse files

Add regionalization environment to support carrier switching

1. Add RegionalizationService to SystemServer for accessing
   files for carrier

2. Add RegionalizationEnvironment to load path of carrier package

Change-Id: I4e3e6c2606d3fbebd42952eabac318797f1d8b0b
CRs-Fixed: 1025803
parent 5510923a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -325,6 +325,7 @@ LOCAL_SRC_FILES += \
	core/java/com/android/internal/policy/IShortcutService.aidl \
	core/java/com/android/internal/os/IDropBoxManagerService.aidl \
	core/java/com/android/internal/os/IParcelFileDescriptorFactory.aidl \
	core/java/com/android/internal/os/IRegionalizationService.aidl \
	core/java/com/android/internal/os/IResultReceiver.aidl \
	core/java/com/android/internal/statusbar/IStatusBar.aidl \
	core/java/com/android/internal/statusbar/IStatusBarService.aidl \
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.internal.os;

/**
 * Direct interface to the RegionalizationService's functionality
 *
 * {@hide}
 */
interface IRegionalizationService {

    boolean checkFileExists(String filepath);

    List<String> readFile(String filepath, String regularExpression);

    boolean writeFile(String filepath, String content, boolean append);

    void deleteFilesUnderDir(String dirPath, String ext, boolean delDir);
}
+279 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.internal.os;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.util.Log;

public class RegionalizationEnvironment {
    private final static String TAG = "RegionalizationEnvironment";

    private final static boolean SUPPORTED = SystemProperties.getBoolean(
            "ro.regionalization.support", false);
    private final static boolean DEBUG = true;

    private static IRegionalizationService mRegionalizationService = null;

    private final static String SPEC_FILE_PATH = "/persist/speccfg/spec";

    private static ArrayList<Package> mPackages = new ArrayList<Package>();
    private static ArrayList<String> mExcludedApps = new ArrayList<String>();

    private static boolean isLoaded = false;
    private static void init() {
        IBinder iBinder = ServiceManager.getService("regionalization");
        mRegionalizationService = IRegionalizationService.Stub.asInterface(iBinder);
        if (mRegionalizationService != null) {
            loadSwitchedPackages();
            loadExcludedApplist();
            isLoaded = true;
        }
    }

    /**
     * {@hide}
     */
    public static boolean isSupported() {
        if (SUPPORTED && !isLoaded) {
            init();
        }
        return SUPPORTED;
    }

    /**
     * {@hide}
     */
    public static int getPackagesCount() {
        return mPackages.size();
    }

    /**
     * {@hide}
     */
    public static List<String> getAllPackageNames() {
        ArrayList<String> packages = new ArrayList<String>();
        for (Package p : mPackages) {
            packages.add(p.getName());
        }
        return packages;
    }

    /**
     * {@hide}
     */
    public static List<File> getAllPackageDirectories() {
        ArrayList<File> directories = new ArrayList<File>();
        for (Package p : mPackages) {
            if (DEBUG)
                Log.v(TAG, "Package Directoriy(" + p.getPriority() + "):" + p.getDirectory());
            directories.add(p.getDirectory());
        }
        return directories;
    }

    /**
     * {@hide}
     */
    public static boolean isExcludedApp(String appName) {
        if (getPackagesCount() == 0) {
            return false;
        }

        if (!appName.endsWith(".apk")) {
            return mExcludedApps.contains(appName + ".apk");
        } else {
            return mExcludedApps.contains(appName);
        }
    }

    /**
     * {@hide}
     */
    public static IRegionalizationService getRegionalizationService() {
        return mRegionalizationService;
    }

    /**
     * {@hide}
     */
    public static String getStoragePos() {
        for (Package pack: mPackages) {
            String pos = pack.getStoragePos();
            if (!TextUtils.isEmpty(pos))
                return pos;
        }
        try {
            mPackages.clear();
            throw new IOException("Read wrong package for Carrier!");
        } catch (IOException e) {
            Log.e(TAG, "Get storage pos error, caused by: " + e.getMessage());
        }
        return "";
    }

    private static void loadSwitchedPackages() {
        if (DEBUG)
            Log.d(TAG, "load packages for Carrier!");

        try {
            ArrayList<String> contents = null;
            try {
                contents = (ArrayList<String>)
                        mRegionalizationService.readFile(SPEC_FILE_PATH, null);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

            if (contents != null && contents.size() > 0) {
                // Get storage pos of carrier pack
                if (!contents.get(0).startsWith("packStorage=")) {
                    throw new IOException("Can't read storage pos for Carrier package!");
                }
                String storagePos = contents.get(0).substring("packStorage=".length());
                if (TextUtils.isEmpty(storagePos)) {
                    throw new IOException("Storage pos for Carrier package is wrong!");
                }

                // Get carrier pack count
                String packNumRegularExpresstion = "^packCount=[0-9]$";
                if (!contents.get(1).matches(packNumRegularExpresstion)) {
                    throw new IOException("Can't read package count of Carrier!");
                }
                int packNum = Integer.parseInt(contents.get(1)
                        .substring("packCount=".length()));
                if (packNum <= 0 || contents.size() <= packNum) {
                    throw new IOException("Package count of Carrier is wrong!");
                }

                for (int i = 2; i < packNum + 2; i++) {
                    String packRegularExpresstion = "^strSpec[0-9]=\\w+$";
                    if (contents.get(i).matches(packRegularExpresstion)) {
                        String packName = contents.get(i).substring("strSpec".length() + 2);
                        if (!TextUtils.isEmpty(packName)) {
                            boolean exists = false;
                            try {
                                exists = mRegionalizationService.checkFileExists(
                                    storagePos + "/" + packName);
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }

                            if (exists) {
                                mPackages.add(new Package(packName, i, storagePos));
                            } else {
                                mPackages.clear();
                                throw new IOException("Read wrong packages for Carrier!");
                            }
                        }
                    } else {
                        mPackages.clear();
                        throw new IOException("Read wrong packages for Carrier!");
                    }
                }
            }
        } catch (IOException e) {
            Log.e(TAG, "Load package for carrier error, caused by: " + e.getMessage());
        }
    }

    private static void loadExcludedApplist() {
        if (DEBUG)
            Log.d(TAG, "loadExcludedApps!");

        if (getPackagesCount() == 0) return;

        for (Package pack : mPackages) {
            Log.d(TAG, "load excluded apps for " + pack.getDirectory());
            String excListFilePath = pack.getExcludedListFilePath();
            ArrayList<String> contents = null;
            try {
                contents = (ArrayList<String>)
                        mRegionalizationService.readFile(excListFilePath, null);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            if (contents != null && contents.size() > 0) {
                for (String content : contents) {
                    if (!TextUtils.isEmpty(content)) {
                        int pos = content.lastIndexOf("/");
                        if (pos != -1) {
                            String apkName = content.substring(pos + 1);
                            if (!TextUtils.isEmpty(apkName) && !mExcludedApps.contains(apkName)) {
                                mExcludedApps.add(apkName);
                            }
                        }
                    }
                }
            }
        }
    }

    private static class Package {
        private final String mName;
        private final int mPriority;
        private final String mStorage;

        public Package(String name, int priority, String storage) {
            mName = name;
            mPriority = priority;
            mStorage = storage;
        }

        public String getName() {
            return mName;
        }

        public int getPriority() {
            return mPriority;
        }

        public String getStoragePos() {
            return mStorage;
        }

        public File getDirectory() {
            return new File(mStorage, mName);
        }

        public String getExcludedListFilePath() {
            return getDirectory().getAbsolutePath() + "/exclude.list";
        }
    }

}
+155 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.server.os;

import android.text.TextUtils;
import android.util.Log;

import com.android.internal.os.IRegionalizationService;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * The implementation of the regionalization service interface.
 *
 * @hide
 */
public class RegionalizationService extends IRegionalizationService.Stub {

    private static final String TAG = "RegionalizationService";

    public RegionalizationService() {
    }

    @Override
    public boolean checkFileExists(String filepath) {
        File file = new File(filepath);
        if (file == null || !file.exists())
            return false;

        return true;
    }

    @Override
    public ArrayList<String> readFile(String filepath, String regularExpression) {
        File file = new File(filepath);
        if (file == null || !file.exists() || !file.canRead()) return null;


        ArrayList<String> contents = new ArrayList<String>();

        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            String line = null;
            while ((line = br.readLine()) != null && (line = line.trim()) != null) {
                if (!TextUtils.isEmpty(regularExpression)) {
                    if (line.matches(regularExpression)) {
                        contents.add(line);
                    }
                } else {
                    contents.add(line);
                }
            }
        } catch (IOException e) {
            Log.e(TAG, "Read File error, caused by: " + e.getMessage());
        } finally {
            try {
                if (br != null) br.close();
                if (fr != null) fr.close();
            } catch (IOException e) {
                Log.e(TAG, "Close the reader error, caused by: " + e.getMessage());
            }
        }

        return contents;
    }

    @Override
    public boolean writeFile(String filepath, String content, boolean append) {
        File file = new File(filepath);
        if (file == null || !file.exists() || !file.canWrite()) return false;

        if (TextUtils.isEmpty(content)) return false;

        // Write the file with the content.
        FileWriter fw = null;
        try {
            fw = new FileWriter(file, append);
            fw.write(content);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (fw != null) try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }

        return true;
    }

    @Override
    public void deleteFilesUnderDir(String dirPath, String ext, boolean delDir) {
        File file = new File(dirPath);
        if (file == null || !file.exists()) return;

        deleteFiles(file, ext, delDir);
    }

    // Delete all files under this folder and its subfolders
    private void deleteFiles(File dir, String ext, boolean delDir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            if (children == null) return;

            for (int i = 0; i < children.length; i++) {
                deleteFiles(new File(dir, children[i]), ext, delDir);
            }
            if (delDir) {
                dir.delete();
            }
        } else {
            if (dir.isFile() && (ext.isEmpty() || dir.getName().endsWith(ext))) {
                dir.delete();
            }
        }
    }
}
+8 −0
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import android.view.WindowManager;

import com.android.internal.R;
import com.android.internal.os.BinderInternal;
import com.android.internal.os.RegionalizationEnvironment;
import com.android.internal.os.SamplingProfilerIntegration;
import com.android.internal.os.ZygoteInit;
import com.android.internal.widget.ILockSettings;
@@ -75,6 +76,7 @@ import com.android.server.media.projection.MediaProjectionManagerService;
import com.android.server.net.NetworkPolicyManagerService;
import com.android.server.net.NetworkStatsService;
import com.android.server.notification.NotificationManagerService;
import com.android.server.os.RegionalizationService;
import com.android.server.os.SchedulingPolicyService;
import com.android.server.pm.BackgroundDexOptService;
import com.android.server.pm.Installer;
@@ -447,6 +449,12 @@ public final class SystemServer {
            mOnlyCore = true;
        }

        if (RegionalizationEnvironment.isSupported()) {
            Slog.i(TAG, "Regionalization Service");
            RegionalizationService regionalizationService = new RegionalizationService();
            ServiceManager.addService("regionalization", regionalizationService);
        }

        // Start the package manager.
        traceBeginAndSlog("StartPackageManagerService");
        mPackageManagerService = PackageManagerService.main(mSystemContext, installer,