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 java.io.IOException;
20import java.util.List;
21
22import org.xmlpull.v1.XmlPullParserException;
23
24import android.app.Activity;
25import android.app.WallpaperInfo;
26import android.app.WallpaperManager;
27import android.os.Bundle;
28import android.os.Parcelable;
29import android.content.ComponentName;
30import android.content.Intent;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
33import android.content.pm.ResolveInfo;
34import android.content.pm.ServiceInfo;
35import android.service.wallpaper.WallpaperService;
36import android.util.Log;
37
38public class LiveWallpaperChange extends Activity {
39    private static final String TAG = "CHANGE_LIVE_WALLPAPER";
40
41    @Override
42    protected void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44
45        Parcelable obj = getIntent().getParcelableExtra(
46                WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT);
47        if (obj == null || !(obj instanceof ComponentName)) {
48            Log.w(TAG, "No LIVE_WALLPAPER_COMPONENT extra supplied");
49            finish();
50            return;
51        }
52
53        ComponentName comp = (ComponentName)obj;
54
55        // Get the information about this component.  Implemented this way
56        // to not allow us to direct the caller to a service that is not a
57        // live wallpaper.
58        Intent queryIntent = new Intent(WallpaperService.SERVICE_INTERFACE);
59        queryIntent.setPackage(comp.getPackageName());
60        List<ResolveInfo> list = getPackageManager().queryIntentServices(
61                queryIntent, PackageManager.GET_META_DATA);
62        if (list != null) {
63            for (int i=0; i<list.size(); i++) {
64                ResolveInfo ri = list.get(i);
65                if (ri.serviceInfo.name.equals(comp.getClassName())) {
66                    WallpaperInfo info = null;
67                    try {
68                        info = new WallpaperInfo(this, ri);
69                    } catch (XmlPullParserException e) {
70                        Log.w(TAG, "Bad wallpaper " + ri.serviceInfo, e);
71                        finish();
72                        return;
73                    } catch (IOException e) {
74                        Log.w(TAG, "Bad wallpaper " + ri.serviceInfo, e);
75                        finish();
76                        return;
77                    }
78                    Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
79                    intent.setClassName(info.getPackageName(), info.getServiceName());
80                    LiveWallpaperPreview.showPreview(this, 0, intent, info);
81                    return;
82                }
83            }
84        }
85
86        Log.w(TAG, "Not a live wallpaper: " + comp);
87        finish();
88    }
89
90    @Override
91    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
92        super.onActivityResult(requestCode, resultCode, data);
93
94        // forward result code
95        setResult(resultCode);
96        finish();
97    }
98}
99