1 /*
2  * Copyright (C) 2012 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 
17 package com.android.launcher2;
18 
19 import java.io.IOException;
20 import java.util.ArrayList;
21 
22 import com.android.launcher.R;
23 
24 import android.app.WallpaperManager;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.res.Resources;
29 
30 /**
31  * Takes care of setting initial wallpaper for a user, by selecting the
32  * first wallpaper that is not in use by another user.
33  */
34 public class UserInitializeReceiver extends BroadcastReceiver {
35     @Override
onReceive(Context context, Intent intent)36     public void onReceive(Context context, Intent intent) {
37         final Resources resources = context.getResources();
38         // Context.getPackageName() may return the "original" package name,
39         // com.android.launcher2; Resources needs the real package name,
40         // com.android.launcher. So we ask Resources for what it thinks the
41         // package name should be.
42         final String packageName = resources.getResourcePackageName(R.array.wallpapers);
43         ArrayList<Integer> list = new ArrayList<Integer>();
44         addWallpapers(resources, packageName, R.array.wallpapers, list);
45         addWallpapers(resources, packageName, R.array.extra_wallpapers, list);
46         WallpaperManager wpm = (WallpaperManager) context.getSystemService(
47                 Context.WALLPAPER_SERVICE);
48         for (int i=1; i<list.size(); i++) {
49             int resid = list.get(i);
50             if (!wpm.hasResourceWallpaper(resid)) {
51                 try {
52                     wpm.setResource(resid);
53                 } catch (IOException e) {
54                 }
55                 return;
56             }
57         }
58     }
59 
addWallpapers(Resources resources, String packageName, int resid, ArrayList<Integer> outList)60     private void addWallpapers(Resources resources, String packageName, int resid,
61             ArrayList<Integer> outList) {
62         final String[] extras = resources.getStringArray(resid);
63         for (String extra : extras) {
64             int res = resources.getIdentifier(extra, "drawable", packageName);
65             if (res != 0) {
66                 outList.add(res);
67             }
68         }
69     }
70 }
71