Example usage for android.app Activity startActivityForResult

List of usage examples for android.app Activity startActivityForResult

Introduction

In this page you can find the example usage for android.app Activity startActivityForResult.

Prototype

public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) 

Source Link

Document

Same as calling #startActivityForResult(Intent,int,Bundle) with no options.

Usage

From source file:eu.trentorise.smartcampus.ac.authenticator.AMSCAccessProvider.java

@Override
public String promote(final Activity activity, String inAuthority, final String token) {
    final String authority = inAuthority == null ? Constants.AUTHORITY_DEFAULT : inAuthority;
    final AccountManager am = AccountManager.get(activity);
    //      Bundle options = new Bundle();
    //      if (token != null) {
    //         options.putString(Constants.PROMOTION_TOKEN, token);
    //      }/*from  ww w .j ava2 s  .c o m*/
    final Account a = new Account(Constants.getAccountName(activity), Constants.getAccountType(activity));
    final String userDataString = am.getUserData(a, AccountManager.KEY_USERDATA);

    invalidateToken(activity, authority);

    am.getAuthToken(new Account(Constants.getAccountName(activity), Constants.getAccountType(activity)),
            authority, null, null, new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> result) {
                    Bundle bundle = null;
                    try {
                        bundle = result.getResult();
                        Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT);
                        if (launch != null) {
                            launch.putExtra(Constants.KEY_AUTHORITY, authority);
                            launch.putExtra(Constants.PROMOTION_TOKEN, token);
                            launch.putExtra(Constants.OLD_DATA, userDataString);
                            activity.startActivityForResult(launch, SC_AUTH_ACTIVITY_REQUEST_CODE);
                        } else if (bundle.getString(AccountManager.KEY_AUTHTOKEN) != null) {
                            //                         am.setAuthToken(a, authority, bundle.getString(AccountManager.KEY_AUTHTOKEN));
                            //                         am.addAccountExplicitly(a, null, null);
                            // no token acquired
                        } else {
                            storeAnonymousToken(token, authority, am, a);
                        }
                    } catch (Exception e) {
                        // revert the invalidated token
                        storeAnonymousToken(token, authority, am, a);
                        return;
                    }
                }
            }, null);
    return null;
}

From source file:org.opendatakit.tables.utils.CollectUtil.java

/**
 * Launch collect with the given intent. This method should be used rather
 * than launching the activity yourself because the rowId needs to be retained
 * in order to update the database.//  w ww .  j  a v  a2  s . c om
 *
 * @param activityToAwaitReturn
 * @param collectEditIntent
 * @param rowId
 */
public static void launchCollectToEditRow(Activity activityToAwaitReturn, Intent collectEditIntent,
        String rowId) {
    // We want to be able to launch an edit row action from a variety of
    // different activities, such as the spreadsheet and the webviews. In
    // order to update the database, we must know what the row id of the row
    // was which we are editing. There appears to be no way to pass this
    // information to collect and have it return it to us, so we're going to
    // store it in a shared preference.
    //
    // Note that we aren't storing this in the key value store because it is
    // a very temporary bit of state that would be meaningless if the call
    // and return to/from collect was interrupted.
    SharedPreferences preferences = activityToAwaitReturn.getSharedPreferences(SHARED_PREFERENCE_NAME,
            Context.MODE_PRIVATE);
    preferences.edit().putString(PREFERENCE_KEY_EDITED_ROW_ID, rowId).commit();
    activityToAwaitReturn.startActivityForResult(collectEditIntent, Constants.RequestCodes.EDIT_ROW_COLLECT);
}

From source file:com.laevatein.SelectionSpecBuilder.java

/**
 * Start to select photo.//from   ww w .  j a  v  a 2  s.c o  m
 * @param requestCode identity of the requester activity.
 */
