summaryrefslogtreecommitdiff
path: root/src/com/android/im/app/SignoutActivity.java
blob: 2cb3bcbdb05dbc4719d7d4fa0a4ba8d060faca17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2008 Esmertec AG.
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.im.app;

import android.app.Activity;
import android.provider.Im;
import android.os.Handler;
import android.os.Bundle;
import android.os.RemoteException;
import android.content.ContentValues;
import android.content.Intent;
import android.content.ContentResolver;
import android.net.Uri;
import android.util.Log;
import android.database.Cursor;
import com.android.im.IImConnection;


public class SignoutActivity extends Activity {

    private String[] ACCOUNT_SELECTION = new String[] {
            Im.Account._ID,
            Im.Account.PROVIDER,
    };

    private ImApp mApp;

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Intent intent = getIntent();
        Uri data = intent.getData();
        if (data == null) {
            Log.e(ImApp.LOG_TAG, "Need account data to sign in");
            finish();
            return;
        }

        ContentResolver cr = getContentResolver();
        Cursor c = cr.query(data,
                ACCOUNT_SELECTION,
                null /* selection */,
                null /* selection args */,
                null /* sort order */);
        final long providerId;
        final long accountId;

        try {
            if (!c.moveToFirst()) {
                Log.w(ImApp.LOG_TAG, "[SignoutActivity] No data for " + data);
                finish();
                return;
            }

            providerId = c.getLong(c.getColumnIndexOrThrow(Im.Account.PROVIDER));
            accountId = c.getLong(c.getColumnIndexOrThrow(Im.Account._ID));
        } finally {
            c.close();
        }

        mApp = ImApp.getApplication(this);
        mApp.callWhenServiceConnected(mHandler, new Runnable() {
            public void run() {
                signOut(providerId, accountId);
            }
        });
    }

    private void signOut(long providerId, long accountId) {
        try {
            IImConnection conn = mApp.getConnection(providerId);
            if (conn != null) {
                conn.logout();
            } else {
                // Normally, we can always get the connection when user chose to
                // sign out. However, if the application crash unexpectedly, the
                // status will never be updated. Clear the status in this case
                // to make it recoverable from the crash.
                ContentValues values = new ContentValues(2);
                values.put(Im.AccountStatus.PRESENCE_STATUS,
                        Im.Presence.OFFLINE);
                values.put(Im.AccountStatus.CONNECTION_STATUS,
                        Im.ConnectionStatus.OFFLINE);
                String where = Im.AccountStatus.ACCOUNT + "=?";
                getContentResolver().update(Im.AccountStatus.CONTENT_URI,
                        values, where,
                        new String[] { Long.toString(accountId) });
            }
        } catch (RemoteException ex) {
            Log.e(ImApp.LOG_TAG, "signout: caught ", ex);
        } finally {
            finish();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

        // always call finish here, because we don't want to be in the backlist ever, and
        // we don't handle onRestart()
        finish();
    }


    static void log(String msg) {
        Log.d(ImApp.LOG_TAG, "[Signout] " + msg);
    }
}