check Backup Api Key - Android App

Android examples for App:Package

Description

check Backup Api Key

Demo Code

/*/*from   w w  w  .j a  v a2  s  . 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 final String ANDROID_BACKUP_APIKEY = "com.google.android.backup.api_key";
    private static Context mContext = null;

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

        boolean haveBackupApiKey = false;
        {
            final ApplicationInfo appInfo = mContext.getApplicationInfo();
            if (null != appInfo) {
                if (null != appInfo.metaData) {
                    if (appInfo.metaData.containsKey(ANDROID_BACKUP_APIKEY)) {
                        final String backupApiKey = appInfo.metaData
                                .getString(ANDROID_BACKUP_APIKEY);
                        Log.d(TAG, "backupApiKey from Context="
                                + backupApiKey);
                        haveBackupApiKey = true;
                    } else {
                        Log.d(TAG, "metaData from Context not contain key="
                                + ANDROID_BACKUP_APIKEY);
                    }
                }
            }
        }

        if (false == haveBackupApiKey) {
            final ApplicationInfo appInfoFromPM = getApplicationInfoFromPackageManager(PackageManager.GET_META_DATA);
            if (null != appInfoFromPM) {
                Log.d(TAG, "metaData from PakcageManager="
                        + appInfoFromPM.metaData);
                if (null != appInfoFromPM.metaData) {
                    if (appInfoFromPM.metaData
                            .containsKey(ANDROID_BACKUP_APIKEY)) {
                        final String backupApiKey = appInfoFromPM.metaData
                                .getString(ANDROID_BACKUP_APIKEY);
                        Log.d(TAG, "backupApiKey from PackageManager="
                                + backupApiKey);
                        haveBackupApiKey = true;
                    } else {
                        Log.d(TAG,
                                "metaData from PackageManager not contain key="
                                        + ANDROID_BACKUP_APIKEY);
                    }
                }
            }

        }

        if (false == haveBackupApiKey) {
            Log.e(TAG, "AndroidManifest.xml doesn't contain\n"
                    + "<application><meta-data android:name=\""
                    + ANDROID_BACKUP_APIKEY
                    + "\" android:value=\"xxxxxx\" /></application>");
            Log.e(TAG,
                    "register url http://code.google.com/android/backup/signup.html");
        }

    }

    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