Majesty - ReDo

2011年8月30日

Majesty

・特定パッケージのリアルタイムインストール検出
・特定パッケージがインストールされているかチェック
・アンインストールへの案内

※apkの配布は一応やめました

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="youten.redo.majesty"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
        android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".Main"
            android:launchMode="singleInstance"
            android:theme="@android:style/Theme.Dialog"
            android:label="@string/app_name">
            <intent-filter>
                <action
                    android:name="android.intent.action.MAIN" />
                <category
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".PackageReceiver">
            <intent-filter>
                <action
                    android:name="android.intent.action.PACKAGE_ADDED" />
                <action
                    android:name="android.intent.action.PACKAGE_INSTALL" />
                <action
                    android:name="android.intent.action.PACKAGE_REPLACED" />
                <data
                    android:scheme="package" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher"
                android:scaleType="center" />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="@string/app_name"
                android:textSize="20dip" />
        </LinearLayout>
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@android:drawable/divider_horizontal_dark"
            android:scaleType="fitXY"
            android:gravity="fill_horizontal"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip" />
        <TextView
            android:id="@+id/msg_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="@string/not_installed"
            android:textSize="14dip" />
        <Button
            android:id="@+id/uninstall_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/uninstall_now" />
    </LinearLayout>
</FrameLayout>

Main.java

package youten.redo.majesty;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity {

    public static final String TARGET_PACKAGE = "xxx.com.application.packagename.xxx";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        boolean isTargetInstalled = false;

        // Installed Package 一覧取得
        PackageManager pm = getPackageManager();

        int flags = 0;
        flags |= PackageManager.GET_INTENT_FILTERS;
        flags |= PackageManager.GET_ACTIVITIES;
        flags |= PackageManager.GET_META_DATA;
        flags |= PackageManager.GET_PROVIDERS;
        flags |= PackageManager.GET_RECEIVERS;
        flags |= PackageManager.GET_SERVICES;
        List<ApplicationInfo> appList = pm.getInstalledApplications(flags);

        for (ApplicationInfo info : appList) {
            if (TARGET_PACKAGE.equals(info.packageName)) {
                // ターゲットパッケージのインストールを確認
                isTargetInstalled = true;
            }
        }

        TextView msgTextView = (TextView) findViewById(R.id.msg_text);
        Button uninstallButton = (Button) findViewById(R.id.uninstall_button);
        if (isTargetInstalled) {
            msgTextView.setText(getString(R.string.installed));
            uninstallButton.setEnabled(true);

            uninstallButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Uri uri = Uri.fromParts("package", TARGET_PACKAGE, null);
                    Intent intent = new Intent(Intent.ACTION_DELETE, uri);
                    startActivity(intent);
                    finish();
                }
            });
        } else {
            msgTextView.setText(getString(R.string.not_installed));
            uninstallButton.setEnabled(false);
        }
    }
}

PackageReceiver.java

package youten.redo.majesty;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class PackageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            String uri = intent.getDataString();
            if (uri != null) {
                if (uri.indexOf(Main.TARGET_PACKAGE) != -1) {
                    Intent main = new Intent();
                    main.setClassName(context, Main.class.getName());
                    main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(main);
                }
            }
        }
    }
}

コメントする