@Override
  public void finish() {
    super.finish();

    if (shouldFinishRootActivity()) {
      TiApplication app = getTiApp();
      if (app != null) {
        TiRootActivity rootActivity = app.getRootActivity();
        if (rootActivity != null && !(rootActivity.equals(this)) && !rootActivity.isFinishing()) {
          rootActivity.finish();
        } else if (rootActivity == null && !app.isRestartPending()) {
          // When the root activity has been killed and garbage collected and the app is not
          // scheduled to restart,
          // we need to force finish the root activity while this activity has an intent to finish
          // root.
          // This happens when the "Don't keep activities" option is enabled and the user stays in
          // some activity
          // (eg. heavyweight window, tabgroup) other than the root activity for a while and then he
          // wants to back
          // out the app.
          app.setForceFinishRootActivity(true);
        }
      }
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    if (willFinishFalseRootActivity(savedInstanceState)) {
      return;
    }

    if (checkInvalidLaunch(savedInstanceState)) {
      // Android bug 2373 detected and we're going to restart.
      return;
    }

    TiApplication tiApp = getTiApp();

    startedFromBoot = getIntentBoolean(TiC.INTENT_PROPERTY_ON_BOOT, false);

    if (tiApp.isRestartPending()
        || TiBaseActivity.isUnsupportedReLaunch(this, savedInstanceState)) {
      super.onCreate(savedInstanceState); // Will take care of scheduling restart and finishing.
      return;
    }

    tiApp.setCurrentActivity(this, this);

    Log.checkpoint(
        TAG, "checkpoint, on root activity create, savedInstanceState: " + savedInstanceState);

    tiApp.setRootActivity(this);

    super.onCreate(savedInstanceState);

    tiApp.verifyCustomModules(this);
  }
  @Override
  /**
   * When this activity restarts, this method updates the current activity to this and fires
   * javascript 'restart' event.
   */
  protected void onRestart() {
    inForeground = true;
    super.onRestart();

    Log.d(TAG, "Activity " + this + " onRestart", Log.DEBUG_MODE);

    TiApplication tiApp = getTiApp();
    if (tiApp.isRestartPending()) {
      if (!isFinishing()) {
        finish();
      }

      return;
    }

    if (activityProxy != null) {
      // we only want to set the current activity for good in the resume state but we need it right
      // now.
      // save off the existing current activity, set ourselves to be the new current activity
      // temporarily
      // so we don't run into problems when we give the proxy the event
      Activity tempCurrentActivity = tiApp.getCurrentActivity();
      tiApp.setCurrentActivity(this, this);

      activityProxy.fireSyncEvent(TiC.EVENT_RESTART, null);

      // set the current activity back to what it was originally
      tiApp.setCurrentActivity(this, tempCurrentActivity);
    }
  }
  @Kroll.method
  public void cancelAlarmService(@Kroll.argument(optional = true) Object requestCode) {
    // To cancel an alarm the signature needs to be the same as the submitting one.
    utils.infoLog("Cancelling Alarm Service");
    int intentRequestCode = AlarmmanagerModule.DEFAULT_REQUEST_CODE;
    if (requestCode != null) {
      if (requestCode instanceof Number) {
        intentRequestCode = ((Number) requestCode).intValue();
      }
    }

    // Create a placeholder for the args value
    HashMap<String, Object> placeholder = new HashMap<String, Object>(0);
    KrollDict args = new KrollDict(placeholder);

    // Create the Alarm Manager
    AlarmManager am =
        (AlarmManager)
            TiApplication.getInstance()
                .getApplicationContext()
                .getSystemService(TiApplication.ALARM_SERVICE);
    Intent intent = createAlarmServiceIntent(args);
    PendingIntent sender =
        PendingIntent.getBroadcast(
            TiApplication.getInstance().getApplicationContext(),
            intentRequestCode,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    am.cancel(sender);
    utils.infoLog("Alarm Service Canceled");
  }
  @Override
  /**
   * When this activity starts, this method updates the current activity to this if necessary and
   * fire javascript 'start' and 'focus' events. Focus events will only fire if the activity is not
   * a tab activity.
   */
  protected void onStart() {
    inForeground = true;
    super.onStart();
    if (isFinishing()) {
      return;
    }

    // Newer versions of Android appear to turn this on by default.
    // Turn if off until an activity indicator is shown.
    setProgressBarIndeterminateVisibility(false);

    Log.d(TAG, "Activity " + this + " onStart", Log.DEBUG_MODE);

    TiApplication tiApp = getTiApp();

    if (tiApp.isRestartPending()) {
      if (!isFinishing()) {
        finish();
      }
      return;
    }

    updateTitle();

    if (activityProxy != null) {
      // we only want to set the current activity for good in the resume state but we need it right
      // now.
      // save off the existing current activity, set ourselves to be the new current activity
      // temporarily
      // so we don't run into problems when we give the proxy the event
      Activity tempCurrentActivity = tiApp.getCurrentActivity();
      tiApp.setCurrentActivity(this, this);

      activityProxy.fireEvent(TiC.EVENT_START, null);

      // set the current activity back to what it was originally
      tiApp.setCurrentActivity(this, tempCurrentActivity);
    }

    synchronized (lifecycleListeners.synchronizedList()) {
      for (OnLifecycleEvent listener : lifecycleListeners.nonNull()) {
        try {
          TiLifecycle.fireLifecycleEvent(this, listener, TiLifecycle.LIFECYCLE_ON_START);

        } catch (Throwable t) {
          Log.e(TAG, "Error dispatching lifecycle event: " + t.getMessage(), t);
        }
      }
    }
    // store current configuration orientation
    // This fixed bug with double orientation chnage firing when activity starts in landscape
    previousOrientation = getResources().getConfiguration().orientation;
  }
Example #6
0
  public SensorModule(TiContext tiContext) {
    this();

    if (TiApplication.getInstance().getSystemService(Context.SENSOR_SERVICE) != null) {
      sensorManager =
          (SensorManager) TiApplication.getInstance().getSystemService(Context.SENSOR_SERVICE);
    }
  }
Example #7
0
  private Object loadMethod(Class<?> source, String name) {
    Object o = null;
    TiApplication tiApp = weakApplication.get();
    if (tiApp != null) {
      o = tiApp.methodFor(source, name);
    }

    return o;
  }
  private static UrbanAirshipModule getModule() {
    TiApplication appContext = TiApplication.getInstance();
    UrbanAirshipModule uaModule = (UrbanAirshipModule) appContext.getModuleByName(ModuleName);

    if (uaModule == null) {
      Log.w(LCAT, "Urban Airship module not currently loaded");
    }
    return uaModule;
  }
  @Override
  /**
   * When this activity pauses, this method sets the current activity to null, fires a javascript
   * 'pause' event, and if the activity is finishing, remove all dialogs associated with it.
   */
  protected void onPause() {
    inForeground = false;
    super.onPause();
    isResumed = false;

    Log.d(TAG, "Activity " + this + " onPause", Log.DEBUG_MODE);

    TiApplication tiApp = getTiApp();
    if (tiApp.isRestartPending()) {
      releaseDialogs(true);
      if (!isFinishing()) {
        finish();
      }
      return;
    }

    if (!windowStack.empty()) {
      windowStack.peek().onWindowFocusChange(false);
    }

    TiApplication.updateActivityTransitionState(true);
    tiApp.setCurrentActivity(this, null);
    TiUIHelper.showSoftKeyboard(getWindow().getDecorView(), false);

    if (this.isFinishing()) {
      releaseDialogs(true);
    } else {
      // release non-persistent dialogs when activity hides
      releaseDialogs(false);
    }

    if (activityProxy != null) {
      activityProxy.fireSyncEvent(TiC.EVENT_PAUSE, null);
    }

    synchronized (lifecycleListeners.synchronizedList()) {
      for (OnLifecycleEvent listener : lifecycleListeners.nonNull()) {
        try {
          TiLifecycle.fireLifecycleEvent(this, listener, TiLifecycle.LIFECYCLE_ON_PAUSE);

        } catch (Throwable t) {
          Log.e(TAG, "Error dispatching lifecycle event: " + t.getMessage(), t);
        }
      }
    }

    // Checkpoint for ti.end event
    if (tiApp != null) {
      tiApp.postAnalyticsEvent(TiAnalyticsEventFactory.createAppEndEvent());
    }
  }
  /**
   * Returns the content view for this tab.
   *
   * @return the content view or null if the tab is empty
   */
  public View getContentView() {
    if (contentView == null) {
      TiWindowProxy windowProxy = getWindowProxy();
      if (windowProxy == null) {
        // If no window is provided use an empty view.
        View emptyContent = new View(TiApplication.getInstance().getApplicationContext());
        emptyContent.setBackgroundColor(Color.BLACK);
        return emptyContent;
      }

      contentView = new TabContentViewProxy();

      // A tab's window should be bound to the tab group's activity.
      // In order for the 'activity' property to work correctly
      // we need to set the content view's activity to that of the group.
      Activity tabGroupActivity = ((TabProxy) proxy).getTabGroup().getActivity();
      contentView.setActivity(tabGroupActivity);

      // Assign parent so events bubble up correctly.
      contentView.setParent(proxy);

      // Allow the window to fill the content view with its children.
      windowProxy.getKrollObject().setWindow(contentView);
    }

    return contentView.getOrCreateView().getNativeView();
  }
Example #11
0
  @Kroll.method
  public void remove(TiViewProxy child) {
    if (child == null) {
      Log.w(LCAT, "add called with null child");

      return;
    }

    if (peekView() != null) {
      if (TiApplication.isUIThread()) {
        handleRemove(child);
        return;
      }

      TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_REMOVE_CHILD), child);

    } else {
      if (children != null) {
        children.remove(child);
        if (child.parent != null && child.parent.get() == this) {
          child.parent = null;
        }
      }
    }
  }
