Android Open Source - Ocypode Start Activity Delegate






From Project

Back to project page Ocypode.

License

The source code is released under:

MIT License

If you think the Android project Ocypode listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ocypode.component.delegate;
// w  w  w .  j  a v  a2  s  .  c o m
import java.util.Random;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.util.SparseArray;

import com.ocypode.R;
import com.ocypode.activity.IResultCallbackActivity;
import com.ocypode.component.delegate.adaptor.IStartActivityAdaptor;
import com.ocypode.utility.Logcat;

/**
 * @author Jairo Barros Junior (jairobjunior@gmail.com)
 * @since May 9, 2013
 */
public class StartActivityDelegate implements IStartActivityDelegate {
    
  private IStartActivityAdaptor mStartActivityAdaptor;
  
  protected SparseArray<IResultCallbackActivity> mCallbackMap;
  
  public StartActivityDelegate(IStartActivityAdaptor startActivityAdaptor) {
    mStartActivityAdaptor = startActivityAdaptor;
    mCallbackMap = new SparseArray<IResultCallbackActivity>();
  }

  @Override
  public void goToActivity(Class<?> screen) {
    Intent intent = new Intent(mStartActivityAdaptor.getContext(), screen);
    mStartActivityAdaptor.startActivity(intent);
  }
  
  @Override
  public void reorderActivityToFront(Class<?> screen, Bundle extras) {
    Intent intent = new Intent(mStartActivityAdaptor.getContext(), screen);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    intent.putExtras(extras);
    mStartActivityAdaptor.startActivity(intent);
  }
  
  @Override
  public void goToActivityWithNoAnimation(Class<?> screen) {    
    goToActivityWithAnimation(screen, 0, 0);
  }  

  @Override
  public void goToActivityWithNoAnimation(Class<?> screen, Bundle extras) {
    goToActivityWithAnimation(screen, extras, 0, 0);  
  }
  
  @Override
  public void goToActivityWithAnimation(Class<?> screen, int enterAnim,
      int exitAnim) {
    goToActivity(screen);
    mStartActivityAdaptor.overridePendingTransition(enterAnim, exitAnim);
  }
  
  @Override
  public void goToActivityWithAnimation(Class<?> screen, Bundle extras,
      int enterAnim, int exitAnim) {
    Intent intent = new Intent(mStartActivityAdaptor.getContext(), screen);
    intent.putExtras(extras);
    mStartActivityAdaptor.startActivity(intent);
    mStartActivityAdaptor.overridePendingTransition(enterAnim, exitAnim);
  }
  

  @Override
  public void goToActivityWithAnimationSettingFlagClearTop(Class<?> screen,
      int enterAnim, int exitAnim) {
    Intent intent = new Intent(mStartActivityAdaptor.getContext(), screen);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mStartActivityAdaptor.startActivity(intent);
    mStartActivityAdaptor.overridePendingTransition(enterAnim, exitAnim);
  }
  
  @Override
  public void goToActivity(String action) {
    Intent intent = new Intent(action);
    mStartActivityAdaptor.startActivity(intent);
  }

  @Override
  public void openBrowser(String url) {
    Intent browserIntent = new Intent(Intent.ACTION_VIEW,
        Uri.parse(url));
    mStartActivityAdaptor.startActivity(browserIntent);    
  }
  
  @Override
  public void launchSubActivity(Class<?> subActivityClass, IResultCallbackActivity callback) {
    Intent i = new Intent(mStartActivityAdaptor.getContext(), subActivityClass);
    
    launchSubActivity(i, callback);
  }
  
