blob: 5532894b0f0d259b1adcd26acbfe1b1d81647d0e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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;
18
19import static android.os.FileObserver.*;
20import static android.os.ParcelFileDescriptor.*;
21import android.app.IWallpaperService;
22import android.app.IWallpaperServiceCallback;
23import android.content.Context;
24import android.content.Intent;
25import android.content.SharedPreferences;
26import android.content.pm.PackageManager;
27import android.os.Binder;
28import android.os.RemoteException;
29import android.os.FileObserver;
30import android.os.ParcelFileDescriptor;
31import android.os.RemoteCallbackList;
32import android.util.Config;
33import android.util.Log;
34
35import java.io.File;
36import java.io.FileNotFoundException;
37
38class WallpaperService extends IWallpaperService.Stub {
39 private static final String TAG = WallpaperService.class.getSimpleName();
40
41 private static final File WALLPAPER_DIR = new File(
42 "/data/data/com.android.settings/files");
43 private static final String WALLPAPER = "wallpaper";
44 private static final File WALLPAPER_FILE = new File(WALLPAPER_DIR, WALLPAPER);
45
46 private static final String PREFERENCES = "wallpaper-hints";
47
48 private static final String HINT_WIDTH = "hintWidth";
49 private static final String HINT_HEIGHT = "hintHeight";
50
51 /**
52 * List of callbacks registered they should each be notified
53 * when the wallpaper is changed.
54 */
55 private final RemoteCallbackList<IWallpaperServiceCallback> mCallbacks
56 = new RemoteCallbackList<IWallpaperServiceCallback>();
57
58 /**
59 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
60 * that the wallpaper has changed. The CREATE is triggered when there is no
61 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
62 * everytime the wallpaper is changed.
63 */
64 private final FileObserver mWallpaperObserver = new FileObserver(
65 WALLPAPER_DIR.getAbsolutePath(), CREATE | CLOSE_WRITE) {
66 @Override
67 public void onEvent(int event, String path) {
68 if (path == null) {
69 return;
70 }
71
72 File changedFile = new File(WALLPAPER_DIR, path);
73 if (WALLPAPER_FILE.equals(changedFile)) {
74 notifyCallbacks();
75 }
76 }
77 };
78
79 private final Context mContext;
80
81 private int mWidth = -1;
82 private int mHeight = -1;
83
84 public WallpaperService(Context context) {
85 if (Config.LOGD) Log.d(TAG, "WallpaperService startup");
86 mContext = context;
87 createFilesDir();
88 mWallpaperObserver.startWatching();
89
90 SharedPreferences preferences = mContext.getSharedPreferences(PREFERENCES,
91 Context.MODE_PRIVATE);
92 mWidth = preferences.getInt(HINT_WIDTH, -1);
93 mHeight = preferences.getInt(HINT_HEIGHT, -1);
94 }
95
96 @Override
97 protected void finalize() throws Throwable {
98 super.finalize();
99 mWallpaperObserver.stopWatching();
100 }
101
102 public void clearWallpaper() {
103 File f = WALLPAPER_FILE;
104 if (f.exists()) {
105 f.delete();
106 }
107 }
108
109 public void setDimensionHints(int width, int height) throws RemoteException {
110 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
111
112 if (width <= 0 || height <= 0) {
113 throw new IllegalArgumentException("width and height must be > 0");
114 }
115
116 if (width != mWidth || height != mHeight) {
117 mWidth = width;
118 mHeight = height;
119
120 SharedPreferences preferences = mContext.getSharedPreferences(PREFERENCES,
121 Context.MODE_PRIVATE);
122
123 final SharedPreferences.Editor editor = preferences.edit();
124 editor.putInt(HINT_WIDTH, width);
125 editor.putInt(HINT_HEIGHT, height);
126 editor.commit();
127 }
128 }
129
130 public int getWidthHint() throws RemoteException {
131 return mWidth;
132 }
133
134 public int getHeightHint() throws RemoteException {
135 return mHeight;
136 }
137
138 public ParcelFileDescriptor getWallpaper(IWallpaperServiceCallback cb) {
139 try {
140 mCallbacks.register(cb);
141 File f = WALLPAPER_FILE;
142 if (!f.exists()) {
143 return null;
144 }
145 return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
146 } catch (FileNotFoundException e) {
147
148 /* Shouldn't happen as we check to see if the file exists */
149 if (Config.LOGD) Log.d(TAG, "Error getting wallpaper", e);
150 }
151 return null;
152 }
153
154 public ParcelFileDescriptor setWallpaper() {
155 checkPermission(android.Manifest.permission.SET_WALLPAPER);
156 try {
157 return ParcelFileDescriptor.open(WALLPAPER_FILE, MODE_CREATE|MODE_READ_WRITE);
158 } catch (FileNotFoundException e) {
159 if (Config.LOGD) Log.d(TAG, "Error setting wallpaper", e);
160 }
161 return null;
162 }
163
164 private void createFilesDir() {
165 if (!WALLPAPER_DIR.exists()) {
166 WALLPAPER_DIR.mkdirs();
167 }
168 }
169
170 private void notifyCallbacks() {
171 final int n = mCallbacks.beginBroadcast();
172 for (int i = 0; i < n; i++) {
173 try {
174 mCallbacks.getBroadcastItem(i).onWallpaperChanged();
175 } catch (RemoteException e) {
176
177 // The RemoteCallbackList will take care of removing
178 // the dead object for us.
179 }
180 }
181 mCallbacks.finishBroadcast();
182 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
183 mContext.sendBroadcast(intent);
184 }
185
186 private void checkPermission(String permission) {
187 if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission(permission)) {
188 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
189 + ", must have permission " + permission);
190 }
191 }
192}