のねのBlog

パソコンの問題や、ソフトウェアの開発で起きた問題など書いていきます。よろしくお願いします^^。

LVL

Googleサンプルソースより
(D:\android-sdk_r24.0.2\sdk\extras\google\play_licensing\sample\src\com\example\android\market\licensing
MainActivity.java

public class MainActivity extends Activity {
    private static final String BASE64_PUBLIC_KEY = "REPLACE THIS WITH YOUR PUBLIC KEY";

    // Generate your own 20 random bytes, and put them here.
    private static final byte[] SALT = new byte[] {
        -46, 65, 30, -128, -103, -57, 74, -64, 51, 88, -95, -45, 77, -117, -36, -113, -11, 32, -64,
        89
    };

    private TextView mStatusText;
    private Button mCheckLicenseButton;

    private LicenseCheckerCallback mLicenseCheckerCallback;
    private LicenseChecker mChecker;
    // A handler on the UI thread.
    private Handler mHandler;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.main);

        mStatusText = (TextView) findViewById(R.id.status_text);
        mCheckLicenseButton = (Button) findViewById(R.id.check_license_button);
        mCheckLicenseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                doCheck();
            }
        });

        mHandler = new Handler();

        // Try to use more data here. ANDROID_ID is a single point of attack.
        // ここでより多くのデータを使用して見てください。
        String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

        // Library calls this when it's done.
        mLicenseCheckerCallback = new MyLicenseCheckerCallback();

        // Construct the LicenseChecker with a policy.
        mChecker = new LicenseChecker(
            this, new ServerManagedPolicy(this,
                new AESObfuscator(SALT, getPackageName(), deviceId)),
            BASE64_PUBLIC_KEY);
        doCheck();
    }

public static final String ANDROID_ID

Added in API level 3
A 64-bit number (as a hex string) that is randomly generated 
when the user first sets up the device 
and should remain constant for the lifetime of the user's device. 
The value may change if a factory reset is performed on the device.

Note: When a device has multiple users 
(available on certain devices running Android 4.2 or higher), 
each user appears as a completely separate device,
 so the ANDROID_ID value is unique to each user.

Constant Value: "android_id"

public static String getString (ContentResolver resolver, String name)

Added in API level 3
Look up a name in the database.

Parameters
resolver	to access the database with
name	to look up in the table
Returns
the corresponding value, or null if not present
    protected Dialog onCreateDialog(int id) {
        final boolean bRetry = id == 1; // リトライをするかどうか

        return new AlertDialog.Builder(this)
            .setTitle(R.string.unlicensed_dialog_title)
            .setMessage(bRetry 
              ? R.string.unlicensed_dialog_retry_body 
              : R.string.unlicensed_dialog_body)

            // 肯定ボタン
            .setPositiveButton(bRetry 
              ? R.string.retry_button 
              : R.string.buy_button, new DialogInterface.OnClickListener() {

                // リトライするかのフラグ
                boolean mRetry = bRetry;

                public void onClick(DialogInterface dialog, int which) {
                    if ( mRetry ) { // リトライする場合 
                        doCheck(); 
                    } else {        // リトライしない場合
                        Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
                                "http://market.android.com/details?id=" + getPackageName()));
                            startActivity(marketIntent);                        
                    }
                }
            })

            // 否定ボタン
            .setNegativeButton(
                R.string.quit_button,
                new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                       finish();
                   }
            }).create();
    }
    private void doCheck() {
        mCheckLicenseButton.setEnabled(false);
        setProgressBarIndeterminateVisibility(true);
        mStatusText.setText(R.string.checking_license);
        mChecker.checkAccess(mLicenseCheckerCallback); // コールバック
    }
    private void displayResult(final String result) {

        mHandler.post(new Runnable() {
            public void run() {
                mStatusText.setText(result);

                setProgressBarIndeterminateVisibility(false);

                mCheckLicenseButton.setEnabled(true);
            }
        });
    }
    private void displayDialog(final boolean showRetry) {

        mHandler.post(new Runnable() {
            public void run() {
                setProgressBarIndeterminateVisibility(false);

                showDialog(showRetry ? 1 : 0);

                mCheckLicenseButton.setEnabled(true);
            }
        });
    }    
    private class MyLicenseCheckerCallback implements LicenseCheckerCallback {

        // 許可
        public void allow(int policyReason) {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                // アクティビティーが終えている場合 UIを更新しないでください。
                return;
            }

            // Should allow user access.
            displayResult(getString(R.string.allow));
        }

        // 不許可
        public void dontAllow(int policyReason) {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                // アクティビティーが終えている場合 UIを更新しないでください。
                return;
            }

            displayResult(getString(R.string.dont_allow));
            displayDialog(policyReason == Policy.RETRY);
        }

        // エラー
        public void applicationError(int errorCode) {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                // アクティビティーが終えている場合 UIを更新しないでください。
                return;
            }

            String result = String.format(getString(R.string.application_error), errorCode);
            displayResult(result);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mChecker.onDestroy();
    }
}