1 /* 2 * Copyright (C) 2008 Esmertec AG. 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package com.android.im.app; 18 19 import android.app.Activity; 20 import android.os.Handler; 21 import android.os.Bundle; 22 import android.os.RemoteException; 23 import android.content.ContentValues; 24 import android.content.Intent; 25 import android.content.ContentResolver; 26 import android.net.Uri; 27 import android.util.Log; 28 import android.database.Cursor; 29 import com.android.im.IImConnection; 30 import com.android.im.provider.Imps; 31 32 33 public class SignoutActivity extends Activity { 34 35 private String[] ACCOUNT_SELECTION = new String[] { 36 Imps.Account._ID, 37 Imps.Account.PROVIDER, 38 }; 39 40 private ImApp mApp; 41 42 private Handler mHandler = new Handler(); 43 44 @Override onCreate(Bundle icicle)45 protected void onCreate(Bundle icicle) { 46 super.onCreate(icicle); 47 48 Intent intent = getIntent(); 49 Uri data = intent.getData(); 50 if (data == null) { 51 Log.e(ImApp.LOG_TAG, "Need account data to sign in"); 52 finish(); 53 return; 54 } 55 56 ContentResolver cr = getContentResolver(); 57 Cursor c = cr.query(data, 58 ACCOUNT_SELECTION, 59 null /* selection */, 60 null /* selection args */, 61 null /* sort order */); 62 final long providerId; 63 final long accountId; 64 65 try { 66 if (!c.moveToFirst()) { 67 Log.w(ImApp.LOG_TAG, "[SignoutActivity] No data for " + data); 68 finish(); 69 return; 70 } 71 72 providerId = c.getLong(c.getColumnIndexOrThrow(Imps.Account.PROVIDER)); 73 accountId = c.getLong(c.getColumnIndexOrThrow(Imps.Account._ID)); 74 } finally { 75 c.close(); 76 } 77 78 mApp = ImApp.getApplication(this); 79 mApp.callWhenServiceConnected(mHandler, new Runnable() { 80 public void run() { 81 signOut(providerId, accountId); 82 } 83 }); 84 } 85 signOut(long providerId, long accountId)86 private void signOut(long providerId, long accountId) { 87 try { 88 IImConnection conn = mApp.getConnection(providerId); 89 if (conn != null) { 90 conn.logout(); 91 } else { 92 // Normally, we can always get the connection when user chose to 93 // sign out. However, if the application crash unexpectedly, the 94 // status will never be updated. Clear the status in this case 95 // to make it recoverable from the crash. 96 ContentValues values = new ContentValues(2); 97 values.put(Imps.AccountStatus.PRESENCE_STATUS, 98 Imps.Presence.OFFLINE); 99 values.put(Imps.AccountStatus.CONNECTION_STATUS, 100 Imps.ConnectionStatus.OFFLINE); 101 String where = Imps.AccountStatus.ACCOUNT + "=?"; 102 getContentResolver().update(Imps.AccountStatus.CONTENT_URI, 103 values, where, 104 new String[] { Long.toString(accountId) }); 105 } 106 } catch (RemoteException ex) { 107 Log.e(ImApp.LOG_TAG, "signout: caught ", ex); 108 } finally { 109 finish(); 110 } 111 } 112 113 @Override onStop()114 protected void onStop() { 115 super.onStop(); 116 117 // always call finish here, because we don't want to be in the backlist ever, and 118 // we don't handle onRestart() 119 finish(); 120 } 121 122 log(String msg)123 static void log(String msg) { 124 Log.d(ImApp.LOG_TAG, "[Signout] " + msg); 125 } 126 } 127