| 1 | /*- |
|---|
| 2 | * Copyright (c) 2006, Derek Konigsberg |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * |
|---|
| 9 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 10 | * notice, this list of conditions and the following disclaimer. |
|---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | * documentation and/or other materials provided with the distribution. |
|---|
| 14 | * 3. Neither the name of the project nor the names of its |
|---|
| 15 | * contributors may be used to endorse or promote products derived |
|---|
| 16 | * from this software without specific prior written permission. |
|---|
| 17 | * |
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|---|
| 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|---|
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 30 | */ |
|---|
| 31 | |
|---|
| 32 | package org.logicprobe.LogicMail; |
|---|
| 33 | |
|---|
| 34 | import net.rim.device.api.system.ApplicationDescriptor; |
|---|
| 35 | import net.rim.device.api.system.Bitmap; |
|---|
| 36 | import net.rim.device.api.system.ControlledAccessException; |
|---|
| 37 | import net.rim.device.api.system.PersistentObject; |
|---|
| 38 | import net.rim.device.api.system.PersistentStore; |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Class to provide information about the application |
|---|
| 42 | * and its environment. Typically this information |
|---|
| 43 | * should come from the deployment descriptor. |
|---|
| 44 | */ |
|---|
| 45 | public final class AppInfo { |
|---|
| 46 | /** System event log GUID */ |
|---|
| 47 | public static final long GUID = 0x6bc611e33074e780L; |
|---|
| 48 | private static PersistentObject store; |
|---|
| 49 | private static PersistableAppInfo persistableInfo; |
|---|
| 50 | |
|---|
| 51 | private static final Bitmap icon = Bitmap.getBitmapResource("logicmail.png"); |
|---|
| 52 | private static final Bitmap rolloverIcon = Bitmap.getBitmapResource("logicmail-rollover.png"); |
|---|
| 53 | private static final Bitmap newMessagesIcon = Bitmap.getBitmapResource("logicmail-new.png"); |
|---|
| 54 | private static final Bitmap newMessagesRolloverIcon = Bitmap.getBitmapResource("logicmail-new-rollover.png"); |
|---|
| 55 | private static String PARAM_RELEASE = "-release"; |
|---|
| 56 | private static String appName; |
|---|
| 57 | private static String appVersion; |
|---|
| 58 | private static boolean release; |
|---|
| 59 | private static String appVersionMoniker; |
|---|
| 60 | private static PlatformInfo platformInfo; |
|---|
| 61 | |
|---|
| 62 | static { |
|---|
| 63 | if(tryInitializeStore()) { |
|---|
| 64 | synchronized(store) { |
|---|
| 65 | if (!(store.getContents() instanceof PersistableAppInfo)) { |
|---|
| 66 | store.setContents(new PersistableAppInfo()); |
|---|
| 67 | store.commit(); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | persistableInfo = (PersistableAppInfo)store.getContents(); |
|---|
| 71 | } |
|---|
| 72 | else { |
|---|
| 73 | persistableInfo = new PersistableAppInfo(); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | private static boolean tryInitializeStore() { |
|---|
| 78 | if(store != null) { return true; } |
|---|
| 79 | try { |
|---|
| 80 | //"org.logicprobe.LogicMail.PersistableAppInfo" |
|---|
| 81 | store = PersistentStore.getPersistentObject(0x6f8bfd06eca43499L); |
|---|
| 82 | return true; |
|---|
| 83 | } catch (ControlledAccessException e) { |
|---|
| 84 | return false; |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * Initializes the application information from the descriptor and the |
|---|
| 90 | * command-line arguments. This method must be called on startup. |
|---|
| 91 | * @param args Arguments |
|---|
| 92 | */ |
|---|
| 93 | public static synchronized void initialize(String args[]) { |
|---|
| 94 | ApplicationDescriptor appDesc = |
|---|
| 95 | ApplicationDescriptor.currentApplicationDescriptor(); |
|---|
| 96 | |
|---|
| 97 | appName = appDesc.getName(); |
|---|
| 98 | appVersion = appDesc.getVersion(); |
|---|
| 99 | platformInfo = PlatformInfo.getInstance(); |
|---|
| 100 | |
|---|
| 101 | // Parse the command-line argument list |
|---|
| 102 | for(int i=0; i<args.length; i++) { |
|---|
| 103 | int p = args[i].indexOf(PARAM_RELEASE); |
|---|
| 104 | if(p != -1) { |
|---|
| 105 | release = true; |
|---|
| 106 | p = args[i].indexOf(':', p + PARAM_RELEASE.length()); |
|---|
| 107 | if(p != -1 && p < args[i].length() - 1) { |
|---|
| 108 | int q = args[i].indexOf(' ', p); |
|---|
| 109 | if(q == -1) { q = args[i].length(); } |
|---|
| 110 | appVersionMoniker = args[i].substring(p + 1, q); |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | public static String getName() { |
|---|
| 117 | return appName; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | public static String getVersion() { |
|---|
| 121 | return appVersion; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | public static String getVersionMoniker() { |
|---|
| 125 | return appVersionMoniker; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | public static String getPlatformVersion() { |
|---|
| 129 | return platformInfo.getPlatformVersion(); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | public static boolean isRelease() { |
|---|
| 133 | return release; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | public static Bitmap getIcon() { |
|---|
| 137 | return icon; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | public static Bitmap getRolloverIcon() { |
|---|
| 141 | return rolloverIcon; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | public static Bitmap getNewMessagesIcon() { |
|---|
| 145 | return newMessagesIcon; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | public static Bitmap getNewMessagesRolloverIcon() { |
|---|
| 149 | return newMessagesRolloverIcon; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | public static String getLastVersion() { |
|---|
| 153 | Object value = persistableInfo.getElement(PersistableAppInfo.FIELD_LAST_APP_VERSION); |
|---|
| 154 | if(value instanceof String) { |
|---|
| 155 | return (String)value; |
|---|
| 156 | } |
|---|
| 157 | else { |
|---|
| 158 | return ""; |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | public static void updateLastVersion() { |
|---|
| 163 | persistableInfo.setElement(PersistableAppInfo.FIELD_LAST_APP_VERSION, appVersion); |
|---|
| 164 | commitPersistableInfo(); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | public static boolean isLicenceAccepted() { |
|---|
| 168 | Object value = persistableInfo.getElement(PersistableAppInfo.FIELD_LICENSE_ACCEPTED); |
|---|
| 169 | if(value instanceof Boolean) { |
|---|
| 170 | return ((Boolean)value).booleanValue(); |
|---|
| 171 | } |
|---|
| 172 | else { |
|---|
| 173 | return false; |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | public static void setLicenseAccepted(boolean accepted) { |
|---|
| 178 | persistableInfo.setElement(PersistableAppInfo.FIELD_LICENSE_ACCEPTED, new Boolean(accepted)); |
|---|
| 179 | commitPersistableInfo(); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | public static boolean isAnalyticsEnabled() { |
|---|
| 183 | Object value = persistableInfo.getElement(PersistableAppInfo.FIELD_ANALYTICS_ENABLED); |
|---|
| 184 | if(value instanceof Boolean) { |
|---|
| 185 | return ((Boolean)value).booleanValue(); |
|---|
| 186 | } |
|---|
| 187 | else { |
|---|
| 188 | return false; |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | public static void setAnalyticsEnabled(boolean enabled) { |
|---|
| 193 | persistableInfo.setElement(PersistableAppInfo.FIELD_ANALYTICS_ENABLED, new Boolean(enabled)); |
|---|
| 194 | commitPersistableInfo(); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | public static void resetPersistableInfo() { |
|---|
| 198 | persistableInfo.setElement(PersistableAppInfo.FIELD_LAST_APP_VERSION, ""); |
|---|
| 199 | persistableInfo.setElement(PersistableAppInfo.FIELD_LICENSE_ACCEPTED, Boolean.FALSE); |
|---|
| 200 | commitPersistableInfo(); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | private static void commitPersistableInfo() { |
|---|
| 204 | if(tryInitializeStore()) { |
|---|
| 205 | synchronized(store) { |
|---|
| 206 | store.setContents(persistableInfo); |
|---|
| 207 | store.commit(); |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | } |
|---|