forked from facebook/facebook-android-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This allows developers to silently refresh their access token by calling Facebook.refreshToken method. This SDK will try to call our Facebook Android App which will handle the API call. Test Plan: This requires adding a refresh token service to our sdk. See D364973. After that try using the new Hackbook example. Reviewers: jimbru, raghuc1, brent, dalves, ttung, yariv Reviewed By: jimbru CC: dalves, jimbru Differential Revision: https://phabricator.fb.com/D366540 Task ID: 799996
- Loading branch information
Kamil Kraszewski
authored and
Kamil Kraszewski
committed
Dec 30, 2011
1 parent
50e4fd5
commit a1ebdc9
Showing
5 changed files
with
372 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:background="@color/black"> | ||
<TextView | ||
android:id="@+id/tokenLabel" | ||
android:text="@string/access_token_label" | ||
android:textColor="@color/white" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:paddingTop="3dp" | ||
android:paddingLeft="3dp" /> | ||
<EditText | ||
android:id="@+id/tokenEdit" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:editable="false" /> | ||
<TextView | ||
android:id="@+id/tokenExpiresLabel" | ||
android:text="@string/access_token_expires_label" | ||
android:textColor="@color/white" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:paddingTop="3dp" | ||
android:paddingLeft="3dp" /> | ||
<EditText | ||
android:id="@+id/tokenExpiresEdit" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:editable="false" /> | ||
<View | ||
android:layout_width="fill_parent" | ||
android:layout_height="2dip" | ||
android:background="@color/grey" /> | ||
<LinearLayout | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content"> | ||
<Button | ||
android:id="@+id/refresh_button" | ||
android:text="@string/refresh_button" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:padding="10dp" /> | ||
</LinearLayout> | ||
<View | ||
android:layout_width="fill_parent" | ||
android:layout_height="2dip" | ||
android:background="@color/grey" /> | ||
<TextView | ||
android:id="@+id/tip_label" | ||
android:text="@string/tip_label" | ||
android:textColor="@color/white" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:paddingTop="3dp" | ||
android:paddingLeft="3dp" /> | ||
<TextView | ||
android:id="@+id/usefulTip" | ||
android:text="@string/refresh_token_tip" | ||
android:textColor="@color/white" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:paddingTop="5dp" | ||
android:paddingLeft="3dp" /> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
examples/Hackbook/src/com/facebook/android/TokenRefreshDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.facebook.android; | ||
|
||
import java.text.DateFormat; | ||
import java.util.Date; | ||
|
||
import android.app.Activity; | ||
import android.app.Dialog; | ||
import android.os.Bundle; | ||
import android.text.method.LinkMovementMethod; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.TextView; | ||
|
||
public class TokenRefreshDialog extends Dialog { | ||
|
||
private EditText tokenEdit, tokenExpiresEdit; | ||
private TextView mUsefulTip; | ||
private Button mRefreshButton; | ||
private Activity activity; | ||
|
||
public TokenRefreshDialog(Activity activity) { | ||
super(activity); | ||
this.activity = activity; | ||
setTitle(R.string.refresh_token_title); | ||
} | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
setContentView(R.layout.token_refresh); | ||
|
||
tokenEdit = (EditText) findViewById(R.id.tokenEdit); | ||
tokenEdit.setText(Utility.mFacebook.getAccessToken()); | ||
|
||
tokenExpiresEdit = (EditText) findViewById(R.id.tokenExpiresEdit); | ||
setExpiresAt(Utility.mFacebook.getAccessExpires()); | ||
|
||
mUsefulTip = (TextView) findViewById(R.id.usefulTip); | ||
mUsefulTip.setMovementMethod(LinkMovementMethod.getInstance()); | ||
mRefreshButton = (Button) findViewById(R.id.refresh_button); | ||
|
||
mRefreshButton.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
changeButtonState(false); | ||
RefreshTokenListener listener = new RefreshTokenListener(); | ||
if (!Utility.mFacebook.extendAccessToken(activity, listener)) { | ||
listener.onError(new Error( | ||
activity.getString(R.string.refresh_token_binding_error))); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private class RefreshTokenListener implements Facebook.ServiceListener { | ||
|
||
@Override | ||
public void onFacebookError(FacebookError e) { | ||
changeButtonState(true); | ||
String title = String.format(activity.getString(R.string.facebook_error) + "%d", | ||
e.getErrorCode()); | ||
Util.showAlert(activity, title, e.getMessage()); | ||
} | ||
|
||
@Override | ||
public void onError(Error e) { | ||
changeButtonState(true); | ||
Util.showAlert(activity, activity.getString(R.string.error), e.getMessage()); | ||
} | ||
|
||
@Override | ||
public void onComplete(Bundle values) { | ||
changeButtonState(true); | ||
|
||
// The access_token and expires_at values are automatically updated, | ||
// so they can be obtained by using: | ||
// - Facebook.getAccessToken() | ||
// - Facebook.getAccessExpires() | ||
// methods, but we can also get them from the 'values' bundle. | ||
tokenEdit.setText(values.getString(Facebook.TOKEN)); | ||
setExpiresAt(values.getLong(Facebook.EXPIRES)); | ||
} | ||
} | ||
|
||
private void changeButtonState(boolean enabled) { | ||
mRefreshButton.setEnabled(enabled); | ||
mRefreshButton.setText(enabled ? R.string.refresh_button : R.string.refresh_button_pending); | ||
} | ||
|
||
private void setExpiresAt(long time) { | ||
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); | ||
tokenExpiresEdit.setText(dateFormat.format(new Date(time))); | ||
} | ||
} |
Oops, something went wrong.