1/*
2 * Copyright (C) 2009 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.wallpaper.livepicker;
18
19import android.app.WallpaperInfo;
20import android.app.WallpaperManager;
21import android.content.ComponentName;
22import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25import android.os.Parcelable;
26import android.service.wallpaper.WallpaperService;
27import android.util.Log;
28
29import org.xmlpull.v1.XmlPullParserException;
30
31import java.io.IOException;
32import java.util.List;
33
34public class LiveWallpaperChange extends LiveWallpaperPreview {
35    private static final String TAG = "CHANGE_LIVE_WALLPAPER";
36
37    @Override
38    protected void init() {
39        Parcelable obj = getIntent().getParcelableExtra(
40                WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT);
41        if (obj == null || !(obj instanceof ComponentName)) {
42            Log.w(TAG, "No LIVE_WALLPAPER_COMPONENT extra supplied");
43            finish();
44            return;
45        }
46
47        ComponentName comp = (ComponentName)obj;
48
49        // Get the information about this component.  Implemented this way
50        // to not allow us to direct the caller to a service that is not a
51        // live wallpaper.
52        Intent queryIntent = new Intent(WallpaperService.SERVICE_INTERFACE);
53        queryIntent.setPackage(comp.getPackageName());
54        List<ResolveInfo> list = getPackageManager().queryIntentServices(
55                queryIntent, PackageManager.GET_META_DATA);
56        if (list != null) {
57            for (int i=0; i<list.size(); i++) {
58                ResolveInfo ri = list.get(i);
59                if (ri.serviceInfo.name.equals(comp.getClassName())) {
60                    WallpaperInfo info;
61                    try {
62                        info = new WallpaperInfo(this, ri);
63                    } catch (XmlPullParserException|IOException e) {
64                        Log.w(TAG, "Bad wallpaper " + ri.serviceInfo, e);
65                        finish();
66                        return;
67                    }
68
69                    initUI(info);
70                    return;
71                }
72            }
73        }
74
75        Log.w(TAG, "Not a live wallpaper: " + comp);
76        finish();
77    }
78}
79