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
18package com.android.mms.ui;
19
20import static com.android.mms.util.RateController.RATE_LIMIT_CONFIRMED_ACTION;
21import android.app.Activity;
22import android.content.Intent;
23import android.os.Bundle;
24import android.os.Handler;
25import android.util.Log;
26import android.view.KeyEvent;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.view.Window;
30import android.widget.Button;
31
32import com.android.mms.R;
33import com.android.mms.util.RateController;
34
35public class ConfirmRateLimitActivity extends Activity {
36    private static final String TAG = "ConfirmRateLimitActivity";
37    private static final boolean DEBUG = false;
38    private static final boolean LOCAL_LOGV = false;
39
40    private long mCreateTime;
41    private Handler mHandler;
42    private Runnable mRunnable;
43
44    @Override
45    protected void onCreate(Bundle savedInstanceState) {
46        super.onCreate(savedInstanceState);
47
48        requestWindowFeature(Window.FEATURE_NO_TITLE);
49        setContentView(R.layout.confirm_rate_limit_activity);
50
51        Button button = (Button) findViewById(R.id.btn_yes);
52        button.setOnClickListener(new OnClickListener() {
53            public void onClick(View v) {
54                doAnswer(true);
55            }
56        });
57
58        button = (Button) findViewById(R.id.btn_no);
59        button.setOnClickListener(new OnClickListener() {
60            public void onClick(View v) {
61                doAnswer(false);
62            }
63        });
64
65        mHandler = new Handler();
66        mRunnable = new Runnable() {
67            public void run() {
68                if (LOCAL_LOGV) {
69                    Log.v(TAG, "Runnable executed.");
70                }
71                doAnswer(false);
72            }
73        };
74
75        mCreateTime = System.currentTimeMillis();
76    }
77
78    @Override
79    protected void onResume() {
80        super.onResume();
81
82        long delay = mCreateTime - System.currentTimeMillis()
83                        + (RateController.ANSWER_TIMEOUT - 500);
84
85        if (delay <= 0) {
86            doAnswer(false);
87        } else if (mHandler != null) {
88            // Close this activity after certain seconds if no user action.
89            mHandler.postDelayed(mRunnable, delay);
90        }
91    }
92
93    @Override
94    protected void onPause() {
95        super.onPause();
96
97        if (mHandler != null) {
98            mHandler.removeCallbacks(mRunnable);
99        }
100    }
101
102    @Override
103    public boolean onKeyDown(int keyCode, KeyEvent event) {
104        if ((keyCode == KeyEvent.KEYCODE_BACK)
105                && (event.getRepeatCount() == 0)) {
106            doAnswer(false);
107        }
108        return super.onKeyDown(keyCode, event);
109    }
110
111    private void doAnswer(boolean answer) {
112        Intent intent = new Intent(RATE_LIMIT_CONFIRMED_ACTION);
113        intent.putExtra("answer", answer);
114        sendBroadcast(intent);
115        finish();
116    }
117}
118