If you think the Android project mobilepower-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2013 Dario Scoppelletti, <http://www.scoppelletti.it/>.
* //fromwww.java2s.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package it.scoppelletti.mobilepower.security;
import com.google.android.vending.licensing.*;
import org.slf4j.*;
/**
* Client del servizio di verifica delle licenze.
*
* @since 1.0
*/publicabstractclass LicenseClient implements LicenseCheckerCallback {
/**
* Risultato positivo.
*/publicstaticfinalint RESULT_POSITIVE = 1;
/**
* Risultato negativo.
*/publicstaticfinalint RESULT_NEGATIVE = 2;
/**
* Risultato indeterminato (la verifica andrebbe ritentata).
*/publicstaticfinalint RESULT_RETRY = 3;
privatestaticfinal Logger myLogger = LoggerFactory.getLogger(
"LicenseClient");
private LicenseChecker myService;
/**
* Costruttore.
*
* @param licenseChecker Client del servizio di verifica delle licenze.
*/protected LicenseClient(LicenseChecker licenseChecker) {
if (licenseChecker == null) {
thrownew NullPointerException("Argument licenseChecker is null.");
}
myService = licenseChecker;
}
/**
* Rilascia le risorse.
*/publicfinalvoid onDestroy() {
if (myService != null) {
myService.onDestroy();
myService = null;
}
}
/**
* Esegue la verifica della licenza.
*/publicvoid check() {
if (myService == null) {
thrownew IllegalStateException(String.format(
"Object %1$s destroyed.", this));
}
myService.checkAccess(this);
}
/**
* Gestisce il risultato della verifica di una licenza.
*
* @param result Risultato.
*/protectedabstractvoid onLicenseResult(int result);
/**
* Gestisce il risultato positivo della verifica della licenza.
*
* @param reason Risultato.
*/publicfinalvoid allow(int reason) {
myLogger.debug("reason={}", reason);
onLicenseResult(LicenseClient.RESULT_POSITIVE);
}
/**
* Gestisce il risultato negativo della verifica della licenza.
*
* @param reason Risultato.
*/publicfinalvoid dontAllow(int reason) {
myLogger.warn("reason={}.", reason);
onLicenseResult((reason == Policy.RETRY) ? LicenseClient.RESULT_RETRY :
LicenseClient.RESULT_NEGATIVE);
}
/**
* Gestisce un errore del sistema.
*
* @param errorCode Codice dell’errore.
*/publicfinalvoid applicationError(int errorCode) {
thrownew RuntimeException(String.format("Internal error %1$d.",
errorCode));
}
}