Guest User

Untitled

a guest
Jan 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. /**
  2. * Appcelerator Titanium Mobile
  3. * Copyright (c) 2009-2011 by Appcelerator, Inc. All Rights Reserved.
  4. * Licensed under the terms of the Apache Public License
  5. * Please see the LICENSE included with this distribution for details.
  6. */
  7. package org.appcelerator.titanium.proxy;
  8.  
  9. import org.appcelerator.kroll.KrollInvocation;
  10. import org.appcelerator.kroll.KrollProxy;
  11. import org.appcelerator.kroll.annotations.Kroll;
  12. import org.appcelerator.titanium.TiApplication;
  13. import org.appcelerator.titanium.util.TiActivityResultHandler;
  14. import org.appcelerator.titanium.util.TiActivitySupport;
  15. import org.appcelerator.titanium.util.TiActivitySupportHelper;
  16. import org.appcelerator.titanium.util.TiConfig;
  17.  
  18. import android.app.Activity;
  19. import android.content.Context;
  20. import android.content.Intent;
  21.  
  22. @Kroll.proxy
  23. @Kroll.dynamicApis(properties = {
  24. "onCreateOptionsMenu",
  25. "onPrepareOptionsMenu"
  26. })
  27. public class ActivityProxy extends KrollProxy
  28. implements TiActivityResultHandler
  29. {
  30. private static final String TAG = "ActivityProxy";
  31. private static boolean DBG = TiConfig.LOGD;
  32.  
  33. protected Activity activity;
  34. protected IntentProxy intentProxy;
  35. //protected KrollCallback resultCallback;
  36.  
  37. public ActivityProxy(Activity activity)
  38. {
  39. super();
  40. setActivity(activity);
  41. }
  42.  
  43. public void setActivity(Activity activity)
  44. {
  45. this.activity = activity;
  46. Intent intent = activity.getIntent();
  47. if (intent != null) {
  48. intentProxy = new IntentProxy(activity.getIntent());
  49. }
  50. }
  51.  
  52. protected Activity getWrappedActivity()
  53. {
  54. if (activity != null) {
  55. return activity;
  56. }
  57. return TiApplication.getInstance().getRootActivity();
  58.  
  59. /*
  60. Activity activity = this.activity;
  61. if (activity != null) return activity;
  62.  
  63. if (invocation != null) {
  64. activity = invocation.getTiContext().getActivity();
  65. if (activity != null) return activity;
  66. }
  67.  
  68. activity = getTiContext().getActivity();
  69. if (activity != null) return activity;
  70.  
  71. activity = getTiContext().getRootActivity();
  72. if (activity != null) return activity;
  73.  
  74. return null;
  75. */
  76. }
  77.  
  78. @Kroll.method
  79. public void startActivity(KrollInvocation invocation, IntentProxy intent)
  80. {
  81. Activity activity = getWrappedActivity();
  82. if (activity != null) {
  83. activity.startActivity(intent.getIntent());
  84. }
  85. }
  86.  
  87. @Kroll.method
  88. public void startActivityForResult(KrollInvocation invocation,
  89. IntentProxy intent/*, KrollCallback callback*/)
  90. {
  91. Activity activity = getWrappedActivity();
  92. if (activity != null) {
  93. TiActivitySupport support = null;
  94. if (activity instanceof TiActivitySupport) {
  95. support = (TiActivitySupport)activity;
  96. } else {
  97. support = new TiActivitySupportHelper(activity);
  98. }
  99.  
  100. //this.resultCallback = callback;
  101. int requestCode = support.getUniqueResultCode();
  102. support.launchActivityForResult(intent.getIntent(), requestCode, this);
  103. }
  104. }
  105.  
  106. @Kroll.method
  107. public void startActivityFromChild(KrollInvocation invocation,
  108. ActivityProxy child, IntentProxy intent, int requestCode)
  109. {
  110. Activity activity = getWrappedActivity();
  111. if (activity != null) {
  112. activity.startActivityFromChild(child.getWrappedActivity(), intent.getIntent(), requestCode);
  113. }
  114. }
  115.  
  116. @Kroll.method
  117. public boolean startActivityIfNeeded(KrollInvocation invocation, IntentProxy intent, int requestCode)
  118. {
  119. Activity activity = getWrappedActivity();
  120. if (activity != null) {
  121. return activity.startActivityIfNeeded(intent.getIntent(), requestCode);
  122. }
  123. return false;
  124. }
  125.  
  126. @Kroll.method
  127. public boolean startNextMatchingActivity(KrollInvocation invocation, IntentProxy intent)
  128. {
  129. Activity activity = getWrappedActivity();
  130. if (activity != null) {
  131. return activity.startNextMatchingActivity(intent.getIntent());
  132. }
  133. return false;
  134. }
  135.  
  136. @Kroll.method
  137. public String getString(KrollInvocation invocation, int resId, Object[] formatArgs)
  138. {
  139. Activity activity = getWrappedActivity();
  140. if (activity != null) {
  141. if (formatArgs == null || formatArgs.length == 0) {
  142. return activity.getString(resId);
  143. } else {
  144. return activity.getString(resId, formatArgs);
  145. }
  146. }
  147. return null;
  148. }
  149.  
  150. @Kroll.method @Kroll.getProperty
  151. public IntentProxy getIntent()
  152. {
  153. return intentProxy;
  154. }
  155.  
  156. @Kroll.method @Kroll.setProperty
  157. public void setRequestedOrientation(KrollInvocation invocation, int orientation)
  158. {
  159. Activity activity = getWrappedActivity();
  160. if (activity != null) {
  161. activity.setRequestedOrientation(orientation);
  162. }
  163. }
  164.  
  165. @Kroll.method
  166. public void setResult(KrollInvocation invocation, int resultCode,
  167. @Kroll.argument(optional=true) IntentProxy intent)
  168. {
  169. Activity activity = getWrappedActivity();
  170. if (activity != null) {
  171. if (intent == null) {
  172. activity.setResult(resultCode);
  173. } else {
  174. activity.setResult(resultCode, intent.getIntent());
  175. }
  176. }
  177. }
  178.  
  179. @Kroll.method
  180. public void finish(KrollInvocation invocation)
  181. {
  182. Activity activity = getWrappedActivity();
  183. if (activity != null) {
  184. activity.finish();
  185. }
  186. }
  187.  
  188. // TODO @Override
  189. public void onResult(Activity activity, int requestCode, int resultCode, Intent data)
  190. {
  191. /*if (resultCallback == null) return;
  192. KrollDict event = new KrollDict();
  193. event.put(TiC.EVENT_PROPERTY_REQUEST_CODE, requestCode);
  194. event.put(TiC.EVENT_PROPERTY_RESULT_CODE, resultCode);
  195. event.put(TiC.EVENT_PROPERTY_INTENT, new IntentProxy(getTiContext(), data));
  196. event.put(TiC.EVENT_PROPERTY_SOURCE, this);
  197. resultCallback.callAsync(event);*/
  198. }
  199.  
  200. // TODO @Override
  201. public void onError(Activity activity, int requestCode, Exception e)
  202. {
  203. /*if (resultCallback == null) return;
  204. KrollDict event = new KrollDict();
  205. event.put(TiC.EVENT_PROPERTY_REQUEST_CODE, requestCode);
  206. event.put(TiC.EVENT_PROPERTY_ERROR, e.getMessage());
  207. event.put(TiC.EVENT_PROPERTY_SOURCE, this);
  208. resultCallback.callAsync(event);*/
  209. }
  210.  
  211. public Context getContext()
  212. {
  213. if (activity == null) {
  214. return TiApplication.getInstance();
  215. //return getTiContext().getActivity().getApplication();
  216. }
  217. return activity;
  218. }
  219.  
  220. /*
  221. public Activity getActivity()
  222. {
  223. return activity;
  224. }
  225. */
  226.  
  227. public void release()
  228. {
  229. activity = null;
  230. }
  231. }
Add Comment
Please, Sign In to add comment