1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5package org.chromium.ui; 6 7import android.app.Activity; 8import android.content.ActivityNotFoundException; 9import android.content.Context; 10import android.content.Intent; 11 12/** 13 * The class provides the WindowAndroid's implementation which requires 14 * Activity Instance. 15 * Only instantiate this class when you need the implemented features. 16 */ 17public class ActivityWindowAndroid extends WindowAndroid { 18 // Constants used for intent request code bounding. 19 private static final int REQUEST_CODE_PREFIX = 1000; 20 private static final int REQUEST_CODE_RANGE_SIZE = 100; 21 private static final String TAG = "ActivityWindowAndroid"; 22 23 private Activity mActivity; 24 private int mNextRequestCode = 0; 25 26 public ActivityWindowAndroid(Activity activity) { 27 super(activity.getApplicationContext()); 28 mActivity = activity; 29 } 30 31 @Override 32 public boolean showIntent(Intent intent, IntentCallback callback, int errorId) { 33 int requestCode = REQUEST_CODE_PREFIX + mNextRequestCode; 34 mNextRequestCode = (mNextRequestCode + 1) % REQUEST_CODE_RANGE_SIZE; 35 36 try { 37 mActivity.startActivityForResult(intent, requestCode); 38 } catch (ActivityNotFoundException e) { 39 return false; 40 } 41 42 mOutstandingIntents.put(requestCode, callback); 43 mIntentErrors.put(requestCode, mApplicationContext.getString(errorId)); 44 45 return true; 46 } 47 48 @Override 49 public boolean onActivityResult(int requestCode, int resultCode, Intent data) { 50 IntentCallback callback = mOutstandingIntents.get(requestCode); 51 mOutstandingIntents.delete(requestCode); 52 String errorMessage = mIntentErrors.remove(requestCode); 53 54 if (callback != null) { 55 callback.onIntentCompleted(this, resultCode, 56 mApplicationContext.getContentResolver(), data); 57 return true; 58 } else { 59 if (errorMessage != null) { 60 showCallbackNonExistentError(errorMessage); 61 return true; 62 } 63 } 64 return false; 65 } 66 67 @Override 68 @Deprecated 69 public Context getContext() { 70 return mActivity; 71 } 72} 73