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