blob: 9918428b5eef3257621d69c16935bc15330de6cf [file] [log] [blame]
The Android Open Source Projectb64d3452009-03-03 19:32:20 -08001/*
2 * Copyright (C) 2009 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.camera;
18
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080019import android.content.Context;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080020import android.graphics.PixelFormat;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080021import android.os.Handler;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080022import android.view.Gravity;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.WindowManager;
26import android.widget.TextView;
27
28/**
29 * A on-screen hint is a view containing a little message for the user and will
30 * be shown on the screen continuously. This class helps you create and show
31 * those.
32 *
33 * <p>
34 * When the view is shown to the user, appears as a floating view over the
35 * application.
36 * <p>
37 * The easiest way to use this class is to call one of the static methods that
38 * constructs everything you need and returns a new OnScreenHint object.
39 */
40public class OnScreenHint {
41 static final String TAG = "OnScreenHint";
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070042 static final boolean LOCAL_LOGV = false;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080043
44 final Context mContext;
45 int mGravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
46 int mX, mY;
47 float mHorizontalMargin;
48 float mVerticalMargin;
49 View mView;
50 View mNextView;
51
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070052 private final WindowManager.LayoutParams mParams =
53 new WindowManager.LayoutParams();
Owen Linbbc560b2009-04-17 11:31:27 +080054 private final WindowManager mWM;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080055 private final Handler mHandler = new Handler();
56
57 /**
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070058 * Construct an empty OnScreenHint object. You must call {@link #setView}
59 * before you can call {@link #show}.
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080060 *
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070061 * @param context The context to use. Usually your
62 * {@link android.app.Application} or
63 * {@link android.app.Activity} object.
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080064 */
65 public OnScreenHint(Context context) {
66 mContext = context;
67 mWM = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070068 mY = context.getResources().getDimensionPixelSize(
69 R.dimen.hint_y_offset);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080070
71 mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
72 mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
73 mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
74 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
75 mParams.format = PixelFormat.TRANSLUCENT;
76 mParams.windowAnimations = R.style.Animation_OnScreenHint;
77 mParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
78 mParams.setTitle("OnScreenHint");
79 }
80
81 /**
82 * Show the view on the screen.
83 */
84 public void show() {
85 if (mNextView == null) {
86 throw new RuntimeException("setView must have been called");
87 }
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080088 mHandler.post(mShow);
89 }
90
91 /**
92 * Close the view if it's showing.
93 */
94 public void cancel() {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080095 mHandler.post(mHide);
96 }
97
98 /**
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080099 * Make a standard hint that just contains a text view.
100 *
Chih-Chung Changb8af1c52009-04-01 02:48:59 -0700101 * @param context The context to use. Usually your
102 * {@link android.app.Application} or
103 * {@link android.app.Activity} object.
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800104 * @param text The text to show. Can be formatted text.
105 *
106 */
107 public static OnScreenHint makeText(Context context, CharSequence text) {
108 OnScreenHint result = new OnScreenHint(context);
109
Chih-Chung Changb8af1c52009-04-01 02:48:59 -0700110 LayoutInflater inflate =
111 (LayoutInflater) context.getSystemService(
112 Context.LAYOUT_INFLATER_SERVICE);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800113 View v = inflate.inflate(R.layout.on_screen_hint, null);
Chih-Chung Changb8af1c52009-04-01 02:48:59 -0700114 TextView tv = (TextView) v.findViewById(R.id.message);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800115 tv.setText(text);
116
117 result.mNextView = v;
118
119 return result;
120 }
121
122 /**
Chih-Chung Changb8af1c52009-04-01 02:48:59 -0700123 * Update the text in a OnScreenHint that was previously created using one
124 * of the makeText() methods.
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800125 * @param s The new text for the OnScreenHint.
126 */
127 public void setText(CharSequence s) {
128 if (mNextView == null) {
Chih-Chung Changb8af1c52009-04-01 02:48:59 -0700129 throw new RuntimeException("This OnScreenHint was not "
130 + "created with OnScreenHint.makeText()");
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800131 }
132 TextView tv = (TextView) mNextView.findViewById(R.id.message);
133 if (tv == null) {
Chih-Chung Changb8af1c52009-04-01 02:48:59 -0700134 throw new RuntimeException("This OnScreenHint was not "
135 + "created with OnScreenHint.makeText()");
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800136 }
137 tv.setText(s);
138 }
139
140 private synchronized void handleShow() {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800141 if (mView != mNextView) {
142 // remove the old view if necessary
143 handleHide();
144 mView = mNextView;
145 final int gravity = mGravity;
146 mParams.gravity = gravity;
Chih-Chung Changb8af1c52009-04-01 02:48:59 -0700147 if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK)
148 == Gravity.FILL_HORIZONTAL) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800149 mParams.horizontalWeight = 1.0f;
150 }
Chih-Chung Changb8af1c52009-04-01 02:48:59 -0700151 if ((gravity & Gravity.VERTICAL_GRAVITY_MASK)
152 == Gravity.FILL_VERTICAL) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800153 mParams.verticalWeight = 1.0f;
154 }
155 mParams.x = mX;
156 mParams.y = mY;
157 mParams.verticalMargin = mVerticalMargin;
158 mParams.horizontalMargin = mHorizontalMargin;
159 if (mView.getParent() != null) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800160 mWM.removeView(mView);
161 }
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800162 mWM.addView(mView, mParams);
163 }
164 }
165
166 private synchronized void handleHide() {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800167 if (mView != null) {
168 // note: checking parent() just to make sure the view has
169 // been added... i have seen cases where we get here when
170 // the view isn't yet added, so let's try not to crash.
171 if (mView.getParent() != null) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800172 mWM.removeView(mView);
173 }
174 mView = null;
175 }
176 }
177
Owen Linbbc560b2009-04-17 11:31:27 +0800178 private final Runnable mShow = new Runnable() {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800179 public void run() {
180 handleShow();
181 }
182 };
183
Owen Linbbc560b2009-04-17 11:31:27 +0800184 private final Runnable mHide = new Runnable() {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800185 public void run() {
186 handleHide();
187 }
188 };
189}
190