summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/ui/ClassZeroActivity.java
blob: ccb15a0898de5aa5035ca60b79bbe74362044d30 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (C) 2015 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.messaging.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.provider.Telephony.Sms;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;

import com.android.messaging.R;
import com.android.messaging.datamodel.action.ReceiveSmsMessageAction;
import com.android.messaging.datamodel.BugleNotifications;
import com.android.messaging.util.Assert;

import java.util.ArrayList;

/**
 * Display a class-zero SMS message to the user. Wait for the user to dismiss
 * it.
 */
public class ClassZeroActivity extends Activity {
    private static final boolean VERBOSE = false;
    private static final String TAG = "display_00";
    private static final int ON_AUTO_SAVE = 1;

    /** Default timer to dismiss the dialog. */
    private static final long DEFAULT_TIMER = 5 * 60 * 1000;

    /** To remember the exact time when the timer should fire. */
    private static final String TIMER_FIRE = "timer_fire";

    private ContentValues mMessageValues = null;

    /** Is the message read. */
    private boolean mRead = false;

    /** The timer to dismiss the dialog automatically. */
    private long mTimerSet = 0;
    private AlertDialog mDialog = null;

    private ArrayList<ContentValues> mMessageQueue = null;

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(final Message msg) {
            // Do not handle an invalid message.
            if (msg.what == ON_AUTO_SAVE) {
                mRead = false;
                mDialog.dismiss();
                saveMessage();
                processNextMessage();
            }
        }
    };

    private boolean queueMsgFromIntent(final Intent msgIntent) {
        final ContentValues messageValues =
                msgIntent.getParcelableExtra(UIIntents.UI_INTENT_EXTRA_MESSAGE_VALUES);
        // that takes the format argument is a hidden API right now.
        final String message = messageValues.getAsString(Sms.BODY);
        if (TextUtils.isEmpty(message)) {
            if (mMessageQueue.size() == 0) {
                finish();
            }
            return false;
        }
        mMessageQueue.add(messageValues);
        // Show a notification to let the user know a new message has arrived
        BugleNotifications.playClassZeroNotification();
        return true;
    }

    private void processNextMessage() {
        if (mMessageQueue.size() > 0) {
            mMessageQueue.remove(0);
        }
        if (mMessageQueue.size() == 0) {
            finish();
        } else {
            displayZeroMessage(mMessageQueue.get(0));
        }
    }

    private void saveMessage() {
        mMessageValues.put(Sms.Inbox.READ, mRead ? Integer.valueOf(1) : Integer.valueOf(0));
        final ReceiveSmsMessageAction action = new ReceiveSmsMessageAction(mMessageValues);
        action.start();
    }

    @Override
    protected void onCreate(final Bundle icicle) {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        if (mMessageQueue == null) {
            mMessageQueue = new ArrayList<ContentValues>();
        }
        if (!queueMsgFromIntent(getIntent())) {
            return;
        }
        Assert.isTrue(mMessageQueue.size() == 1);
        if (mMessageQueue.size() == 1) {
            displayZeroMessage(mMessageQueue.get(0));
        }

        if (icicle != null) {
            mTimerSet = icicle.getLong(TIMER_FIRE, mTimerSet);
        }
    }

    private void displayZeroMessage(final ContentValues messageValues) {
        /* This'll be used by the save action */
        mMessageValues = messageValues;
        final String message = messageValues.getAsString(Sms.BODY);;

        mDialog = new AlertDialog.Builder(this).setMessage(message)
            .setPositiveButton(R.string.save, mSaveListener)
            .setNegativeButton(android.R.string.cancel, mCancelListener)
            .setTitle(R.string.class_0_message_activity)
            .setCancelable(false).show();
        final long now = SystemClock.uptimeMillis();
        mTimerSet = now + DEFAULT_TIMER;
    }

    @Override
    protected void onNewIntent(final Intent msgIntent) {
        // Running with another visible message, queue this one
        queueMsgFromIntent(msgIntent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        final long now = SystemClock.uptimeMillis();
        if (mTimerSet <= now) {
            // Save the message if the timer already expired.
            mHandler.sendEmptyMessage(ON_AUTO_SAVE);
        } else {
            mHandler.sendEmptyMessageAtTime(ON_AUTO_SAVE, mTimerSet);
            if (VERBOSE) {
                Log.d(TAG, "onRestart time = " + Long.toString(mTimerSet) + " "
                        + this.toString());
            }
        }
    }

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong(TIMER_FIRE, mTimerSet);
        if (VERBOSE) {
            Log.d(TAG, "onSaveInstanceState time = " + Long.toString(mTimerSet)
                    + " " + this.toString());
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mHandler.removeMessages(ON_AUTO_SAVE);
        if (VERBOSE) {
            Log.d(TAG, "onStop time = " + Long.toString(mTimerSet)
                    + " " + this.toString());
        }
    }

    private final OnClickListener mCancelListener = new OnClickListener() {
        @Override
        public void onClick(final DialogInterface dialog, final int whichButton) {
            dialog.dismiss();
            processNextMessage();
        }
    };

    private final OnClickListener mSaveListener = new OnClickListener() {
        @Override
        public void onClick(final DialogInterface dialog, final int whichButton) {
            mRead = true;
            saveMessage();
            dialog.dismiss();
            processNextMessage();
        }
    };
}