Home | History | Annotate | Download | only in livepicker
      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 
     17 package com.android.wallpaper.livepicker;
     18 
     19 import java.io.IOException;
     20 import java.util.List;
     21 
     22 import org.xmlpull.v1.XmlPullParserException;
     23 
     24 import android.app.Activity;
     25 import android.app.WallpaperInfo;
     26 import android.app.WallpaperManager;
     27 import android.os.Bundle;
     28 import android.os.Parcelable;
     29 import android.content.ComponentName;
     30 import android.content.Intent;
     31 import android.content.pm.PackageManager;
     32 import android.content.pm.PackageManager.NameNotFoundException;
     33 import android.content.pm.ResolveInfo;
     34 import android.content.pm.ServiceInfo;
     35 import android.service.wallpaper.WallpaperService;
     36 import android.util.Log;
     37 
     38 public 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                     finish();
     82                     return;
     83                 }
     84             }
     85         }
     86 
     87         Log.w(TAG, "Not a live wallpaper: " + comp);
     88         finish();
     89     }
     90 }
     91