Example #12
0
  @Kroll.method
  public void add(TiViewProxy child) {
    if (child == null) {
      Log.w(LCAT, "add called with null child");
      return;
    }

    if (children == null) {
      children = new ArrayList<TiViewProxy>();
    }

    if (peekView() != null) {
      if (TiApplication.isUIThread()) {
        handleAdd(child);
        return;
      }

      TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_ADD_CHILD), child);

    } else {
      children.add(child);
      child.parent = new WeakReference<TiViewProxy>(this);
    }
    // TODO zOrder
  }
Example #13
0
 protected InputMethodManager getIMM() {
   InputMethodManager imm = null;
   imm =
       (InputMethodManager)
           TiApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);
   return imm;
 }
 @Kroll.method
 @Kroll.getProperty
 public String getExternalFilesDir() {
   File f = m_app.getExternalFilesDir(null);
   if (!f.exists()) f.mkdirs();
   return f.getAbsolutePath();
 }
 private Activity getCurrentActivity() {
   Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
   if (currentActivity == null) {
     currentActivity = proxy.getActivity();
   }
   return currentActivity;
 }
Example #16
0
  @Kroll.method
  public Crouton makeText(String text, int style) {
    Log.d(TAG, "makeText called");

    return Crouton.makeText(
        TiApplication.getInstance().getCurrentActivity(), text, getStyle(style));
  }
 @Kroll.method
 public void cancelNotification(int requestCode) {
   NotificationManager notificationManager =
       (NotificationManager)
           TiApplication.getInstance().getSystemService(TiApplication.NOTIFICATION_SERVICE);
   notificationManager.cancel(requestCode);
 }
  private Intent createAlarmServiceIntent(KrollDict args) {
    String serviceName = args.getString("service");
    Intent intent =
        new Intent(TiApplication.getInstance().getApplicationContext(), AlarmServiceListener.class);
    intent.putExtra("alarm_service_name", serviceName);
    // Pass in flag if we need to restart the service on each call
    intent.putExtra("alarm_service_force_restart", (optionIsEnabled(args, "forceRestart")));
    // Check if the user has selected to use intervals
    boolean hasInterval = (args.containsKeyAndNotNull("interval"));
    long intervalValue = 0;
    if (hasInterval) {
      Object interval = args.get("interval");
      if (interval instanceof Number) {
        intervalValue = ((Number) interval).longValue();
      } else {
        hasInterval = false;
      }
    }
    intent.putExtra("alarm_service_has_interval", hasInterval);
    if (hasInterval) {
      intent.putExtra("alarm_service_interval", intervalValue);
    }

    utils.debugLog(
        "created alarm service intent for "
            + serviceName
            + "(forceRestart: "
            + (optionIsEnabled(args, "forceRestart") ? "true" : "false")
            + ", intervalValue: "
            + intervalValue
            + ")");

    return intent;
  }
  @Kroll.method
  public void takeScreenshot(KrollFunction callback) {
    Activity a = TiApplication.getAppCurrentActivity();

    if (a == null) {
      Log.w(TAG, "Could not get current activity for takeScreenshot.", Log.DEBUG_MODE);
      callback.callAsync(getKrollObject(), new Object[] {null});
      return;
    }

    while (a.getParent() != null) {
      a = a.getParent();
    }

    Window w = a.getWindow();

    while (w.getContainer() != null) {
      w = w.getContainer();
    }

    KrollDict image = TiUIHelper.viewToImage(null, w.getDecorView());
    if (callback != null) {
      callback.callAsync(getKrollObject(), new Object[] {image});
    }
  }
