1/*
2 * Copyright 2014 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.example.wallpapertest;
18
19import android.app.Activity;
20import android.app.WallpaperManager;
21import android.content.Context;
22import android.graphics.Point;
23import android.graphics.Rect;
24import android.os.Bundle;
25import android.os.IBinder;
26import android.text.Editable;
27import android.text.TextUtils;
28import android.text.TextWatcher;
29import android.util.Log;
30import android.view.WindowManager;
31import android.widget.TextView;
32
33public class MainActivity extends Activity {
34    private static final String TAG = "MainActivity";
35
36    WallpaperManager mWallpaperManager;
37    WindowManager mWindowManager;
38
39    TextView mDimenWidthView;
40    TextView mDimenHeightView;
41
42    TextView mWallOffXView;
43    TextView mWallOffYView;
44
45    TextView mPaddingLeftView;
46    TextView mPaddingRightView;
47    TextView mPaddingTopView;
48    TextView mPaddingBottomView;
49
50    TextView mDispOffXView;
51    TextView mDispOffYView;
52
53    @Override
54    public void onCreate(Bundle savedInstanceState) {
55        super.onCreate(savedInstanceState);
56        setContentView(R.layout.activity_main);
57
58        mWallpaperManager = (WallpaperManager)getSystemService(Context.WALLPAPER_SERVICE);
59        mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
60
61        mDimenWidthView = (TextView) findViewById(R.id.dimen_width);
62        mDimenWidthView.addTextChangedListener(mTextWatcher);
63        mDimenHeightView = (TextView) findViewById(R.id.dimen_height);
64        mDimenHeightView.addTextChangedListener(mTextWatcher);
65
66        mWallOffXView = (TextView) findViewById(R.id.walloff_x);
67        mWallOffXView.addTextChangedListener(mTextWatcher);
68        mWallOffYView = (TextView) findViewById(R.id.walloff_y);
69        mWallOffYView.addTextChangedListener(mTextWatcher);
70
71        mPaddingLeftView = (TextView) findViewById(R.id.padding_left);
72        mPaddingLeftView.addTextChangedListener(mTextWatcher);
73        mPaddingRightView = (TextView) findViewById(R.id.padding_right);
74        mPaddingRightView.addTextChangedListener(mTextWatcher);
75        mPaddingTopView = (TextView) findViewById(R.id.padding_top);
76        mPaddingTopView.addTextChangedListener(mTextWatcher);
77        mPaddingBottomView = (TextView) findViewById(R.id.padding_bottom);
78        mPaddingBottomView.addTextChangedListener(mTextWatcher);
79
80        mDispOffXView = (TextView) findViewById(R.id.dispoff_x);
81        mDispOffXView.addTextChangedListener(mTextWatcher);
82        mDispOffYView = (TextView) findViewById(R.id.dispoff_y);
83        mDispOffYView.addTextChangedListener(mTextWatcher);
84
85        updateDimens();
86        updateWallOff();
87        updatePadding();
88        updateDispOff();
89    }
90
91    private int loadPropIntText(TextView view, int baseVal) {
92        String str = view.getText().toString();
93        if (str != null && !TextUtils.isEmpty(str)) {
94            try {
95                float fval = Float.parseFloat(str);
96                return (int)(fval*baseVal);
97            } catch (NumberFormatException e) {
98                Log.i(TAG, "Bad number: " + str, e);
99            }
100        }
101        return baseVal;
102    }
103
104    private float loadFloatText(TextView view) {
105        String str = view.getText().toString();
106        if (str != null && !TextUtils.isEmpty(str)) {
107            try {
108                return Float.parseFloat(str);
109            } catch (NumberFormatException e) {
110                Log.i(TAG, "Bad number: " + str, e);
111            }
112        }
113        return 0;
114    }
115
116    private int loadIntText(TextView view) {
117        String str = view.getText().toString();
118        if (str != null && !TextUtils.isEmpty(str)) {
119            try {
120                return Integer.parseInt(str);
121            } catch (NumberFormatException e) {
122                Log.i(TAG, "Bad number: " + str, e);
123            }
124        }
125        return 0;
126    }
127
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));
135    }
136
137    public void updateWallOff() {
138        IBinder token = getWindow().getDecorView().getWindowToken();
139        if (token != null) {
140            mWallpaperManager.setWallpaperOffsets(token, loadFloatText(mWallOffXView),
141                    loadFloatText(mWallOffYView));
142        }
143    }
144
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);
152    }
153
154    public void updateDispOff() {
155        IBinder token = getWindow().getDecorView().getWindowToken();
156        if (token != null) {
157            mWallpaperManager.setDisplayOffset(token, loadIntText(mDispOffXView),
158                    loadIntText(mDispOffYView));
159        }
160    }
161
162    final TextWatcher mTextWatcher = new TextWatcher() {
163        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
164        }
165
166        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
167            updateDimens();
168            updateWallOff();
169            updatePadding();
170            updateDispOff();
171        }
172
173        @Override public void afterTextChanged(Editable s) {
174        }
175    };
176}
177