Итак, в Google Play у вас есть крутое приложение, которое купит у вас целая куча народа. И вы хотите встроить проверку лицензии на него. Кстати, если вы разместили в маркете бесплатное приложение, платным его сделать уже не получится.
Наше лицензируемое приложение уже должно лежать в Google Developer Console. С новой политикой Google каждое приложение имеет свой уникальный ключ. Щелкаем по приложению, выбираем в левом меню "Службы и API" и копируем RSA ключ:
Необходимо подготовить аккаунт к тестированию, для этого заходим через левое меню в Настройки - Сведения об аккаунте, добавляем свой e-mail в Аккаунты GMail разработчиков (соответственно этот аккаунт должен у нас быть забит в настройках Вашего устройства). Выбирая в ComboBox "Информация о лицензии" требуемый ответ, можно протестировать реакцию приложения. А вот и картинка этого окна:
В Eclipse заходим в Window - Android SDK Manager, секция Extras и скачиваем Google Play Licensing Library (на картинке - в красной рамке):
Теперь подключаем библиотеку к нашему проекту. Для этого жмем правой кнопкой в Package Explorer, далее Import, далее Android - Existing Android Code Into Workspace, находим нашу библиотеку в директории <путь до android sdk>/google-market_licensing. Импортируем.
В нашем проекте заходим в свойства проекта, секция Android, нижняя вкладка Library. Подключаем библиотеку к проекту:
Вроде всё готово.
В нашем приложении в Android Manifest.xml в корень manifest добавляем всего одну строчку:
Заодно исправим главный layout в res/layout/. Вот его полный код:
import com.google.android.vending.licensing.AESObfuscator;
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.ServerManagedPolicy;
И следующие члены класса:
1. Настраиваем консоль разработчика
Наше лицензируемое приложение уже должно лежать в Google Developer Console. С новой политикой Google каждое приложение имеет свой уникальный ключ. Щелкаем по приложению, выбираем в левом меню "Службы и API" и копируем RSA ключ:
Необходимо подготовить аккаунт к тестированию, для этого заходим через левое меню в Настройки - Сведения об аккаунте, добавляем свой e-mail в Аккаунты GMail разработчиков (соответственно этот аккаунт должен у нас быть забит в настройках Вашего устройства). Выбирая в ComboBox "Информация о лицензии" требуемый ответ, можно протестировать реакцию приложения. А вот и картинка этого окна:
2. Настраиваем Eclipse
В Eclipse заходим в Window - Android SDK Manager, секция Extras и скачиваем Google Play Licensing Library (на картинке - в красной рамке):
Теперь подключаем библиотеку к нашему проекту. Для этого жмем правой кнопкой в Package Explorer, далее Import, далее Android - Existing Android Code Into Workspace, находим нашу библиотеку в директории <путь до android sdk>/google-market_licensing. Импортируем.
3. Создаем проект Eclipse
В нашем проекте заходим в свойства проекта, секция Android, нижняя вкладка Library. Подключаем библиотеку к проекту:
Вроде всё готово.
В нашем приложении в Android Manifest.xml в корень manifest добавляем всего одну строчку:
Заодно исправим главный layout в res/layout/. Вот его полный код:
В главной активити приложения добавляем импорты:
import com.google.android.vending.licensing.AESObfuscator;
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.ServerManagedPolicy;
И следующие члены класса:
// Поставьте сюда свой RSA ключ из Developer Console
private static final String BASE64_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFveIcGxnHJQfSUs6S5eoWwYr87LfNBupk9G0rN1QZ3YiVYzp/LeJ7fNkDjC1f8RHMeJF3ZA9VjLH5d4TUmQ3M4e6/vVrNFga+BXEbAmhsv6aQ1fNzt5tBWFYUdGlhHTcfsTiFPDh17ejlfm7XlhxWuYNLuxJtzXpwdiqTqiTZed0mFut1Z1khL+34SXL4qDzegbkSdxrka/zyLnuS5dSyacszmyST7x+/NjWgg/9zlu+FRETXl+XYO2STb6RuVVgLaQIDAQAB";
// Забейте сюда какие-нибудь числа. Свои, иначе ничего не заработает!
private static final byte[] SALT = new byte[] { -46, 65, 30, -19, -103, -5, 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;
// Хэндлер для процесса UI.
private Handler mHandler;
Метод onCreate с комментариями:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
// получаем компоненты layouta, по кнопке запускаем повторную проверку лицензии
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();
// Попробуйте использовать здесь другие данные для ID устройства. ANDROID_ID будет взламываться в первую очередь.
String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
// Коллбэк функция по окончании проверки лицензии
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
// конструктор LicenseChecker с правами.
mChecker = new LicenseChecker(
this, new ServerManagedPolicy(this,
new AESObfuscator(SALT, getPackageName(), deviceId)),
BASE64_PUBLIC_KEY);
// запускаем проверку
doCheck();
}
// в методе doCheck() готовим лэйаут к отсылке запроса
private void doCheck() {
mCheckLicenseButton.setEnabled(false);
setProgressBarIndeterminateVisibility(true);
mStatusText.setText(R.string.checking_license);
mChecker.checkAccess(mLicenseCheckerCallback);
}
Описываем класс MyLicenseChecker, который служит для обработки ответов от сервера:
private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
@Override
public void allow(int reason) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
// Should allow user access.
displayResult("Allow");
}
@Override
public void dontAllow(int reason) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
displayResult("DONT ALLOW");
// Should not allow access. In most cases, the app should assume
// the user has access unless it encounters this. If it does,
// the app should inform the user of their unlicensed ways
// and then either shut down the app or limit the user to a
// restricted set of features.
// In this example, we show a dialog that takes the user to Market.
showDialog(0);
}
@Override
public void applicationError(int errorCode) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
// This is a polite way of saying the developer made a mistake
// while setting up or calling the license checker library.
// Please examine the error code and fix the error.
String result = String.format("ERROR", errorCode);
displayResult(result);
}
}
А также несколько функций чтобы просто показать диалог:
protected Dialog onCreateDialog(int id) {
// We have only one dialog.
return new AlertDialog.Builder(this)
.setTitle("UNLICENSED")
.setMessage("UNLICENSED DIALOG BODY")
.setPositiveButton("BUY BUTTON", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"http://market.android.com/details?id=" + getPackageName()));
startActivity(marketIntent);
}
})
.setNegativeButton("QUIT BUTTON", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.create();
}
private void displayResult(final String result) {
mHandler.post(new Runnable() {
public void run() {
mStatusText.setText(result);
setProgressBarIndeterminateVisibility(false);
mCheckLicenseButton.setEnabled(true);
}
});
}
Собственно и всё. Запуская приложение и изменяя требуемый ответ через Developer Console(Информация о лицензии), получаем требуемые данные
P.S.: Если в консоли видим, что типа Using cached license response, меняйте SALT.
Полный код активити:
package com.osmsoft.licensingexample;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings.Secure;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import com.android.vending.licensing.AESObfuscator;
import com.android.vending.licensing.LicenseChecker;
import com.android.vending.licensing.LicenseCheckerCallback;
import com.android.vending.licensing.ServerManagedPolicy;
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();
}
protected Dialog onCreateDialog(int id) {
// We have only one dialog.
return new AlertDialog.Builder(this)
.setTitle("UNLICENSED")
.setMessage("UNLICENSED DIALOG BODY")
.setPositiveButton("BUY BUTTON", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"http://market.android.com/details?id=" + getPackageName()));
startActivity(marketIntent);
}
})
.setNegativeButton("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 class MyLicenseCheckerCallback implements LicenseCheckerCallback {
@Override
public void allow(int reason) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
// Should allow user access.
displayResult("Allow");
}
@Override
public void dontAllow(int reason) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
displayResult("DONT ALLOW");
// Should not allow access. In most cases, the app should assume
// the user has access unless it encounters this. If it does,
// the app should inform the user of their unlicensed ways
// and then either shut down the app or limit the user to a
// restricted set of features.
// In this example, we show a dialog that takes the user to Market.
showDialog(0);
}
@Override
public void applicationError(int errorCode) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
// This is a polite way of saying the developer made a mistake
// while setting up or calling the license checker library.
// Please examine the error code and fix the error.
String result = String.format("ERROR", errorCode);
displayResult(result);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mChecker.onDestroy();
}
}




Комментариев нет:
Отправить комментарий