1804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn/*
2804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * Copyright (C) 2012 The Android Open Source Project
3804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn *
4804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * you may not use this file except in compliance with the License.
6804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * You may obtain a copy of the License at
7804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn *
8804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn *
10804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
11804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * See the License for the specific language governing permissions and
14804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * limitations under the License.
15804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn */
16804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn
17804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackbornpackage com.android.launcher2;
18804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn
19804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackbornimport java.io.IOException;
20804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackbornimport java.util.ArrayList;
21804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn
22804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackbornimport com.android.launcher.R;
23804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn
24804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackbornimport android.app.WallpaperManager;
25804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackbornimport android.content.BroadcastReceiver;
26804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackbornimport android.content.Context;
27804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackbornimport android.content.Intent;
28804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackbornimport android.content.res.Resources;
29804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn
30804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn/**
31804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * Takes care of setting initial wallpaper for a user, by selecting the
32804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn * first wallpaper that is not in use by another user.
33804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn */
34804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackbornpublic class UserInitializeReceiver extends BroadcastReceiver {
35804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn    @Override
36804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn    public void onReceive(Context context, Intent intent) {
37804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        final Resources resources = context.getResources();
38804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        // Context.getPackageName() may return the "original" package name,
39804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        // com.android.launcher2; Resources needs the real package name,
40804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        // com.android.launcher. So we ask Resources for what it thinks the
41804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        // package name should be.
42804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        final String packageName = resources.getResourcePackageName(R.array.wallpapers);
43804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        ArrayList<Integer> list = new ArrayList<Integer>();
44804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        addWallpapers(resources, packageName, R.array.wallpapers, list);
45804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        addWallpapers(resources, packageName, R.array.extra_wallpapers, list);
46804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        WallpaperManager wpm = (WallpaperManager) context.getSystemService(
47804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn                Context.WALLPAPER_SERVICE);
48804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        for (int i=1; i<list.size(); i++) {
49804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn            int resid = list.get(i);
50804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn            if (!wpm.hasResourceWallpaper(resid)) {
51804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn                try {
52804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn                    wpm.setResource(resid);
53804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn                } catch (IOException e) {
54804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn                }
55804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn                return;
56804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn            }
57804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        }
58804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn    }
59804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn
60804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn    private void addWallpapers(Resources resources, String packageName, int resid,
61804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn            ArrayList<Integer> outList) {
62804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        final String[] extras = resources.getStringArray(resid);
63804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        for (String extra : extras) {
64804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn            int res = resources.getIdentifier(extra, "drawable", packageName);
65804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn            if (res != 0) {
66804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn                outList.add(res);
67804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn            }
68804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn        }
69804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn    }
70804503c75a12e1578f2f71ff3cb32ae912fc9451Dianne Hackborn}
71