Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
/**
2
 * Copyright 2010-present Facebook.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.facebook;
18
 
19
import android.app.Activity;
20
import android.content.Intent;
21
import android.os.Bundle;
22
import android.util.Log;
23
import android.view.View;
24
import com.facebook.android.R;
25
 
26
/**
27
 * This Activity is a necessary part of the overall Facebook login process
28
 * but is not meant to be used directly. Add this activity to your
29
 * AndroidManifest.xml to ensure proper handling of Facebook login.
30
 * <pre>
31
 * {@code
32
 * <activity android:name="com.facebook.LoginActivity"
33
 *           android:theme="@android:style/Theme.Translucent.NoTitleBar"
34
 *           android:label="@string/app_name" />
35
 * }
36
 * </pre>
37
 * Do not start this activity directly.
38
 */
39
public class LoginActivity extends Activity {
40
    static final String RESULT_KEY = "com.facebook.LoginActivity:Result";
41
 
42
    private static final String TAG = LoginActivity.class.getName();
43
    private static final String NULL_CALLING_PKG_ERROR_MSG =
44
            "Cannot call LoginActivity with a null calling package. " +
45
                    "This can occur if the launchMode of the caller is singleInstance.";
46
    private static final String SAVED_CALLING_PKG_KEY = "callingPackage";
47
    private static final String SAVED_AUTH_CLIENT = "authorizationClient";
48
    private static final String EXTRA_REQUEST = "request";
49
 
50
    private String callingPackage;
51
    private AuthorizationClient authorizationClient;
52
    private AuthorizationClient.AuthorizationRequest request;
53
 
54
    @Override
55
    public void onCreate(Bundle savedInstanceState) {
56
        super.onCreate(savedInstanceState);
57
        setContentView(R.layout.com_facebook_login_activity_layout);
58
 
59
        if (savedInstanceState != null) {
60
            callingPackage = savedInstanceState.getString(SAVED_CALLING_PKG_KEY);
61
            authorizationClient = (AuthorizationClient) savedInstanceState.getSerializable(SAVED_AUTH_CLIENT);
62
        } else {
63
            callingPackage = getCallingPackage();
64
            authorizationClient = new AuthorizationClient();
65
            request = (AuthorizationClient.AuthorizationRequest) getIntent().getSerializableExtra(EXTRA_REQUEST);
66
        }
67
 
68
        authorizationClient.setContext(this);
69
        authorizationClient.setOnCompletedListener(new AuthorizationClient.OnCompletedListener() {
70
            @Override
71
            public void onCompleted(AuthorizationClient.Result outcome) {
72
                onAuthClientCompleted(outcome);
73
            }
74
        });
75
        authorizationClient.setBackgroundProcessingListener(new AuthorizationClient.BackgroundProcessingListener() {
76
            @Override
77
            public void onBackgroundProcessingStarted() {
78
                findViewById(R.id.com_facebook_login_activity_progress_bar).setVisibility(View.VISIBLE);
79
            }
80
 
81
            @Override
82
            public void onBackgroundProcessingStopped() {
83
                findViewById(R.id.com_facebook_login_activity_progress_bar).setVisibility(View.GONE);
84
            }
85
        });
86
    }
87
 
88
    private void onAuthClientCompleted(AuthorizationClient.Result outcome) {
89
        request = null;
90
 
91
        int resultCode = (outcome.code == AuthorizationClient.Result.Code.CANCEL) ?
92
                RESULT_CANCELED : RESULT_OK;
93
 
94
        Bundle bundle = new Bundle();
95
        bundle.putSerializable(RESULT_KEY, outcome);
96
 
97
        Intent resultIntent = new Intent();
98
        resultIntent.putExtras(bundle);
99
        setResult(resultCode, resultIntent);
100
 
101
        finish();
102
    }
103
 
104
    @Override
105
    public void onResume() {
106
        super.onResume();
107
 
108
        // If the calling package is null, this generally means that the callee was started
109
        // with a launchMode of singleInstance. Unfortunately, Android does not allow a result
110
        // to be set when the callee is a singleInstance, so we log an error and return.
111
        if (callingPackage == null) {
112
            Log.e(TAG, NULL_CALLING_PKG_ERROR_MSG);
113
            finish();
114
            return;
115
        }
116
 
117
        authorizationClient.startOrContinueAuth(request);
118
    }
119
 
120
    @Override
121
    public void onPause() {
122
        super.onPause();
123
 
124
        authorizationClient.cancelCurrentHandler();
125
        findViewById(R.id.com_facebook_login_activity_progress_bar).setVisibility(View.GONE);
126
    }
127
 
128
    @Override
129
    public void onSaveInstanceState(Bundle outState) {
130
        super.onSaveInstanceState(outState);
131
 
132
        outState.putString(SAVED_CALLING_PKG_KEY, callingPackage);
133
        outState.putSerializable(SAVED_AUTH_CLIENT, authorizationClient);
134
    }
135
 
136
    @Override
137
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
138
        authorizationClient.onActivityResult(requestCode, resultCode, data);
139
    }
140
 
141
    static Bundle populateIntentExtras(AuthorizationClient.AuthorizationRequest request) {
142
        Bundle extras = new Bundle();
143
        extras.putSerializable(EXTRA_REQUEST, request);
144
        return extras;
145
    }
146
}