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