  @Override
  public void launchSubActivity(Intent i, IResultCallbackActivity callback) {
    int correlationId = new Random().nextInt(9999);

    mCallbackMap.put(correlationId, callback);
    mStartActivityAdaptor.startActivityForResult(i, correlationId);
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
      IResultCallbackActivity callback = mCallbackMap.get(requestCode);

      switch (resultCode) {
        case Activity.RESULT_CANCELED:
          callback.onResultCancel(data);
          mCallbackMap.remove(requestCode);
          break;
        case Activity.RESULT_OK:
          callback.onResultOk(data);
          mCallbackMap.remove(requestCode);
          break;
        default:
          Logcat.e("Couldn't find callback handler for correlationId");
      }
    } catch (Exception e) {
      Logcat.e("Problem processing result from sub-activity", e);
    }
  }

  @Override
  public void startService(Class<?> cl) {
    Intent i = new Intent(mStartActivityAdaptor.getContext(), cl);
    mStartActivityAdaptor.startService(i);
  }

  @Override
  public void stopService(Class<?> cl) {
    Intent i = new Intent(mStartActivityAdaptor.getContext(), cl);
    mStartActivityAdaptor.stopService(i);
  }

  @Override
  public void bindService(Service service, Class<?> serviceClass,
      ServiceConnection serviceConnection) {
    if (service == null) {
      Logcat.d("Binding Service: " + serviceClass.getName());
      mStartActivityAdaptor.bindService(new Intent(mStartActivityAdaptor.getContext(), serviceClass), serviceConnection,
          Context.BIND_AUTO_CREATE);
    }
  }

  @Override
  public void pushActivity(Class<?> screen) {
    goToActivityWithAnimation(screen, R.anim.slide_left_enter, R.anim.slide_left_exit);
  }

  @Override
  public void pushActivity(Class<?> screen, Bundle extras) {
    goToActivityWithAnimation(screen, extras, R.anim.slide_left_enter, R.anim.slide_left_exit);
  }

  @Override
  public void popActivity(Class<?> screen) {
    goToActivityWithAnimation(screen, R.anim.slide_right_enter, R.anim.slide_right_exit);
  }

  @Override
  public void popActivity(Class<?> screen, Bundle extras) {
    goToActivityWithAnimation(screen, extras, R.anim.slide_right_enter, R.anim.slide_right_exit);
  }

}




Java Source Code List

com.ocypode.BuildConfig.java
com.ocypode.BuildConfig.java
com.ocypode.activity.IResultCallbackActivity.java
com.ocypode.activity.robo.AbstractRoboActivity.java
com.ocypode.activity.robo.AbstractRoboFragmentActivity.java
com.ocypode.activity.robo.fragment.AbstractRoboFragment.java
com.ocypode.bluetooth.BluetoothHelper.java
com.ocypode.component.Typefaced.java
com.ocypode.component.animation.AnimationCallbackListener.java
com.ocypode.component.delegate.IStartActivityDelegate.java
com.ocypode.component.delegate.StartActivityDelegate.java
com.ocypode.component.delegate.adaptor.IStartActivityAdaptor.java
com.ocypode.component.delegate.adaptor.StartActivityAdaptor.java
com.ocypode.component.delegate.adaptor.StartFragmentAdaptor.java
com.ocypode.component.view.button.TypefacedButton.java
com.ocypode.component.view.dialog.AlertDialogBuilder.java
com.ocypode.component.view.dialog.ProgressDialogBuilder.java
com.ocypode.component.view.dialog.titledescription.TitleDescriptionArrayAdapter.java
com.ocypode.component.view.dialog.titledescription.TitleDescriptionModel.java
com.ocypode.component.view.edittext.TypefacedEditText.java
com.ocypode.component.view.imageview.CircleImageView.java
com.ocypode.component.view.listview.LockableListView.java
com.ocypode.component.view.scrollview.LockableScrollView.java
com.ocypode.component.view.textview.AutoResizeTextView.java
com.ocypode.component.view.textview.TypefacedTextView.java
com.ocypode.repository.AbstractRepository.java
com.ocypode.utility.ICallback.java
com.ocypode.utility.Logcat.java
com.ocypode.utility.Utility.java
com.ocypode.utility.bitmap.BitmapUtil.java
com.ocypode.utility.formatter.CurrencyFormatter.java
com.ocypode.utility.formatter.DateFormatter.java
com.ocypode.utility.formatter.NumberFormatter.java
com.ocypode.utility.validation.StringValidation.java
com.ocypode.widgets.list.OCListView.java