/** * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. * * You are hereby granted a non-exclusive, worldwide, royalty-free license to use, * copy, modify, and distribute this software in source code or binary form for use * in connection with the web services and APIs provided by Facebook. * * As with any software that integrates with the Facebook platform, your use of * this software is subject to the Facebook Developer Principles and Policies * [http://developers.facebook.com/policy/]. This copyright notice shall be * included in all copies or substantial portions of the software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.facebook.internal; import android.content.Intent; import com.facebook.CallbackManager; import com.facebook.FacebookSdk; import java.util.HashMap; import java.util.Map; /** * com.facebook.internal is solely for the use of other packages within the * Facebook SDK for Android. Use of any of the classes in this package is * unsupported, and they may be modified or removed without warning at any time. */ public final class CallbackManagerImpl implements CallbackManager { private static Map<Integer, Callback> staticCallbacks = new HashMap<>(); /** * If there is no explicit callback, but we still need to call the Facebook component, * because it's going to update some state, e.g., login, like. Then we should register a * static callback that can still handle the response. * @param requestCode The request code. * @param callback The callback for the feature. */ public synchronized static void registerStaticCallback( int requestCode, Callback callback) { Validate.notNull(callback, "callback"); if (staticCallbacks.containsKey(requestCode)) { return; } staticCallbacks.put(requestCode, callback); } private static synchronized Callback getStaticCallback(Integer requestCode) { return staticCallbacks.get(requestCode); } private static boolean runStaticCallback( int requestCode, int resultCode, Intent data) { Callback callback = getStaticCallback(requestCode); if (callback != null) { return callback.onActivityResult(resultCode, data); } return false; } private Map<Integer, Callback> callbacks = new HashMap<>(); public void registerCallback(int requestCode, Callback callback) { Validate.notNull(callback, "callback"); callbacks.put(requestCode, callback); } @Override public boolean onActivityResult(int requestCode, int resultCode, Intent data) { Callback callback = callbacks.get(requestCode); if (callback != null) { return callback.onActivityResult(resultCode, data); } return runStaticCallback(requestCode, resultCode, data); } public interface Callback { public boolean onActivityResult(int resultCode, Intent data); } public enum RequestCodeOffset { Login(0), Share(1), Message(2), Like(3), GameRequest(4), AppGroupCreate(5), AppGroupJoin(6), AppInvite(7), DeviceShare(8), ; private final int offset; RequestCodeOffset(int offset) { this.offset = offset; } public int toRequestCode() { return FacebookSdk.getCallbackRequestCodeOffset() + offset; } } }