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.internal;
18
 
19
import android.content.BroadcastReceiver;
20
import android.content.Context;
21
import android.content.Intent;
22
import android.content.IntentFilter;
23
import android.support.v4.content.LocalBroadcastManager;
24
import com.facebook.Session;
25
import com.facebook.SessionState;
26
 
27
/**
28
 * com.facebook.internal is solely for the use of other packages within the Facebook SDK for Android. Use of
29
 * any of the classes in this package is unsupported, and they may be modified or removed without warning at
30
 * any time.
31
 */
32
public class SessionTracker {
33
 
34
    private Session session;
35
    private final Session.StatusCallback callback;
36
    private final BroadcastReceiver receiver;
37
    private final LocalBroadcastManager broadcastManager;
38
    private boolean isTracking = false;
39
 
40
    /**
41
     * Constructs a SessionTracker to track the active Session object.
42
     * 
43
     * @param context the context object.
44
     * @param callback the callback to use whenever the active Session's 
45
     *                 state changes
46
     */
47
    public SessionTracker(Context context, Session.StatusCallback callback) {
48
        this(context, callback, null);
49
    }
50
 
51
    /**
52
     * Constructs a SessionTracker to track the Session object passed in.
53
     * If the Session is null, then it will track the active Session instead.
54
     * 
55
     * @param context the context object.
56
     * @param callback the callback to use whenever the Session's state changes
57
     * @param session the Session object to track
58
     */
59
    SessionTracker(Context context, Session.StatusCallback callback, Session session) {
60
        this(context, callback, session, true);
61
    }
62
 
63
    /**
64
     * Constructs a SessionTracker to track the Session object passed in.
65
     * If the Session is null, then it will track the active Session instead.
66
     * 
67
     * @param context the context object.
68
     * @param callback the callback to use whenever the Session's state changes
69
     * @param session the Session object to track
70
     * @param startTracking whether to start tracking the Session right away
71
     */
72
    public SessionTracker(Context context, Session.StatusCallback callback, Session session, boolean startTracking) {
73
        this.callback = new CallbackWrapper(callback);
74
        this.session = session;
75
        this.receiver = new ActiveSessionBroadcastReceiver();
76
        this.broadcastManager = LocalBroadcastManager.getInstance(context);
77
 
78
        if (startTracking) {
79
            startTracking();
80
        }
81
    }
82
 
83
    /**
84
     * Returns the current Session that's being tracked.
85
     * 
86
     * @return the current Session associated with this tracker
87
     */
88
    public Session getSession() {
89
        return (session == null) ? Session.getActiveSession() : session;
90
    }
91
 
92
    /**
93
     * Returns the current Session that's being tracked if it's open, 
94
     * otherwise returns null.
95
     * 
96
     * @return the current Session if it's open, otherwise returns null
97
     */
98
    public Session getOpenSession() {
99
        Session openSession = getSession();
100
        if (openSession != null && openSession.isOpened()) {
101
            return openSession;
102
        }
103
        return null;
104
    }
105
 
106
    /**
107
     * Set the Session object to track.
108
     * 
109
     * @param newSession the new Session object to track
110
     */
111
    public void setSession(Session newSession) {
112
        if (newSession == null) {
113
            if (session != null) {
114
                // We're current tracking a Session. Remove the callback
115
                // and start tracking the active Session.
116
                session.removeCallback(callback);
117
                session = null;
118
                addBroadcastReceiver();
119
                if (getSession() != null) {
120
                    getSession().addCallback(callback);
121
                }
122
            }
123
        } else {
124
            if (session == null) {
125
                // We're currently tracking the active Session, but will be
126
                // switching to tracking a different Session object.
127
                Session activeSession = Session.getActiveSession();
128
                if (activeSession != null) {
129
                    activeSession.removeCallback(callback);
130
                }
131
                broadcastManager.unregisterReceiver(receiver);
132
            } else {
133
                // We're currently tracking a Session, but are now switching 
134
                // to a new Session, so we remove the callback from the old 
135
                // Session, and add it to the new one.
136
                session.removeCallback(callback);
137
            }
138
            session = newSession;
139
            session.addCallback(callback);
140
        }
141
    }
142
 
143
    /**
144
     * Start tracking the Session (either active or the one given). 
145
     */
146
    public void startTracking() {
147
        if (isTracking) {
148
            return;
149
        }
150
        if (this.session == null) {
151
            addBroadcastReceiver();
152
        }        
153
        // if the session is not null, then add the callback to it right away
154
        if (getSession() != null) {
155
            getSession().addCallback(callback);
156
        }
157
        isTracking = true;
158
    }
159
 
160
    /**
161
     * Stop tracking the Session and remove any callbacks attached
162
     * to those sessions.
163
     */
164
    public void stopTracking() {
165
        if (!isTracking) {
166
            return;
167
        }
168
        Session session = getSession();
169
        if (session != null) {
170
            session.removeCallback(callback);
171
        }
172
        broadcastManager.unregisterReceiver(receiver);
173
        isTracking = false;
174
    }
175
 
176
    /**
177
     * Returns whether it's currently tracking the Session.
178
     * 
179
     * @return true if currently tracking the Session
180
     */
181
    public boolean isTracking() {
182
        return isTracking;
183
    }
184
 
185
    /**
186
     * Returns whether it's currently tracking the active Session.
187
     *
188
     * @return true if the currently tracked session is the active Session.
189
     */
190
    public boolean isTrackingActiveSession() {
191
        return session == null;
192
    }
193
 
194
    private void addBroadcastReceiver() {
195
        IntentFilter filter = new IntentFilter();
196
        filter.addAction(Session.ACTION_ACTIVE_SESSION_SET);
197
        filter.addAction(Session.ACTION_ACTIVE_SESSION_UNSET);
198
 
199
        // Add a broadcast receiver to listen to when the active Session
200
        // is set or unset, and add/remove our callback as appropriate    
201
        broadcastManager.registerReceiver(receiver, filter);
202
    }
203
 
204
    /**
205
     * The BroadcastReceiver implementation that either adds or removes the callback
206
     * from the active Session object as it's SET or UNSET.
207
     */
208
    private class ActiveSessionBroadcastReceiver extends BroadcastReceiver {
209
        @Override
210
        public void onReceive(Context context, Intent intent) {
211
            if (Session.ACTION_ACTIVE_SESSION_SET.equals(intent.getAction())) {
212
                Session session = Session.getActiveSession();
213
                if (session != null) {
214
                    session.addCallback(SessionTracker.this.callback);
215
                }
216
            }
217
        }
218
    }
219
 
220
    private class CallbackWrapper implements Session.StatusCallback {
221
 
222
        private final Session.StatusCallback wrapped;
223
        public CallbackWrapper(Session.StatusCallback wrapped) {
224
            this.wrapped = wrapped;
225
        }
226
 
227
        @Override
228
        public void call(Session session, SessionState state, Exception exception) {
229
            if (wrapped != null && isTracking()) {
230
                wrapped.call(session, state, exception);
231
            }
232
            // if we're not tracking the Active Session, and the current session
233
            // is closed, then start tracking the Active Session.
234
            if (session == SessionTracker.this.session && state.isClosed()) {
235
                setSession(null);
236
            }
237
        }
238
    }
239
}