2 * Copyright 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.example.wallpapertest;
19 import android.app.Activity;
20 import android.app.WallpaperManager;
21 import android.content.Context;
22 import android.graphics.Point;
23 import android.graphics.Rect;
24 import android.os.Bundle;
25 import android.os.IBinder;
26 import android.text.Editable;
27 import android.text.TextUtils;
28 import android.text.TextWatcher;
29 import android.util.Log;
30 import android.view.WindowManager;
31 import android.widget.TextView;
33 public class MainActivity extends Activity {
34 private static final String TAG = "MainActivity";
36 WallpaperManager mWallpaperManager;
37 WindowManager mWindowManager;
39 TextView mDimenWidthView;
40 TextView mDimenHeightView;
42 TextView mWallOffXView;
43 TextView mWallOffYView;
45 TextView mPaddingLeftView;
46 TextView mPaddingRightView;
47 TextView mPaddingTopView;
48 TextView mPaddingBottomView;
50 TextView mDispOffXView;
51 TextView mDispOffYView;
54 public void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56 setContentView(R.layout.activity_main);
58 mWallpaperManager = (WallpaperManager)getSystemService(Context.WALLPAPER_SERVICE);
59 mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
61 mDimenWidthView = findViewById(R.id.dimen_width);
62 mDimenWidthView.addTextChangedListener(mTextWatcher);
63 mDimenHeightView = findViewById(R.id.dimen_height);
64 mDimenHeightView.addTextChangedListener(mTextWatcher);
66 mWallOffXView = findViewById(R.id.walloff_x);
67 mWallOffXView.addTextChangedListener(mTextWatcher);
68 mWallOffYView = findViewById(R.id.walloff_y);
69 mWallOffYView.addTextChangedListener(mTextWatcher);
71 mPaddingLeftView = findViewById(R.id.padding_left);
72 mPaddingLeftView.addTextChangedListener(mTextWatcher);
73 mPaddingRightView = findViewById(R.id.padding_right);
74 mPaddingRightView.addTextChangedListener(mTextWatcher);
75 mPaddingTopView = findViewById(R.id.padding_top);
76 mPaddingTopView.addTextChangedListener(mTextWatcher);
77 mPaddingBottomView = findViewById(R.id.padding_bottom);
78 mPaddingBottomView.addTextChangedListener(mTextWatcher);
80 mDispOffXView = findViewById(R.id.dispoff_x);
81 mDispOffXView.addTextChangedListener(mTextWatcher);
82 mDispOffYView = findViewById(R.id.dispoff_y);
83 mDispOffYView.addTextChangedListener(mTextWatcher);
91 private int loadPropIntText(TextView view, int baseVal) {
92 String str = view.getText().toString();
93 if (str != null && !TextUtils.isEmpty(str)) {
95 float fval = Float.parseFloat(str);
96 return (int)(fval*baseVal);
97 } catch (NumberFormatException e) {
98 Log.i(TAG, "Bad number: " + str, e);
104 private float loadFloatText(TextView view) {
105 String str = view.getText().toString();
106 if (str != null && !TextUtils.isEmpty(str)) {
108 return Float.parseFloat(str);
109 } catch (NumberFormatException e) {
110 Log.i(TAG, "Bad number: " + str, e);
116 private int loadIntText(TextView view) {
117 String str = view.getText().toString();
118 if (str != null && !TextUtils.isEmpty(str)) {
120 return Integer.parseInt(str);
121 } catch (NumberFormatException e) {
122 Log.i(TAG, "Bad number: " + str, e);
128 public void updateDimens() {
129 Point minDims = new Point();
130 Point maxDims = new Point();
131 mWindowManager.getDefaultDisplay().getCurrentSizeRange(minDims, maxDims);
132 mWallpaperManager.suggestDesiredDimensions(
133 loadPropIntText(mDimenWidthView, maxDims.x),
134 loadPropIntText(mDimenHeightView, maxDims.y));
137 public void updateWallOff() {
138 IBinder token = getWindow().getDecorView().getWindowToken();
140 mWallpaperManager.setWallpaperOffsets(token, loadFloatText(mWallOffXView),
141 loadFloatText(mWallOffYView));
145 public void updatePadding() {
146 Rect padding = new Rect();
147 padding.left = loadIntText(mPaddingLeftView);
148 padding.top = loadIntText(mPaddingTopView);
149 padding.right = loadIntText(mPaddingRightView);
150 padding.bottom = loadIntText(mPaddingBottomView);
151 mWallpaperManager.setDisplayPadding(padding);
154 public void updateDispOff() {
155 IBinder token = getWindow().getDecorView().getWindowToken();
157 mWallpaperManager.setDisplayOffset(token, loadIntText(mDispOffXView),
158 loadIntText(mDispOffYView));
162 final TextWatcher mTextWatcher = new TextWatcher() {
163 @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
166 @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
173 @Override public void afterTextChanged(Editable s) {