PageRenderTime 221ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/device/sample/apps/SampleEmailPolicy/src/com/android/email/policy/EmailPolicy.java

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
Java | 247 lines | 55 code | 13 blank | 179 comment | 11 complexity | e28c9e0659629f8638b005a8490a468a MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.android.email.policy;
  17. import android.os.Bundle;
  18. /**
  19. * This sample is the framework that can be used to build EmailPolicy packages for inclusion
  20. * on specific devices. This sample is intended for use by OEMs or other creators of system
  21. * images.
  22. *
  23. * When this package is built and included in a system image, the Email application will detect
  24. * it (using reflection) and will make calls to the getPolicy() method at certain times. This
  25. * can be used to provide local customization of the Email application.
  26. *
  27. * Do not change the package, class name, or method name - these must match the names used in
  28. * this sample code or the Email application will not find the helper.
  29. *
  30. * Three customization points are provided:
  31. *
  32. * * Alternate strings for Exchange/ActiveSync UI
  33. * * Insertion of device-specific text into IMAP ID commands
  34. * * Device- or Carrier- specific account presets
  35. *
  36. * Each policy request may contain one or more parameters; These are supplied as keyed entries
  37. * in the "arguments" bundle. If there is a single argument, it will typically use the same key
  38. * as the policy name. If there are multiple arguments, they keys are provided and called out.
  39. *
  40. * In all cases, getPolicy() should return a bundle. For default behavior, or for any unknown
  41. * policy, simply return Bundle.EMPTY.
  42. *
  43. * To return actual data, create a new bundle and place result values in it. If there is a single
  44. * return value, this value is placed in the return bundle using the same key as the request.
  45. * If there are multiple return values, keys will be provided for them as well.
  46. *
  47. * Future versions of the Email application may access additional customization points. If
  48. * the call to getPolicy() is made with an unknown or unexpected policy keys, or the expected
  49. * argument values cannot be found, the method should Bundle.EMPTY.
  50. */
  51. public class EmailPolicy {
  52. /**
  53. * This policy request configures the UI to conform to various Exchange/ActiveSync
  54. * license requirements. In the default configuration, the UI will refer to this protocol as
  55. * "Exchange", while in the alternate configuration, the UI will refer to it as
  56. * "Exchange ActiveSync" or "Microsoft Exchange ActiveSync".
  57. *
  58. * For the default behavior, return the empty bundle.
  59. * For the alternate behavior, return a bundle containing a single entry with a key of
  60. * USE_ALTERNATE_EXCHANGE_STRINGS and a value of "true".
  61. */
  62. private static final String USE_ALTERNATE_EXCHANGE_STRINGS = "useAlternateExchangeStrings";
  63. /**
  64. * This policy request allows you to insert field/value pairs into the IMAP ID command, which
  65. * is sent to an IMAP server on each connection.
  66. *
  67. * The following arguments are provided:
  68. * * GET_IMAP_ID_USER - the userid of the account being connected to
  69. * * GET_IMAP_ID_HOST - the hostname of the server being connected to
  70. * * GET_IMAP_ID_CAPA - the values returned by the "CAPA" command
  71. *
  72. * For default behavior (no values inserted), return the empty bundle.
  73. * To insert additional values into the IMAP ID command, return a bundle containing a single
  74. * entry with a key of GET_IMAP_ID and a String value. The value must be field/value pairs
  75. * surrounded by quotes and separated by spaces. Multiple field/value pairs may be provided.
  76. * See RFC 2971 for more information.
  77. */
  78. private static final String GET_IMAP_ID = "getImapId";
  79. private static final String GET_IMAP_ID_USER = "getImapId.user";
  80. private static final String GET_IMAP_ID_HOST = "getImapId.host";
  81. private static final String GET_IMAP_ID_CAPA = "getImapId.capabilities";
  82. /**
  83. * This policy request allows you to supply preset server configurations to provide
  84. * automatic setup/configuration for specific email providers. These values supplement (or
  85. * override) the automatic configurations provided in res/xml/providers.xml in
  86. * the Email sources. (See that file for more information and plenty of samples.)
  87. *
  88. * The only argument (with the key FIND_PROVIDER) is a string containing the domain that the
  89. * user entered as part of their email address; For example, if the user enters
  90. * "MyEmailAddress@gmail.com", the domain will be "gmail.com".
  91. *
  92. * If no server information is provided for this domain, simply return Bundle.EMPTY.
  93. * If server information is available for this domain, it can be returned in the following
  94. * values:
  95. * * FIND_PROVIDER_IN_URI The server configuration for the incoming (IMAP or POP) server
  96. * * FIND_PROVIDER_IN_USER Format of the username (login) value
  97. * * FIND_PROVIDER_OUT_URI The server configuration for the outgoing (SMTP) server
  98. * * FIND_PROVIDER_OUT_USER Format of the username (login) value
  99. *
  100. * Valid incoming uri schemes are:
  101. * imap IMAP with no transport security.
  102. * imap+tls+ IMAP with required TLS transport security.
  103. * If TLS is not available the connection fails.
  104. * imap+ssl+ IMAP with required SSL transport security.
  105. * If SSL is not available the connection fails.
  106. *
  107. * pop3 POP3 with no transport security.
  108. * pop3+tls+ POP3 with required TLS transport security.
  109. * If TLS is not available the connection fails.
  110. * pop3+ssl+ POP3 with required SSL transport security.
  111. * If SSL is not available the connection fails.
  112. *
  113. * Valid outgoing uri schemes are:
  114. * smtp SMTP with no transport security.
  115. * smtp+tls+ SMTP with required TLS transport security.
  116. * If TLS is not available the connection fails.
  117. * smtp+ssl+ SMTP with required SSL transport security.
  118. * If SSL is not available the connection fails.
  119. *
  120. * To the above schemes you may also add "trustallcerts" to indicate that,
  121. * although link encryption is still required, "non-trusted" certificates may
  122. * will be excepted. For example, "imap+ssl+trustallcerts" or
  123. * "smtp+tls+trustallcerts". This should only used when necessary, as it
  124. * could allow a spoofed server to intercept password and mail.
  125. *
  126. * The URIs should be full templates for connection, including a port if
  127. * the service uses a non-default port. The default ports are as follows:
  128. * imap 143 pop3 110 smtp 587
  129. * imap+tls+ 143 pop3+tls+ 110 smtp+tls+ 587
  130. * imap+ssl+ 993 pop3+ssl+ 995 smtp+ssl+ 465
  131. *
  132. * The username attribute is used to supply a template for the username
  133. * that will be presented to the server. This username is built from a
  134. * set of variables that are substituted with parts of the user
  135. * specified email address.
  136. *
  137. * Valid substitution values for the username attribute are:
  138. * $email - the email address the user entered
  139. * $user - the value before the @ sign in the email address the user entered
  140. * $domain - the value after the @ signin the email address the user entered
  141. *
  142. * The username attribute MUST be specified for the incoming element, so the POP3 or IMAP
  143. * server can identify the mailbox to be opened.
  144. *
  145. * The username attribute MAY be the empty string for the outgoing element, but only if the
  146. * SMTP server supports anonymous transmission (most don't).
  147. *
  148. * For more information about these values, and many examples, see res/xml/providers.xml in
  149. * the Email sources.
  150. */
  151. private static final String FIND_PROVIDER = "findProvider";
  152. private static final String FIND_PROVIDER_IN_URI = "findProvider.inUri";
  153. private static final String FIND_PROVIDER_IN_USER = "findProvider.inUser";
  154. private static final String FIND_PROVIDER_OUT_URI = "findProvider.outUri";
  155. private static final String FIND_PROVIDER_OUT_USER = "findProvider.outUser";
  156. /**
  157. * The following data is simply examples, and would be changed (or removed) based on the
  158. * requirements for your device.
  159. */
  160. /**
  161. * Sample: Email domains that will be auto-configured. In our example, a number of domains
  162. * are controlled by a single mail server.
  163. */
  164. private static final String[] KNOWN_DOMAINS = new String[] {
  165. "physics.school.edu", "math.school.edu", "language.school.edu", "history.school.edu"
  166. };
  167. /**
  168. * Sample: When we see a particular capability (identifying a particular server), send
  169. * back a special value in the IMAP ID command.
  170. */
  171. private static final String MY_SERVER_CAPABILITY = "MY-SERVER-CAPABILITY";
  172. private static final String MY_DEVICE_ID = "\"DEVICE-ID-FIELD\" \"MY-DEVICE-ID-VALUE\"";
  173. /**
  174. * Entry point from the Email application.
  175. *
  176. * @param policy A string requesting a particular policy
  177. * @param arguments A bundle containing zero or more argument values for the requested policy
  178. * @return A bundle containing zero or more return values for the requested policy
  179. */
  180. public static Bundle getPolicy(String policy, Bundle arguments) {
  181. /*
  182. * Policy: Use alternate exchange strings
  183. */
  184. if (USE_ALTERNATE_EXCHANGE_STRINGS.equals(policy)) {
  185. // Un-comment the following code to select alternate exchange strings
  186. // Bundle alternates = new Bundle();
  187. // alternates.putBoolean(USE_ALTERNATE_EXCHANGE_STRINGS, true);
  188. // return alternates;
  189. }
  190. /*
  191. * Policy: For a known domain, configure to the servers for that domain
  192. */
  193. if (FIND_PROVIDER.equals(policy)) {
  194. String domain = arguments.getString(FIND_PROVIDER);
  195. if (domain != null) {
  196. domain = domain.toLowerCase();
  197. boolean isKnownDomain = false;
  198. for (String knownDomain : KNOWN_DOMAINS) {
  199. if (knownDomain.equals(domain)) {
  200. isKnownDomain = true;
  201. break;
  202. }
  203. }
  204. if (isKnownDomain) {
  205. Bundle b = new Bundle();
  206. b.putString(FIND_PROVIDER_IN_URI, "imap+ssl://imap.school.edu");
  207. b.putString(FIND_PROVIDER_IN_USER, "$email");
  208. b.putString(FIND_PROVIDER_OUT_URI, "smtp+ssl://smtp.school.edu");
  209. b.putString(FIND_PROVIDER_OUT_USER, "$email");
  210. return b;
  211. }
  212. }
  213. }
  214. /**
  215. * Policy: If the IMAP server presents a particular capability, send back a particular
  216. * identifier in the IMAP ID.
  217. */
  218. if (GET_IMAP_ID.equals(policy)) {
  219. String capabilities = arguments.getString(GET_IMAP_ID_CAPA);
  220. if (capabilities != null) {
  221. if (capabilities.toUpperCase().contains(MY_SERVER_CAPABILITY)) {
  222. Bundle b = new Bundle();
  223. b.putString(GET_IMAP_ID, MY_DEVICE_ID);
  224. return b;
  225. }
  226. }
  227. }
  228. /**
  229. * For any other policy request, or any policy request that cannot be processed,
  230. * return an empty bundle.
  231. */
  232. return Bundle.EMPTY;
  233. }
  234. }