public void forResult(int requestCode) {
    Activity activity = mLaevatein.getActivity();
    if (activity == null) {
        return; // cannot continue;
    }

    mSelectionSpec.setMimeTypeSet(mMimeType);

    ViewResourceSpec viewSpec = new ViewResourceSpec.Builder().setActionViewResources(mActionViewResources)
            .setAlbumViewResources(mAlbumViewResources).setCountViewResources(mCountViewResources)
            .setItemViewResources(mItemViewResources).setEnableCapture(mEnableCapture)
            .setEnableSelectedView(mEnableSelectedView).setActivityOrientation(mActivityOrientation).create();
    ErrorViewSpec errorSpec = new ErrorViewSpec.Builder().setCountSpec(mCountErrorSpec)
            .setOverQualitySpec(mOverQualityErrorSpec).setUnderQualitySpec(mUnderQualityErrorSpec)
            .setTypeSpec(mTypeErrorSpec).create();

    if (mPhotoSelectionActivityClass == null) {
        mPhotoSelectionActivityClass = PhotoSelectionActivity.class;
    }

    Intent intent = new Intent(activity, mPhotoSelectionActivityClass);
    intent.putExtra(PhotoSelectionActivity.EXTRA_VIEW_SPEC, viewSpec);
    intent.putExtra(PhotoSelectionActivity.EXTRA_ERROR_SPEC, errorSpec);
    intent.putExtra(PhotoSelectionActivity.EXTRA_SELECTION_SPEC, mSelectionSpec);
    intent.putParcelableArrayListExtra(PhotoSelectionActivity.EXTRA_RESUME_LIST,
            (ArrayList<? extends android.os.Parcelable>) mResumeList);

    Fragment fragment = mLaevatein.getFragment();
    if (fragment != null) {
        fragment.startActivityForResult(intent, requestCode);
    } else {
        activity.startActivityForResult(intent, requestCode);
    }
}

From source file:cm.aptoide.com.facebook.android.Facebook.java

/**
 * Internal method to handle single sign-on backend for authorize().
 *
 * @param activity/*from  ww  w  .ja  va2  s  .c  o  m*/
 *            The Android Activity that will parent the ProxyAuth Activity.
 * @param applicationId
 *            The Facebook application identifier.
 * @param permissions
 *            A list of permissions required for this application. If you do
 *            not require any permissions, pass an empty String array.
 * @param activityCode
 *            Activity code to uniquely identify the result Intent in the
 *            callback.
 */
private boolean startSingleSignOn(Activity activity, String applicationId, String[] permissions,
        int activityCode) {
    boolean didSucceed = true;
    Intent intent = new Intent();

    intent.setClassName("com.facebook.katana", "com.facebook.katana.ProxyAuth");
    intent.putExtra("client_id", applicationId);
    if (permissions.length > 0) {
        intent.putExtra("scope", TextUtils.join(",", permissions));
    }

    // Verify that the application whose package name is
    // com.facebook.katana.ProxyAuth
    // has the expected FB app signature.
    if (!validateActivityIntent(activity, intent)) {
        return false;
    }

    mAuthActivity = activity;
    mAuthPermissions = permissions;
    mAuthActivityCode = activityCode;
    try {
        activity.startActivityForResult(intent, activityCode);
    } catch (ActivityNotFoundException e) {
        didSucceed = false;
    }

    return didSucceed;
}

From source file:com.zhihu.matisse.SelectionSpecBuilder.java

/**
 * Start to select media and wait for result.
 *
 * @param requestCode Identity of the request Activity or Fragment.
 *//*from   w ww .j a v a2  s .co  m*/
