Options.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Copyright 2013-2014 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. package de.appplant.cordova.plugin.localnotification;
  19. import java.util.Calendar;
  20. import java.util.Date;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23. import android.app.Activity;
  24. import android.app.AlarmManager;
  25. import android.content.Context;
  26. import android.media.RingtoneManager;
  27. import android.net.Uri;
  28. /**
  29. * Class that helps to store the options that can be specified per alarm.
  30. */
  31. public class Options {
  32. private JSONObject options = new JSONObject();
  33. private String packageName = null;
  34. private long interval = 0;
  35. Options (Activity activity) {
  36. packageName = activity.getPackageName();
  37. }
  38. Options (Context context) {
  39. packageName = context.getPackageName();
  40. }
  41. /**
  42. * Parst die übergebenen Eigenschaften.
  43. */
  44. public Options parse (JSONObject options) {
  45. String repeat = options.optString("repeat");
  46. this.options = options;
  47. if (repeat.equalsIgnoreCase("secondly")) {
  48. interval = 1000;
  49. } if (repeat.equalsIgnoreCase("minutely")) {
  50. interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES / 15;
  51. } if (repeat.equalsIgnoreCase("hourly")) {
  52. interval = AlarmManager.INTERVAL_HOUR;
  53. } if (repeat.equalsIgnoreCase("daily")) {
  54. interval = AlarmManager.INTERVAL_DAY;
  55. } else if (repeat.equalsIgnoreCase("weekly")) {
  56. interval = AlarmManager.INTERVAL_DAY*7;
  57. } else if (repeat.equalsIgnoreCase("monthly")) {
  58. interval = AlarmManager.INTERVAL_DAY*31; // 31 days
  59. } else if (repeat.equalsIgnoreCase("yearly")) {
  60. interval = AlarmManager.INTERVAL_DAY*365;
  61. } else {
  62. try {
  63. interval = Integer.parseInt(repeat) * 60000;
  64. } catch (Exception e) {};
  65. }
  66. return this;
  67. }
  68. /**
  69. * Setzt die neue Zeit an Hand des Intervalls.
  70. */
  71. public Options moveDate () {
  72. try {
  73. options.put("date", (getDate() + interval) / 1000);
  74. } catch (JSONException e) {}
  75. return this;
  76. }
  77. /**
  78. * Gibt die Eigenschaften als JSONObjekt an.
  79. */
  80. public JSONObject getJSONObject() {
  81. return options;
  82. }
  83. /**
  84. * Gibt die Zeit in Millisekunden an, wann die Notification aufpoppen soll.
  85. */
  86. public long getDate() {
  87. return options.optLong("date", 0) * 1000;
  88. }
  89. /**
  90. * Gibt die Zeit als Kalender an.
  91. */
  92. public Calendar getCalendar () {
  93. Calendar calendar = Calendar.getInstance();
  94. calendar.setTime(new Date(getDate()));
  95. return calendar;
  96. }
  97. /**
  98. * Gibt die Nachricht der Notification an.
  99. */
  100. public String getMessage () {
  101. return options.optString("message", "");
  102. }
  103. /**
  104. * Gibt den Titel der Notification an.
  105. */
  106. public String getTitle () {
  107. return options.optString("title", "");
  108. }
  109. /**
  110. * Gibt den Pfad zum Sound der Notification an.
  111. */
  112. public Uri getSound () {
  113. String sound = options.optString("sound", null);
  114. if (sound != null) {
  115. try {
  116. int soundId = (Integer) RingtoneManager.class.getDeclaredField(sound).get(Integer.class);
  117. return RingtoneManager.getDefaultUri(soundId);
  118. } catch (Exception e) {
  119. return Uri.parse(sound);
  120. }
  121. }
  122. return null;
  123. }
  124. /**
  125. * Gibt den ID Code des Bildes an.
  126. */
  127. public int getIcon () {
  128. int icon = 0;
  129. String iconName = options.optString("icon", "icon");
  130. icon = getIconValue(packageName, iconName);
  131. if (icon == 0) {
  132. icon = getIconValue("android", iconName);
  133. }
  134. if (icon == 0) {
  135. icon = android.R.drawable.ic_menu_info_details;
  136. }
  137. return options.optInt("icon", icon);
  138. }
  139. /**
  140. * Gibt den ID Code des kleinen Bildes an.
  141. */
  142. public int getSmallIcon () {
  143. int resId = 0;
  144. String iconName = options.optString("smallIcon", "");
  145. resId = getIconValue(packageName, iconName);
  146. if (resId == 0) {
  147. resId = getIconValue("android", iconName);
  148. }
  149. if (resId == 0) {
  150. resId = getIcon();
  151. }
  152. return options.optInt("smallIcon", resId);
  153. }
  154. /**
  155. * Gibt das Intervall an, in dem die Notification aufpoppen soll (daily, weekly, monthly, yearly)
  156. */
  157. public long getInterval () {
  158. return interval;
  159. }
  160. /**
  161. * Gibt die Badge-Nummer der Notification an.
  162. */
  163. public int getBadge () {
  164. return options.optInt("badge", 0);
  165. }
  166. /**
  167. * Gibt die Callback-ID des PluginResults an.
  168. */
  169. public String getId () {
  170. return options.optString("id", "0");
  171. }
  172. /**
  173. * Gibt an, ob die Notification automatisch geschlossen werden soll, wenn der Benutzer darauf klickt.
  174. */
  175. public Boolean getAutoCancel () {
  176. return options.optBoolean("autoCancel", false);
  177. }
  178. /**
  179. *
  180. */
  181. public Boolean getOngoing () {
  182. return options.optBoolean("ongoing", false);
  183. }
  184. /**
  185. * Gibt die zusätzlichen Daten als String an.
  186. */
  187. public String getJSON () {
  188. return options.optString("json", "");
  189. }
  190. /**
  191. * Gibt den Zahlwert des Icons an.
  192. *
  193. * @param {String} className
  194. * @param {String} iconName
  195. */
  196. private int getIconValue (String className, String iconName) {
  197. int icon = 0;
  198. try {
  199. Class<?> klass = Class.forName(className + ".R$drawable");
  200. icon = (Integer) klass.getDeclaredField(iconName).get(Integer.class);
  201. } catch (Exception e) {}
  202. return icon;
  203. }
  204. }