merge in OI distribution
[android_pandora.git] / apps / oi-filemanager / FileManager / src / org / openintents / filemanager / EulaOrNewVersion.java
diff --git a/apps/oi-filemanager/FileManager/src/org/openintents/filemanager/EulaOrNewVersion.java b/apps/oi-filemanager/FileManager/src/org/openintents/filemanager/EulaOrNewVersion.java
new file mode 100644 (file)
index 0000000..06cba6e
--- /dev/null
@@ -0,0 +1,131 @@
+/* \r
+ * Copyright (C) 2007-2011 OpenIntents.org\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.openintents.filemanager;\r
+\r
+import org.openintents.util.VersionUtils;\r
+\r
+import android.app.Activity;\r
+import android.content.ComponentName;\r
+import android.content.Context;\r
+import android.content.Intent;\r
+import android.content.SharedPreferences;\r
+import android.preference.PreferenceManager;\r
+import android.util.Log;\r
+\r
+\r
+\r
+/**\r
+ * Displays the Eula for the first time, reading it from a raw resource.\r
+ * \r
+ * @author Peli\r
+ *\r
+ */\r
+public class EulaOrNewVersion {\r
+       /** TAG for log messages. */\r
+       private static final String TAG = "EulaOrNewVersion";\r
+       private static final boolean debug = false;\r
+       \r
+       public static final String PREFERENCES_EULA_ACCEPTED = "eula_accepted";\r
+\r
+       public static final String PREFERENCES_VERSION_NUMBER = "org.openintents.distribution.version_number_check";\r
+       \r
+       /**\r
+        * Extra for main intent.\r
+        * Specifies activity that should be launched after Eula has been accepted.\r
+        */\r
+       static final String EXTRA_LAUNCH_ACTIVITY_PACKAGE = "org.openintents.extra.launch_activity_package";\r
+       static final String EXTRA_LAUNCH_ACTIVITY_CLASS = "org.openintents.extra.launch_activity_class";\r
+       static final String EXTRA_LAUNCH_ACTIVITY_INTENT = "org.openintents.extra.launch_activity_intent";\r
+       \r
+       /**\r
+        * Test whether EULA has been accepted. Otherwise display EULA.\r
+        * \r
+        * @return True if Eula needs to be shown.\r
+        */\r
+       static boolean showEula(Activity activity) {\r
+               SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);\r
+               boolean accepted = sp.getBoolean(PREFERENCES_EULA_ACCEPTED, false);\r
+               \r
+               if (accepted) {\r
+                       if (debug) Log.d(TAG, "Eula has been accepted.");\r
+                       return false;\r
+               } else {\r
+                       if (debug) Log.d(TAG, "Eula has not been accepted yet.");\r
+                       \r
+                       startForwardActivity(activity, EulaActivity.class);\r
+                       return true;\r
+               }\r
+       }\r
+       \r
+       static void storeEulaAccepted(Context context) {\r
+               SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);\r
+               SharedPreferences.Editor e = sp.edit();\r
+               e.putBoolean(PREFERENCES_EULA_ACCEPTED, true);\r
+               e.commit();\r
+       }\r
+       \r
+       /**\r
+        * Test whether version code changed.\r
+        * \r
+        * @return True if version code changed and recent changes are being shown.\r
+        */\r
+       static boolean showNewVersion(Activity activity) {\r
+               SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);\r
+               int lastVersion = sp.getInt(PREFERENCES_VERSION_NUMBER, 0);\r
+               int thisVersion = VersionUtils.getVersionCode(activity);\r
+               \r
+               if (lastVersion == thisVersion) {\r
+                       if (debug) Log.i(TAG, "Same version " + lastVersion + " as last launch.");\r
+                       return false;\r
+               } else {\r
+                       if (debug) Log.i(TAG, "Newer version " + thisVersion + " since last launch " + lastVersion + ". Show recent changes.");\r
+                       \r
+                       startForwardActivity(activity, NewVersionActivity.class);\r
+                       return true;\r
+               }\r
+       }\r
+       \r
+       static void storeCurrentVersionCode(Context context) {\r
+               SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);\r
+               int thisVersion = VersionUtils.getVersionCode(context);\r
+               SharedPreferences.Editor e = sp.edit();\r
+               e.putInt(PREFERENCES_VERSION_NUMBER, thisVersion);\r
+               e.commit();\r
+       }\r
+\r
+       private static void startForwardActivity(Activity activity, Class launchClass) {\r
+               // Launch Eula activity\r
+               Intent forwardIntent = activity.getIntent();\r
+               \r
+               Intent i = new Intent(activity, launchClass);\r
+               ComponentName ci = activity.getComponentName();\r
+               \r
+               // Specify in intent extras which activity should be called\r
+               // after Eula has been accepted.\r
+               if (debug) Log.d(TAG, "Local package name: " + ci.getPackageName());\r
+               if (debug) Log.d(TAG, "Local class name: " + ci.getClassName());\r
+               i.putExtra(EXTRA_LAUNCH_ACTIVITY_PACKAGE, ci.getPackageName());\r
+               i.putExtra(EXTRA_LAUNCH_ACTIVITY_CLASS, ci.getClassName());\r
+               if (forwardIntent != null) {\r
+                       i.putExtra(EXTRA_LAUNCH_ACTIVITY_INTENT, forwardIntent);\r
+               }\r
+               i.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);\r
+               activity.startActivity(i);\r
+               activity.finish();\r
+       }\r
+       \r
+}\r