public void forResult(int requestCode) {
    Activity activity = mMatisse.getActivity();
    if (activity == null) {
        return;
    }

    mSelectionSpec.mimeTypeSet = mMimeType;
    if (mThemeId == 0) {
        mThemeId = R.style.Matisse_Zhihu;
    }
    mSelectionSpec.themeId = mThemeId;
    mSelectionSpec.orientation = mOrientation;

    if (mMaxSelectable <= 1) {
        mSelectionSpec.countable = false;
        mSelectionSpec.maxSelectable = 1;
    } else {
        mSelectionSpec.countable = mCountable;
        mSelectionSpec.maxSelectable = mMaxSelectable;
    }

    if (mFilters != null && mFilters.size() > 0) {
        mSelectionSpec.filters = mFilters;
    }
    mSelectionSpec.capture = mCapture;
    if (mCapture) {
        if (mCaptureStrategy == null) {
            throw new IllegalArgumentException("Don't forget to set CaptureStrategy.");
        }
        mSelectionSpec.captureStrategy = mCaptureStrategy;
    }

    if (mGridExpectedSize > 0) {
        mSelectionSpec.gridExpectedSize = mGridExpectedSize;
    } else {
        mSelectionSpec.spanCount = mSpanCount <= 0 ? 3 : mSpanCount;
    }

    if (mThumbnailScale < 0 || mThumbnailScale > 1.0f) {
        throw new IllegalArgumentException("Thumbnail scale must be between (0.0, 1.0]");
    }
    if (mThumbnailScale == 0) {
        mThumbnailScale = 0.5f;
    }
    mSelectionSpec.thumbnailScale = mThumbnailScale;

    mSelectionSpec.imageEngine = mImageEngine;

    Intent intent = new Intent(activity, MatisseActivity.class);

    Fragment fragment = mMatisse.getFragment();
    if (fragment != null) {
        fragment.startActivityForResult(intent, requestCode);
    } else {
        activity.startActivityForResult(intent, requestCode);
    }
}

From source file:jp.mixi.android.sdk.MixiContainerImpl.java

private boolean startSingleSignOn(Activity activity, int activityCode, String[] permissions) {
    // ?????intent????
    Intent intent = new Intent();
    intent.putExtra("mode", "authorize");
    intent.setClassName(OFFICIAL_PACKAGE, AUTH_ACTIVITY);
    intent.putExtra(CLIENT_ID, mClientId);
    // ??//  w  w  w. j  a v  a 2 s . c o  m
    if (permissions != null && permissions.length > 0) {
        intent.putExtra("scope", TextUtils.join(" ", permissions));
    }
    // state?
    intent.putExtra(STATE, String.valueOf(new Random().nextLong() >>> 1));

    if (!validateOfficialAppsForIntent(activity, intent, VALIDATE_OFFICIAL_FOR_ACTIVITY, SUPPORTED_VERSION)) {
        return false;
    }

    try {
        activity.startActivityForResult(intent, activityCode);
    } catch (ActivityNotFoundException e) {
        Log.v(TAG, e.getMessage());
        return false;
    }
    return true;
}

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

public static void openForView(TLObject media, Activity activity) throws Exception {
    if (media == null || activity == null) {
        return;/*from  w  ww. j a v a  2 s .  c o m*/
    }
    String fileName = FileLoader.getAttachFileName(media);
    File f = FileLoader.getPathToAttach(media, true);
    if (f != null && f.exists()) {
        String realMimeType = null;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        MimeTypeMap myMime = MimeTypeMap.getSingleton();
        int idx = fileName.lastIndexOf('.');
        if (idx != -1) {
            String ext = fileName.substring(idx + 1);
            realMimeType = myMime.getMimeTypeFromExtension(ext.toLowerCase());
            if (realMimeType == null) {
                if (media instanceof TLRPC.TL_document) {
                    realMimeType = ((TLRPC.TL_document) media).mime_type;
                }
                if (realMimeType == null || realMimeType.length() == 0) {
                    realMimeType = null;
                }
            }
        }
        if (Build.VERSION.SDK_INT >= 24) {
            intent.setDataAndType(
                    FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", f),
                    realMimeType != null ? realMimeType : "text/plain");
        } else {
            intent.setDataAndType(Uri.fromFile(f), realMimeType != null ? realMimeType : "text/plain");
        }
        if (realMimeType != null) {
            try {
                activity.startActivityForResult(intent, 500);
            } catch (Exception e) {
                if (Build.VERSION.SDK_INT >= 24) {
                    intent.setDataAndType(
                            FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", f),
                            "text/plain");
                } else {
                    intent.setDataAndType(Uri.fromFile(f), "text/plain");
                }
                activity.startActivityForResult(intent, 500);
            }
        } else {
            activity.startActivityForResult(intent, 500);
        }
    }
}

From source file:jp.mixi.android.sdk.MixiContainerImpl.java

