Skip to content

Commit

Permalink
Update Sample Apps with BroadcastReceiver to handle access token revo…
Browse files Browse the repository at this point in the history
…cation
  • Loading branch information
bhariharan committed Jan 8, 2013
1 parent aaa15ab commit 7e0c184
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 65 deletions.
@@ -1,10 +1,37 @@
/*
* Copyright (c) 2012, salesforce.com, inc.
* All rights reserved.
* Redistribution and use of this software in source and binary forms, with or
* without modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of salesforce.com, inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission of salesforce.com, inc.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.salesforce.androidsdk.sample;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
Expand All @@ -25,21 +52,19 @@
import com.salesforce.androidsdk.util.EventsObservable;
import com.salesforce.androidsdk.util.EventsObservable.EventType;

public class AlbumListActivity extends ListActivity{
public class AlbumListActivity extends ListActivity {

private String[] albums;
private String[] albumIds;
private RestClient client;
private String apiVersion;
private TokenRevocationReceiver tokenRevocatinReceiver;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ListView lv = getListView();
apiVersion = getString(R.string.api_version);

lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Expand All @@ -54,7 +79,7 @@ public void onItemClick(AdapterView<?> parent, View view,
}

@Override
public void onResume(){
public void onResume() {
super.onResume();

// Login options
Expand All @@ -65,8 +90,8 @@ public void onResume(){
getString(R.string.oauth_callback_url),
getString(R.string.oauth_client_id),
new String[] {"api"});

new ClientManager(this, accountType, loginOptions).getRestClient(this, new RestClientCallback() {
new ClientManager(this, accountType, loginOptions, ForceApp.APP.shouldLogoutWhenTokenRevoked()).getRestClient(this, new RestClientCallback() {

@Override
public void authenticatedRestClient(RestClient client) {
if (client == null) {
Expand All @@ -77,69 +102,67 @@ public void authenticatedRestClient(RestClient client) {
getAlbums();
}
});
tokenRevocatinReceiver = new TokenRevocationReceiver(this);
registerReceiver(tokenRevocatinReceiver, new IntentFilter(ClientManager.ACCESS_TOKEN_REVOKE_INTENT));
}



private void getAlbums(){
@Override
public void onPause() {
super.onPause();
unregisterReceiver(tokenRevocatinReceiver);
tokenRevocatinReceiver = null;
}

private void getAlbums() {
try {

String soql = "select id, name, description__c, Price__c, Released_On__c from Album__c";
RestRequest request = RestRequest.getRequestForQuery(apiVersion, soql);

client.sendAsync(request, new AsyncRequestCallback() {

@Override
public void onSuccess(RestRequest request, RestResponse response) {
try {
if (response == null || response.asJSONObject() == null)
if (response == null || response.asJSONObject() == null) {
return;

JSONArray records = response.asJSONObject().getJSONArray("records");

if (records.length() == 0)
return;

}
JSONArray records = response.asJSONObject().getJSONArray("records");
if (records.length() == 0) {
return;
}
albums = new String[records.length()];
albumIds = new String[records.length()];

for (int i = 0; i < records.length(); i++){
JSONObject album = (JSONObject)records.get(i);
albums[i] = album.getString("Name");
albumIds[i] = album.getString("Id");
}
ArrayAdapter<String> ad = new ArrayAdapter<String>(AlbumListActivity.this,
R.layout.list_item,
albums);
R.layout.list_item,
albums);
setListAdapter(ad);
EventsObservable.get().notifyEvent(EventType.RenditionComplete);
} catch (Exception e) {
e.printStackTrace();
displayError(e.getMessage());
}
}

@Override
public void onError(Exception exception) {
displayError(exception.getMessage());
EventsObservable.get().notifyEvent(EventType.RenditionComplete);
}
});

} catch (Exception e) {
e.printStackTrace();
displayError(e.getMessage());
}
}


private void displayError(String error) {
ArrayAdapter<String> ad = new ArrayAdapter<String>( AlbumListActivity.this,
R.layout.list_item,
new String[]{"Error retrieving Album data - " + error});
setListAdapter(ad);

R.layout.list_item,
new String[]{"Error retrieving Album data - " + error});
setListAdapter(ad);
}

}
@@ -1,3 +1,29 @@
/*
* Copyright (c) 2012, salesforce.com, inc.
* All rights reserved.
* Redistribution and use of this software in source and binary forms, with or
* without modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of salesforce.com, inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission of salesforce.com, inc.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.salesforce.androidsdk.sample;

import android.app.Activity;
Expand Down
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2013, salesforce.com, inc.
* All rights reserved.
* Redistribution and use of this software in source and binary forms, with or
* without modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of salesforce.com, inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission of salesforce.com, inc.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.salesforce.androidsdk.sample;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.salesforce.androidsdk.app.ForceApp;
import com.salesforce.androidsdk.rest.ClientManager;

/**
* Listens for the access token revocation event, and acts on it.
*
* @author bhariharan
*/
public class TokenRevocationReceiver extends BroadcastReceiver {

private Activity curActivity;

public TokenRevocationReceiver(Activity currentActivity) {
curActivity = currentActivity;
}

@Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction().equals(ClientManager.ACCESS_TOKEN_REVOKE_INTENT)) {
Toast.makeText(curActivity, "Your access token has been revoked by the administrator. You will now be logged out.", Toast.LENGTH_LONG).show();
ForceApp.APP.logout(curActivity, true);
}
}
}

0 comments on commit 7e0c184

Please sign in to comment.