Example #20
0
 @Kroll.method
 protected boolean hasFlashLight() {
   Activity activity = TiApplication.getAppRootOrCurrentActivity();
   boolean hasFlash =
       activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
   return hasFlash;
 }
 private void setImage(final Bitmap bitmap) {
   if (!TiApplication.isUIThread()) {
     TiMessenger.sendBlockingMainMessage(handler.obtainMessage(SET_IMAGE), bitmap);
   } else {
     handleSetImage(bitmap);
   }
 }
 @Kroll.method
 public void hide() {
   if (TiApplication.isUIThread()) {
     handleHide();
   } else {
     getMainHandler().obtainMessage(MSG_HIDE).sendToTarget();
   }
 }
 @Kroll.method
 public void show() {
   if (TiApplication.isUIThread()) {
     handleShow();
   } else {
     getMainHandler().obtainMessage(MSG_SHOW).sendToTarget();
   }
 }
 @Kroll.method
 public void release() {
   if (TiApplication.isUIThread()) {
     super.releaseViews();
   } else {
     getMainHandler().sendEmptyMessage(MSG_RELEASE);
   }
 }
 @Kroll.method
 public void appendItems(Object data) {
   if (TiApplication.isUIThread()) {
     handleAppendItems(data);
   } else {
     TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_APPEND_ITEMS), data);
   }
 }