@Override
public void requestPayment(final Activity activity, final PaymentParameter param, final int activityCode,
        final CallbackListener listener) {
    mPaymentCallbackListener = listener;
    mPaymentRequestCode = activityCode;/*from w  ww .java  2s .c  o  m*/
    if (!validatePaymentParam(param)) {
        listener.onError(new ErrorInfo("parameter invalid", ErrorInfo.OTHER_ERROR));
        return;
    }

    Intent intent = new Intent();
    intent.setClassName(OFFICIAL_PACKAGE, PAYMENT_ACTIVITY);
    intent.putExtras(convertPaymentBundle(param));
    if (!validateOfficialAppsForIntent(activity, intent, VALIDATE_OFFICIAL_FOR_ACTIVITY,
            PAYMENT_SUPPORTED_VERSION)) {
        listener.onFatal(new ErrorInfo(activity.getString(R.string.err_unsupported_versions),
                ErrorInfo.OFFICIAL_APP_NOT_FOUND));

        return;
    }
    try {
        activity.startActivityForResult(intent, activityCode);
    } catch (ActivityNotFoundException e) {
        Log.v(TAG, e.getMessage());
        listener.onFatal(new ErrorInfo(e));
    }

}

From source file:com.todoroo.astrid.activity.TaskListFragment.java

public boolean handleOptionsMenuItemSelected(int id, Intent intent) {
    Activity activity = getActivity();
    switch (id) {
    case MENU_SORT_ID:
        StatisticsService.reportEvent(StatisticsConstants.TLA_MENU_SORT);
        if (activity != null) {
            AlertDialog dialog = SortSelectionActivity.createDialog(getActivity(), hasDraggableOption(), this,
                    sortFlags, sortSort);
            dialog.show();/*from w  w  w .j  a  v  a2s .com*/
        }
        return true;
    case MENU_SYNC_ID:
        StatisticsService.reportEvent(StatisticsConstants.TLA_MENU_SYNC);
        syncActionHelper.performSyncAction();
        return true;
    case MENU_ADDON_INTENT_ID:
        if (activity != null)
            AndroidUtilities.startExternalIntent(activity, intent, ACTIVITY_MENU_EXTERNAL);
        return true;
    case MENU_NEW_FILTER_ID:
        if (activity != null) {
            intent = new Intent(activity, CustomFilterActivity.class);
            activity.startActivityForResult(intent, ACTIVITY_REQUEST_NEW_FILTER);
            return true;
        }
    }
    return false;
}

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

public static void openForView(MessageObject message, Activity activity) throws Exception {
    File f = null;// www . j a  v  a2s .com
    String fileName = message.getFileName();
    if (message.messageOwner.attachPath != null && message.messageOwner.attachPath.length() != 0) {
        f = new File(message.messageOwner.attachPath);
    }
    if (f == null || !f.exists()) {
        f = FileLoader.getPathToMessage(message.messageOwner);
    }
    if (f != null && f.exists()) {
        String realMimeType = null;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        MimeTypeMap myMime = MimeTypeMap.getSingleton();
        int idx = fileName.lastIndexOf('.');
        if (idx != -1) {
            String ext = fileName.substring(idx + 1);
            realMimeType = myMime.getMimeTypeFromExtension(ext.toLowerCase());
            if (realMimeType == null) {
                if (message.type == 9 || message.type == 0) {
                    realMimeType = message.getDocument().mime_type;
                }
                if (realMimeType == null || realMimeType.length() == 0) {
                    realMimeType = null;
                }
            }
        }
        if (Build.VERSION.SDK_INT >= 24) {
            intent.setDataAndType(
                    FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", f),
                    realMimeType != null ? realMimeType : "text/plain");
        } else {
            intent.setDataAndType(Uri.fromFile(f), realMimeType != null ? realMimeType : "text/plain");
        }
        if (realMimeType != null) {
            try {
                activity.startActivityForResult(intent, 500);
            } catch (Exception e) {
                if (Build.VERSION.SDK_INT >= 24) {
                    intent.setDataAndType(
                            FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", f),
                            "text/plain");
                } else {
                    intent.setDataAndType(Uri.fromFile(f), "text/plain");
                }
                activity.startActivityForResult(intent, 500);
            }
        } else {
            activity.startActivityForResult(intent, 500);
        }
    }
}