/*****************************************************************
BioZen
Copyright (C) 2011 The National Center for Telehealth and
Technology
Eclipse Public License 1.0 (EPL-1.0)
This library is free software; you can redistribute it and/or
modify it under the terms of the Eclipse Public License as
published by the Free Software Foundation, version 1.0 of the
License.
The Eclipse Public License is a reciprocal license, under
Section 3. REQUIREMENTS iv) states that source code for the
Program is available from such Contributor, and informs licensees
how to obtain it in a reasonable manner on or through a medium
customarily used for software exchange.
Post your updates and modifications to our GitHub or email to
t2@tee2.org.
This library is distributed WITHOUT ANY WARRANTY; without
the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the Eclipse Public License 1.0 (EPL-1.0)
for more details.
You should have received a copy of the Eclipse Public License
along with this library; if not,
visit http://www.opensource.org/licenses/EPL-1.0
*****************************************************************/
package com.t2.compassionMeditation;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
//Need the following import to get access to the app resources, since this
//class is in a sub-package.
import com.t2.R;
/**
* Displays an EULA ("End User License Agreement") that the user has to accept before
* using the application. Your application should call {@link Eula#show(android.app.Activity)}
* in the onCreate() method of the first activity. If the user accepts the EULA, it will never
* be shown again. If the user refuses, {@link android.app.Activity#finish()} is invoked
* on your activity.
* @see http://code.google.com/p/apps-for-android/source/browse/trunk/Photostream/src/com/google/android/photostream/Eula.java
*/
public class Eula {
private static final String PREFERENCE_EULA_ACCEPTED = "eula.accepted";
private static final String PREFERENCES_EULA = "eula";
/**
* callback to let the activity know when the user has accepted the EULA.
*/
static interface OnEulaAgreedTo {
/**
* Called when the user has accepted the eula and the dialog closes.
*/
void onEulaAgreedTo();
}
/**
* Displays the EULA if necessary. This method should be called from the onCreate()
* method of your main Activity.
*
* @param activity The Activity to finish if the user rejects the EULA.
* @return Whether the user has agreed already.
*/
public static boolean show(final Activity activity) {
final SharedPreferences preferences = activity.getSharedPreferences(
PREFERENCES_EULA,
Activity.MODE_PRIVATE
);
// preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, false).commit();
if(!preferences.getBoolean(PREFERENCE_EULA_ACCEPTED, false)) {
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle(R.string.eula_title);
alert.setCancelable(true);
alert.setPositiveButton(R.string.eula_accept, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
accept(preferences);
if (activity instanceof OnEulaAgreedTo) {
((OnEulaAgreedTo) activity).onEulaAgreedTo();
}
}
});
alert.setNegativeButton(R.string.eula_refuse, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
refuse(activity);
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
refuse(activity);
}
});
alert.setMessage(readEula(activity));
alert.show();
return false;
}
return true;
}
private static void accept(SharedPreferences preferences) {
preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, true).commit();
}
private static void refuse(Activity activity) {
activity.finish();
}
private static CharSequence readEula(Activity activity) {
return activity.getString(R.string.eula_text);
}
}