check Backup Agent - Android App

Android examples for App:Package

Description

check Backup Agent

Demo Code

/*//  w  ww  .ja  v a 2s  . c om
 * The MIT License
 *
 * Copyright 2014 Kiyofumi Kondoh
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
//package com.java2s;

import android.content.Context;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

public class Main {
    private static final String TAG = "kk-Backup-Helper";
    private static Context mContext = null;

    private static void checkBackupAgent() {
        if (null == mContext) {
            return;
        }

        boolean haveBackupAgent = false;
        {
            final ApplicationInfo appInfo = mContext.getApplicationInfo();
            final String backupAgentName = appInfo.backupAgentName; /* API-8 */
            Log.d(TAG, "backupAgentName=" + backupAgentName);
            if (null != appInfo.backupAgentName) {
                {
                    Class<?> clazz = null;
                    try {
                        clazz = Class.forName(appInfo.backupAgentName);
                        haveBackupAgent = true;
                    } catch (ClassNotFoundException ex) {
                        Log.d(TAG, "" + ex);
                    }
                    if (false == haveBackupAgent) {
                        try {
                            if (appInfo.backupAgentName.startsWith(".")) {
                                clazz = Class.forName(appInfo.packageName
                                        + appInfo.backupAgentName);
                            } else {
                                clazz = Class.forName(appInfo.packageName
                                        + "." + appInfo.backupAgentName);
                            }
                            haveBackupAgent = true;
                        } catch (ClassNotFoundException ex) {
                            Log.d(TAG, "" + ex);
                        }
                    }
                }
            }
        }

        if (false == haveBackupAgent) {
            final ApplicationInfo appInfoFromPM = getApplicationInfoFromPackageManager(0);
            if (null != appInfoFromPM) {
                Log.d(TAG, "backupAgentName from PackageManager="
                        + appInfoFromPM.backupAgentName);
                if (null != appInfoFromPM.backupAgentName) {
                    {
                        Class<?> clazz = null;
                        try {
                            clazz = Class
                                    .forName(appInfoFromPM.backupAgentName);
                            haveBackupAgent = true;
                        } catch (ClassNotFoundException ex) {
                            Log.d(TAG, "" + ex);
                        }
                        if (false == haveBackupAgent) {
                            try {
                                if (appInfoFromPM.backupAgentName
                                        .startsWith(".")) {
                                    clazz = Class
                                            .forName(appInfoFromPM.packageName
                                                    + appInfoFromPM.backupAgentName);
                                } else {
                                    clazz = Class
                                            .forName(appInfoFromPM.packageName
                                                    + "."
                                                    + appInfoFromPM.backupAgentName);
                                }
                                haveBackupAgent = true;
                            } catch (ClassNotFoundException ex) {
                                Log.d(TAG, "" + ex);
                            }
                        }
                    }
                }
            }
        }

        if (false == haveBackupAgent) {
            Log.e(TAG,
                    "AndroidManifest.xml doesn't contain\n"
                            + "<application android:backupAgent=\"xxxxx\"/>\n"
                            + "format of class-name similar to '<application><activity android:name>'");
        }

    }

    private static ApplicationInfo getApplicationInfoFromPackageManager(
            final int flags) {
        assert null != mContext;

        if (null == mContext) {
            return null;
        }

        final PackageManager pm = mContext.getPackageManager();
        if (null == pm) {
            return null;
        }

        ApplicationInfo appInfo = null;
        try {
            appInfo = pm.getApplicationInfo(mContext.getPackageName(),
                    flags);
        } catch (PackageManager.NameNotFoundException e) {

        }

        return appInfo;
    }
}

Related Tutorials