From 2f26cced2d4257c4fb106939447d901ae971b4d1 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Sun, 8 Nov 2009 13:51:50 -0800 Subject: New, safer, live wallpaper picker. --- .../livepicker/LiveWallpaperListActivity.java | 214 +++++++++++ .../livepicker/LiveWallpaperPickActivity.java | 411 --------------------- .../wallpaper/livepicker/LiveWallpaperPreview.java | 224 +++++++++++ 3 files changed, 438 insertions(+), 411 deletions(-) create mode 100644 src/com/android/wallpaper/livepicker/LiveWallpaperListActivity.java delete mode 100644 src/com/android/wallpaper/livepicker/LiveWallpaperPickActivity.java create mode 100644 src/com/android/wallpaper/livepicker/LiveWallpaperPreview.java (limited to 'src/com/android/wallpaper/livepicker') diff --git a/src/com/android/wallpaper/livepicker/LiveWallpaperListActivity.java b/src/com/android/wallpaper/livepicker/LiveWallpaperListActivity.java new file mode 100644 index 0000000..44493ee --- /dev/null +++ b/src/com/android/wallpaper/livepicker/LiveWallpaperListActivity.java @@ -0,0 +1,214 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.wallpaper.livepicker; + +import android.app.ListActivity; +import android.app.WallpaperInfo; +import android.os.Bundle; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.content.pm.ComponentInfo; +import android.content.Intent; +import android.content.res.Resources; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.Paint; +import android.graphics.Canvas; +import android.graphics.Bitmap; +import android.util.Log; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.view.LayoutInflater; +import android.service.wallpaper.WallpaperService; +import android.widget.BaseAdapter; +import android.widget.TextView; +import android.widget.ImageView; +import android.widget.AdapterView; +import android.text.Html; + +import java.util.ArrayList; +import java.util.List; +import java.io.IOException; + +import org.xmlpull.v1.XmlPullParserException; + +public class LiveWallpaperListActivity extends ListActivity implements AdapterView.OnItemClickListener { + private static final String LOG_TAG = "LiveWallpapersPicker"; + + private static final int REQUEST_PREVIEW = 100; + + private PackageManager mPackageManager; + + private ArrayList mThumbnails; + private ArrayList mWallpaperInfos; + private ArrayList mWallpaperIntents; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.live_wallpaper_list); + + mPackageManager = getPackageManager(); + + findLiveWallpapers(); + + setListAdapter(new LiveWallpapersAdapter()); + getListView().setOnItemClickListener(this); + } + + private void findLiveWallpapers() { + List list = mPackageManager.queryIntentServices( + new Intent(WallpaperService.SERVICE_INTERFACE), + PackageManager.GET_META_DATA); + + int listSize = list.size(); + + mThumbnails = new ArrayList(listSize); + mWallpaperIntents = new ArrayList(listSize); + mWallpaperInfos = new ArrayList(listSize); + + Resources res = getResources(); + Drawable galleryIcon = res.getDrawable(R.drawable.livewallpaper_placeholder); + + Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); + paint.setTextAlign(Paint.Align.CENTER); + + Canvas canvas = new Canvas(); + + for (int i = 0; i < listSize; i++) { + ResolveInfo resolveInfo = list.get(i); + ComponentInfo ci = resolveInfo.serviceInfo; + WallpaperInfo info; + try { + info = new WallpaperInfo(this, resolveInfo); + } catch (XmlPullParserException e) { + Log.w(LOG_TAG, "Skipping wallpaper " + ci, e); + continue; + } catch (IOException e) { + Log.w(LOG_TAG, "Skipping wallpaper " + ci, e); + continue; + } + + String packageName = info.getPackageName(); + String className = info.getServiceName(); + + Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); + intent.setClassName(packageName, className); + + mWallpaperIntents.add(intent); + mWallpaperInfos.add(info); + + Drawable thumb = info.loadThumbnail(mPackageManager); + if (thumb == null) { + int thumbWidth = res.getDimensionPixelSize(R.dimen.live_wallpaper_thumbnail_width); + int thumbHeight = res.getDimensionPixelSize(R.dimen.live_wallpaper_thumbnail_height); + + Bitmap thumbnail = Bitmap.createBitmap(thumbWidth, thumbHeight, + Bitmap.Config.ARGB_8888); + + paint.setColor(res.getColor(R.color.live_wallpaper_thumbnail_background)); + canvas.setBitmap(thumbnail); + canvas.drawPaint(paint); + + galleryIcon.setBounds(0, 0, thumbWidth, thumbHeight); + ((BitmapDrawable) galleryIcon).setGravity(Gravity.CENTER); + galleryIcon.draw(canvas); + + String title = info.loadLabel(mPackageManager).toString(); + + paint.setColor(res.getColor(R.color.live_wallpaper_thumbnail_text_color)); + paint.setTextSize( + res.getDimensionPixelSize(R.dimen.live_wallpaper_thumbnail_text_size)); + + canvas.drawText(title, (int) (thumbWidth * 0.5), + thumbHeight - res.getDimensionPixelSize( + R.dimen.live_wallpaper_thumbnail_text_offset), paint); + + thumb = new BitmapDrawable(res, thumbnail); + } + + thumb.setDither(true); + mThumbnails.add(thumb); + } + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + if (requestCode == REQUEST_PREVIEW) { + if (resultCode == RESULT_OK) finish(); + } + } + + public void onItemClick(AdapterView parent, View view, int position, long id) { + final Intent intent = mWallpaperIntents.get(position); + final WallpaperInfo info = mWallpaperInfos.get(position); + LiveWallpaperPreview.showPreview(this, REQUEST_PREVIEW, intent, info); + } + + static class ViewHolder { + TextView titleAuthor; + TextView description; + ImageView thumbnail; + } + + private class LiveWallpapersAdapter extends BaseAdapter { + private final LayoutInflater mInflater; + + LiveWallpapersAdapter() { + mInflater = LayoutInflater.from(LiveWallpaperListActivity.this); + } + + public int getCount() { + return mWallpaperInfos.size(); + } + + public Object getItem(int position) { + return mWallpaperInfos.get(position); + } + + public long getItemId(int position) { + return position; + } + + public View getView(int position, View convertView, ViewGroup parent) { + ViewHolder holder; + if (convertView == null) { + convertView = mInflater.inflate(R.layout.live_wallpaper_entry, parent, false); + + holder = new ViewHolder(); + holder.titleAuthor = (TextView) convertView.findViewById(R.id.title_author); + holder.description = (TextView) convertView.findViewById(R.id.description); + holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail); + convertView.setTag(holder); + } else { + holder = (ViewHolder) convertView.getTag(); + } + + WallpaperInfo info = mWallpaperInfos.get(position); + holder.thumbnail.setImageDrawable(mThumbnails.get(position)); + holder.titleAuthor.setText(getString(R.string.wallpaper_title_and_author, + info.loadLabel(mPackageManager), info.loadAuthor(mPackageManager))); + holder.description.setText(Html.fromHtml( + info.loadDescription(mPackageManager).toString())); + + return convertView; + } + } +} diff --git a/src/com/android/wallpaper/livepicker/LiveWallpaperPickActivity.java b/src/com/android/wallpaper/livepicker/LiveWallpaperPickActivity.java deleted file mode 100644 index b93b01c..0000000 --- a/src/com/android/wallpaper/livepicker/LiveWallpaperPickActivity.java +++ /dev/null @@ -1,411 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.wallpaper.livepicker; - -import org.xmlpull.v1.XmlPullParserException; - -import android.app.Activity; -import android.app.WallpaperInfo; -import android.app.WallpaperManager; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; -import android.content.pm.ComponentInfo; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.content.res.Resources.NotFoundException; -import android.graphics.drawable.Drawable; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.os.Bundle; -import android.os.IBinder; -import android.os.ParcelFileDescriptor; -import android.os.RemoteException; -import android.service.wallpaper.IWallpaperConnection; -import android.service.wallpaper.IWallpaperEngine; -import android.service.wallpaper.IWallpaperService; -import android.service.wallpaper.WallpaperService; -import android.service.wallpaper.WallpaperSettingsActivity; -import android.text.Html; -import android.util.DisplayMetrics; -import android.util.Log; -import android.view.Gravity; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.WindowManager; -import android.widget.AdapterView; -import android.widget.BaseAdapter; -import android.widget.Button; -import android.widget.Gallery; -import android.widget.ImageView; -import android.widget.TextView; - -import java.io.IOException; -import java.util.List; -import java.util.ArrayList; - -/** - * Displays a list of live wallpapers, allowing the user to select one - * and make it the system global wallpaper. - */ -public class LiveWallpaperPickActivity extends Activity implements - AdapterView.OnItemSelectedListener, View.OnClickListener { - - private static final String TAG = "LiveWallpaperPickActivity"; - - private PackageManager mPackageManager; - private WallpaperManager mWallpaperManager; - - Intent mSelectedIntent; - WallpaperInfo mSelectedInfo; - WallpaperConnection mWallpaperConnection; - - private Button mConfigureButton; - private TextView mWallpaperTitle; - private TextView mWallpaperAuthor; - private TextView mWallpaperSynopsis; - - private ArrayList mWallpaperIntents; - private ArrayList mWallpaperInfos; - - private ArrayList mThumbnails; - - class WallpaperConnection extends IWallpaperConnection.Stub implements ServiceConnection { - final Intent mIntent; - IWallpaperService mService; - IWallpaperEngine mEngine; - boolean mConnected; - - WallpaperConnection(Intent intent) { - mIntent = intent; - } - - public boolean connect() { - synchronized (this) { - if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) { - return false; - } - - mConnected = true; - return true; - } - } - - public void disconnect() { - synchronized (this) { - mConnected = false; - if (mEngine != null) { - try { - mEngine.destroy(); - } catch (RemoteException e) { - // Ignore - } - mEngine = null; - } - unbindService(this); - mService = null; - } - } - - public void onServiceConnected(ComponentName name, IBinder service) { - if (mWallpaperConnection == this) { - mService = IWallpaperService.Stub.asInterface(service); - try { - View button = findViewById(R.id.set); - mService.attach(this, button.getWindowToken(), - WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA, - true, - button.getRootView().getWidth(), - button.getRootView().getHeight()); - } catch (RemoteException e) { - Log.w(TAG, "Failed attaching wallpaper; clearing", e); - } - } - } - - public void onServiceDisconnected(ComponentName name) { - mService = null; - mEngine = null; - if (mWallpaperConnection == this) { - Log.w(TAG, "Wallpaper service gone: " + name); - } - } - - public void attachEngine(IWallpaperEngine engine) { - synchronized (this) { - if (mConnected) { - mEngine = engine; - try { - engine.setVisibility(true); - } catch (RemoteException e) { - // Ignore - } - } else { - try { - engine.destroy(); - } catch (RemoteException e) { - // Ignore - } - } - } - } - - public ParcelFileDescriptor setWallpaper(String name) { - return null; - } - } - - private class ImageAdapter extends BaseAdapter { - private LayoutInflater mLayoutInflater; - - ImageAdapter(LiveWallpaperPickActivity context) { - mLayoutInflater = context.getLayoutInflater(); - } - - public int getCount() { - return mThumbnails.size(); - } - - public Object getItem(int position) { - return position; - } - - public long getItemId(int position) { - return position; - } - - public View getView(int position, View convertView, ViewGroup parent) { - ImageView image; - - if (convertView == null) { - image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false); - image.setLayoutParams(new Gallery.LayoutParams( - ViewGroup.LayoutParams.WRAP_CONTENT, - ViewGroup.LayoutParams.FILL_PARENT)); - image.setAdjustViewBounds(true); - image.setScaleType(ImageView.ScaleType.FIT_CENTER); - } else { - image = (ImageView) convertView; - } - - image.setImageDrawable(mThumbnails.get(position)); - - return image; - } - } - - - private void findLiveWallpapers() { - mThumbnails = new ArrayList(24); - List list = mPackageManager.queryIntentServices(getTargetIntent(), - PackageManager.GET_META_DATA); - - mWallpaperIntents = new ArrayList(list.size()); - mWallpaperInfos = new ArrayList(list.size()); - - int listSize = list.size(); - DisplayMetrics metrics = new DisplayMetrics(); - getWindowManager().getDefaultDisplay().getMetrics(metrics); - - Drawable galleryIcon = getResources().getDrawable(R.drawable.livewallpaper_placeholder); - - Paint pt = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DITHER_FLAG); - pt.setTextAlign(Paint.Align.CENTER); - - Canvas canvas = new Canvas(); - - for (int i = 0; i < listSize; i++) { - ResolveInfo resolveInfo = list.get(i); - ComponentInfo ci = resolveInfo.serviceInfo; - WallpaperInfo winfo; - try { - winfo = new WallpaperInfo(this, resolveInfo); - } catch (XmlPullParserException e) { - Log.w(TAG, "Skipping wallpaper " + ci, e); - continue; - } catch (IOException e) { - Log.w(TAG, "Skipping wallpaper " + ci, e); - continue; - } - - String packageName = winfo.getPackageName(); - String className = winfo.getServiceName(); - Intent intent = new Intent(getTargetIntent()); - intent.setClassName(packageName, className); - mWallpaperIntents.add(intent); - mWallpaperInfos.add(winfo); - - Drawable thumb = winfo.loadThumbnail(mPackageManager); - if (thumb == null) { - final int thumbWidth = (int) (180 * metrics.density); - final int thumbHeight = (int) (160 * metrics.density); - Bitmap thumbBit = Bitmap.createBitmap(thumbWidth, thumbHeight, - Bitmap.Config.ARGB_8888); - pt.setARGB(204,102,102,102); - canvas.setBitmap(thumbBit); - canvas.drawPaint(pt); - - galleryIcon.setBounds(0, 0, thumbWidth, thumbHeight); - ((BitmapDrawable) galleryIcon).setGravity(Gravity.CENTER); - galleryIcon.draw(canvas); - - pt.setARGB(255, 255, 255, 255); - pt.setTextSize(20 * metrics.density); - canvas.drawText(className.substring(className.lastIndexOf('.') + 1), - (int) (thumbWidth * 0.5), (int) (thumbHeight - 22 * metrics.density), pt); - thumb = new BitmapDrawable(getResources(), thumbBit); - } - - thumb.setDither(true); - mThumbnails.add(thumb); - } - } - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - - mPackageManager = getPackageManager(); - mWallpaperManager = WallpaperManager.getInstance(this); - - findLiveWallpapers(); - - setContentView(R.layout.live_wallpaper_content); - - Gallery gallery = (Gallery) findViewById(R.id.gallery); - gallery.setAdapter(new ImageAdapter(this)); - gallery.setOnItemSelectedListener(this); - gallery.setCallbackDuringFling(false); - - View button = findViewById(R.id.set); - button.setOnClickListener(this); - - mConfigureButton = (Button)findViewById(R.id.configure); - mConfigureButton.setVisibility(View.GONE); - mConfigureButton.setOnClickListener(this); - - mWallpaperTitle = (TextView)findViewById(R.id.title); - mWallpaperAuthor = (TextView)findViewById(R.id.author); - mWallpaperSynopsis = (TextView)findViewById(R.id.synopsis); - - // Set default return data - setResult(RESULT_CANCELED); - } - - @Override - public void onResume() { - super.onResume(); - if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) { - try { - mWallpaperConnection.mEngine.setVisibility(true); - } catch (RemoteException e) { - // Ignore - } - } - } - - @Override - public void onPause() { - super.onPause(); - if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) { - try { - mWallpaperConnection.mEngine.setVisibility(false); - } catch (RemoteException e) { - // Ignore - } - } - } - - @Override - public void onDetachedFromWindow() { - super.onDetachedFromWindow(); - if (mWallpaperConnection != null) { - mWallpaperConnection.disconnect(); - } - mWallpaperConnection = null; - } - - protected Intent getTargetIntent() { - return new Intent(WallpaperService.SERVICE_INTERFACE); - } - - public void onItemSelected(AdapterView parent, View v, int position, long id) { - mSelectedIntent = mWallpaperIntents.get(position); - mSelectedInfo = mWallpaperInfos.get(position); - mConfigureButton.setVisibility( - (mSelectedInfo != null && - mSelectedInfo.getSettingsActivity() != null) - ? View.VISIBLE - : View.GONE); - findViewById(R.id.set).setEnabled(true); - - mWallpaperTitle.setText(mSelectedInfo.loadLabel(mPackageManager).toString()); - try { - mWallpaperAuthor.setText(mSelectedInfo.loadAuthor(mPackageManager).toString()); - } catch (NotFoundException e) { - mWallpaperAuthor.setText(""); - } - try { - String htmlText = mSelectedInfo.loadDescription(mPackageManager).toString(); - mWallpaperSynopsis.setText(Html.fromHtml(htmlText)); - } catch (NotFoundException e) { - mWallpaperSynopsis.setText(""); - } - - WallpaperConnection conn = new WallpaperConnection(mSelectedIntent); - if (conn.connect()) { - if (mWallpaperConnection != null) { - mWallpaperConnection.disconnect(); - } - mWallpaperConnection = conn; - } - } - - public void onClick(View v) { // "Set" button - if (v.getId() == R.id.set) { - if (mSelectedIntent != null) { - try { - mWallpaperManager.getIWallpaperManager().setWallpaperComponent( - mSelectedIntent.getComponent()); - mWallpaperManager.setWallpaperOffsets( - v.getRootView().getWindowToken(), 0.5f, 0.0f); - this.setResult(RESULT_OK); - } catch (RemoteException e) { - // do nothing - } catch (RuntimeException e) { - Log.w(TAG, "Failure setting wallpaper", e); - } - finish(); - } - } else if (v.getId() == R.id.configure) { - if (mSelectedInfo != null && mSelectedInfo.getSettingsActivity() != null) { - Intent intent = new Intent(); - intent.setComponent(new ComponentName(mSelectedInfo.getPackageName(), - mSelectedInfo.getSettingsActivity())); - intent.putExtra(WallpaperSettingsActivity.EXTRA_PREVIEW_MODE, true); - startActivity(intent); - } - } - } - - public void onNothingSelected(AdapterView parent) { - } -} diff --git a/src/com/android/wallpaper/livepicker/LiveWallpaperPreview.java b/src/com/android/wallpaper/livepicker/LiveWallpaperPreview.java new file mode 100644 index 0000000..374f5ec --- /dev/null +++ b/src/com/android/wallpaper/livepicker/LiveWallpaperPreview.java @@ -0,0 +1,224 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.wallpaper.livepicker; + +import android.app.Activity; +import android.app.WallpaperManager; +import android.app.WallpaperInfo; +import android.service.wallpaper.IWallpaperConnection; +import android.service.wallpaper.IWallpaperService; +import android.service.wallpaper.IWallpaperEngine; +import android.service.wallpaper.WallpaperSettingsActivity; +import android.content.ServiceConnection; +import android.content.Intent; +import android.content.Context; +import android.content.ComponentName; +import android.os.RemoteException; +import android.os.IBinder; +import android.os.ParcelFileDescriptor; +import android.os.Bundle; +import android.view.View; +import android.view.WindowManager; +import android.util.Log; + +public class LiveWallpaperPreview extends Activity { + static final String EXTRA_LIVE_WALLPAPER_INTENT = "android.live_wallpaper.intent"; + static final String EXTRA_LIVE_WALLPAPER_SETTINGS = "android.live_wallpaper.settings"; + static final String EXTRA_LIVE_WALLPAPER_PACKAGE = "android.live_wallpaper.package"; + + private static final String LOG_TAG = "LiveWallpaperPreview"; + + private WallpaperManager mWallpaperManager; + private WallpaperConnection mWallpaperConnection; + + private String mSettings; + private String mPackageName; + private Intent mWallpaperIntent; + + static void showPreview(Activity activity, int code, Intent intent, WallpaperInfo info) { + Intent preview = new Intent(activity, LiveWallpaperPreview.class); + preview.putExtra(EXTRA_LIVE_WALLPAPER_INTENT, intent); + preview.putExtra(EXTRA_LIVE_WALLPAPER_SETTINGS, info.getSettingsActivity()); + preview.putExtra(EXTRA_LIVE_WALLPAPER_PACKAGE, info.getPackageName()); + activity.startActivityForResult(preview, code); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + Bundle extras = getIntent().getExtras(); + mWallpaperIntent = (Intent) extras.get(EXTRA_LIVE_WALLPAPER_INTENT); + if (mWallpaperIntent == null) { + setResult(RESULT_CANCELED); + finish(); + } + + setContentView(R.layout.live_wallpaper_preview); + + mSettings = extras.getString(EXTRA_LIVE_WALLPAPER_SETTINGS); + mPackageName = extras.getString(EXTRA_LIVE_WALLPAPER_PACKAGE); + if (mSettings == null) { + findViewById(R.id.configure).setVisibility(View.GONE); + } + + mWallpaperManager = WallpaperManager.getInstance(this); + + WallpaperConnection connection = new WallpaperConnection(mWallpaperIntent); + if (connection.connect()) { + mWallpaperConnection = connection; + } + } + + public void setLiveWallpaper(View v) { + try { + mWallpaperManager.getIWallpaperManager().setWallpaperComponent( + mWallpaperIntent.getComponent()); + mWallpaperManager.setWallpaperOffsets(v.getRootView().getWindowToken(), 0.5f, 0.0f); + setResult(RESULT_OK); + } catch (RemoteException e) { + // do nothing + } catch (RuntimeException e) { + Log.w(LOG_TAG, "Failure setting wallpaper", e); + } + finish(); + } + + @SuppressWarnings({"UnusedDeclaration"}) + public void configureLiveWallpaper(View v) { + Intent intent = new Intent(); + intent.setComponent(new ComponentName(mPackageName, mSettings)); + intent.putExtra(WallpaperSettingsActivity.EXTRA_PREVIEW_MODE, true); + startActivity(intent); + } + + @Override + public void onResume() { + super.onResume(); + if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) { + try { + mWallpaperConnection.mEngine.setVisibility(true); + } catch (RemoteException e) { + // Ignore + } + } + } + + @Override + public void onPause() { + super.onPause(); + if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) { + try { + mWallpaperConnection.mEngine.setVisibility(false); + } catch (RemoteException e) { + // Ignore + } + } + } + + @Override + public void onDetachedFromWindow() { + super.onDetachedFromWindow(); + if (mWallpaperConnection != null) { + mWallpaperConnection.disconnect(); + } + mWallpaperConnection = null; + } + + class WallpaperConnection extends IWallpaperConnection.Stub implements ServiceConnection { + final Intent mIntent; + IWallpaperService mService; + IWallpaperEngine mEngine; + boolean mConnected; + + WallpaperConnection(Intent intent) { + mIntent = intent; + } + + public boolean connect() { + synchronized (this) { + if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) { + return false; + } + + mConnected = true; + return true; + } + } + + public void disconnect() { + synchronized (this) { + mConnected = false; + if (mEngine != null) { + try { + mEngine.destroy(); + } catch (RemoteException e) { + // Ignore + } + mEngine = null; + } + unbindService(this); + mService = null; + } + } + + public void onServiceConnected(ComponentName name, IBinder service) { + if (mWallpaperConnection == this) { + mService = IWallpaperService.Stub.asInterface(service); + try { + final View view = getWindow().getDecorView().getRootView(); + mService.attach(this, view.getWindowToken(), + WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA, + true, view.getWidth(), view.getHeight()); + } catch (RemoteException e) { + Log.w(LOG_TAG, "Failed attaching wallpaper; clearing", e); + } + } + } + + public void onServiceDisconnected(ComponentName name) { + mService = null; + mEngine = null; + if (mWallpaperConnection == this) { + Log.w(LOG_TAG, "Wallpaper service gone: " + name); + } + } + + public void attachEngine(IWallpaperEngine engine) { + synchronized (this) { + if (mConnected) { + mEngine = engine; + try { + engine.setVisibility(true); + } catch (RemoteException e) { + // Ignore + } + } else { + try { + engine.destroy(); + } catch (RemoteException e) { + // Ignore + } + } + } + } + + public ParcelFileDescriptor setWallpaper(String name) { + return null; + } + } +} -- cgit v1.2.3