Example #26
0
 @Kroll.method
 public void hide(@Kroll.argument(optional = true) KrollDict options) {
   if (TiApplication.isUIThread()) {
     handleHide(options);
   } else {
     getMainHandler().obtainMessage(MSG_HIDE, options).sendToTarget();
   }
 }
Example #27
0
 @Kroll.method
 public void blur() {
   if (TiApplication.isUIThread()) {
     handleBlur();
   } else {
     getMainHandler().sendEmptyMessage(MSG_BLUR);
   }
 }
Example #28
0
 @Kroll.method
 public void focus() {
   if (TiApplication.isUIThread()) {
     handleFocus();
   } else {
     getMainHandler().sendEmptyMessage(MSG_FOCUS);
   }
 }
 public void stop() {
   if (!TiApplication.isUIThread()) {
     Message message = handler.obtainMessage(STOP);
     message.sendToTarget();
   } else {
     handleStop();
   }
 }
  @Override
  public void finish() {
    if (finishing2373) {
      super.finish();
      return;
    }

    // Ensure we only run the finish logic once. We want to avoid an infinite loop since this method
    // can be called
    // from the finish method inside TiBaseActivity ( which can be triggered by
    // terminateActivityStack() )
    if (!finishing) {
      finishing = true;
      TiApplication.removeFromActivityStack(this);
      TiApplication.terminateActivityStack();
      super.finish();
    }
  }