DateView.java revision 15a4d2ffd04dc6c70f2cd17dae12ac6bc14c69ab
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.status;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.util.AttributeSet;
24import android.util.Slog;
25import android.widget.TextView;
26import android.view.MotionEvent;
27
28import java.text.DateFormat;
29import java.util.Date;
30
31public final class DateView extends TextView {
32    private static final String TAG = "DateView";
33
34    private boolean mUpdating = false;
35
36    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
37        @Override
38        public void onReceive(Context context, Intent intent) {
39            String action = intent.getAction();
40            if (action.equals(Intent.ACTION_TIME_TICK)
41                    || action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
42                updateClock();
43            }
44        }
45    };
46
47    public DateView(Context context, AttributeSet attrs) {
48        super(context, attrs);
49    }
50
51    @Override
52    protected void onAttachedToWindow() {
53        super.onAttachedToWindow();
54    }
55
56    @Override
57    protected void onDetachedFromWindow() {
58        super.onDetachedFromWindow();
59        setUpdates(false);
60    }
61
62    @Override
63    protected int getSuggestedMinimumWidth() {
64        // makes the large background bitmap not force us to full width
65        return 0;
66    }
67
68    private final void updateClock() {
69        Date now = new Date();
70        setText(DateFormat.getDateInstance(DateFormat.LONG).format(now));
71    }
72
73    void setUpdates(boolean update) {
74        if (update != mUpdating) {
75            mUpdating = update;
76            if (update) {
77                // Register for Intent broadcasts for the clock and battery
78                IntentFilter filter = new IntentFilter();
79                filter.addAction(Intent.ACTION_TIME_TICK);
80                filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
81                mContext.registerReceiver(mIntentReceiver, filter, null, null);
82                updateClock();
83            } else {
84                mContext.unregisterReceiver(mIntentReceiver);
85            }
86        }
87    }
88}
89
90