Adding WidgetPreview utility.

Change-Id: Idfdf866400687e58304863c3c4f3dd3c4aed45b1
This commit is contained in:
Winson Chung
2011-01-16 17:12:47 -08:00
parent e63a4b58a2
commit eb80909dfc
12 changed files with 546 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
#
# Copyright (C) 2010 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.
#
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := samples
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := WidgetPreview
LOCAL_SDK_VERSION := current
include $(BUILD_PACKAGE)

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.widgetpreview">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.BIND_APPWIDGET" />
<application android:label="@string/application_label">
<activity
android:name="WidgetPreviewActivity"
android:label="@string/application_label"
android:icon="@drawable/ic_widget_preview">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 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.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<Button
android:id="@+id/email_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/email_button" />
<Button
android:id="@+id/snapshot_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/email_button"
android:text="@string/snapshot_button" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@id/snapshot_button"
android:layout_centerHorizontal="true">
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
</FrameLayout>
</RelativeLayout>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 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.
-->
<resources>
<dimen name="workspace_cell_width">106dip</dimen>
<dimen name="workspace_cell_height">74dip</dimen>
<dimen name="workspace_width_gap">0dp</dimen>
<dimen name="workspace_height_gap">0dp</dimen>
</resources>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 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.
-->
<resources>
<dimen name="workspace_cell_width">80dip</dimen>
<dimen name="workspace_cell_height">100dip</dimen>
<dimen name="workspace_width_gap">0dp</dimen>
<dimen name="workspace_height_gap">0dp</dimen>
</resources>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 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.
-->
<resources>
<dimen name="workspace_cell_width">96dip</dimen>
<dimen name="workspace_cell_height">96dip</dimen>
<dimen name="workspace_width_gap">0dp</dimen>
<dimen name="workspace_height_gap">0dp</dimen>
</resources>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 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.
-->
<resources>
<dimen name="preview_cell_size">196dip</dimen>
</resources>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 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.
-->
<resources>
<string name="application_label">Widget Preview</string>
<string name="saving_preview">Saving widget preview...</string>
<string name="preview_saved">Widget preview saved!</string>
<string name="no_preview">Please take a snapshot first</string>
<string name="preview_save_error">Error saving preview</string>
<string name="configure_error">Error configuring, no configuration activity found</string>
<string name="email_subject">Widget preview</string>
<string name="email_body">Attached is the preview of your AppWidget</string>
<string name="snapshot_button">Take Snapshot</string>
<string name="email_button">Email Preview</string>
</resources>

View File

@@ -0,0 +1,322 @@
/*
* Copyright (C) 2010 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.widgetpreview;
import android.app.Activity;
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WidgetPreviewActivity extends Activity implements OnClickListener {
private static final String LOG_TAG = "WidgetPreviewActivity";
private static final boolean DEBUG = true;
private static final int APPWIDGET_HOST_ID = 2048;
private static final int REQUEST_WIDGET = 0;
private static final int REQUEST_CONFIGURE = 1;
private AppWidgetHost mAppWidgetHost = null;
private FrameLayout mAppWidgetFrame = null;
private AppWidgetHostView mAppWidgetView = null;
private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private String mAppWidgetName;
private int mPreviewWidth;
private int mPreviewHeight;
private Button mSnapshotButton = null;
private Button mEmailButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppWidgetFrame = (FrameLayout)findViewById(R.id.main_frame);
mSnapshotButton = (Button)findViewById(R.id.snapshot_button);
mSnapshotButton.setOnClickListener(this);
mEmailButton = (Button)findViewById(R.id.email_button);
mEmailButton.setOnClickListener(this);
mAppWidgetHost = new AppWidgetHost(getApplicationContext(), APPWIDGET_HOST_ID);
final Object retainedObj = getLastNonConfigurationInstance();
if (retainedObj instanceof AppWidgetProviderInfo) {
AppWidgetProviderInfo info = (AppWidgetProviderInfo) retainedObj;
int id = mAppWidgetHost.allocateAppWidgetId();
AppWidgetManager.getInstance(getBaseContext()).bindAppWidgetId(id, info.provider);
setAppWidget(id);
} else {
startChooseActivity();
}
}
@Override
public void onStart() {
super.onStart();
mAppWidgetHost.startListening();
}
@Override
public Object onRetainNonConfigurationInstance() {
AppWidgetProviderInfo info = AppWidgetManager.getInstance(
getBaseContext()).getAppWidgetInfo(mAppWidgetId);
return info;
}
private void startChooseActivity() {
int id = mAppWidgetHost.allocateAppWidgetId();
Intent selectIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
selectIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
startActivityForResult(selectIntent, REQUEST_WIDGET);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_WIDGET) {
if (data != null) {
int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
if (data.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
appWidgetId = data.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
}
if (resultCode == RESULT_OK) {
setAppWidget(appWidgetId);
} else {
mAppWidgetHost.deleteAppWidgetId(appWidgetId);
finish();
}
} else {
finish();
}
} else if (requestCode == REQUEST_CONFIGURE) {
if (data != null) {
int appWidgetId = data.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (resultCode == RESULT_OK) {
finishSetAppWidget(appWidgetId);
} else {
mAppWidgetHost.deleteAppWidgetId(appWidgetId);
}
}
}
}
private void setAppWidget(int appWidgetId) {
if (mAppWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
mAppWidgetHost.deleteAppWidgetId(mAppWidgetId);
}
/* Check for configuration */
AppWidgetProviderInfo providerInfo =
AppWidgetManager.getInstance(getBaseContext()).getAppWidgetInfo(appWidgetId);
if (providerInfo.configure != null) {
Intent configureIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
configureIntent.setComponent(providerInfo.configure);
configureIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
if (configureIntent != null) {
try {
startActivityForResult(configureIntent, REQUEST_CONFIGURE);
} catch (ActivityNotFoundException e) {
Log.d(LOG_TAG, "Configuration activity not found: " + e);
Toast errorToast = Toast.makeText(
getBaseContext(), R.string.configure_error, Toast.LENGTH_SHORT);
errorToast.show();
}
}
} else {
finishSetAppWidget(appWidgetId);
}
}
private void finishSetAppWidget(int appWidgetId) {
AppWidgetProviderInfo providerInfo =
AppWidgetManager.getInstance(getBaseContext()).getAppWidgetInfo(appWidgetId);
if (providerInfo != null) {
mAppWidgetView =
mAppWidgetHost.createView(getBaseContext(), appWidgetId, providerInfo);
int [] dimensions =
getLauncherCellDimensions(providerInfo.minWidth, providerInfo.minHeight);
mPreviewWidth = dimensions[0];
mPreviewHeight = dimensions[1];
mAppWidgetName =
AppWidgetManager.getInstance(getBaseContext()).getAppWidgetInfo(appWidgetId).label;
mAppWidgetName = mAppWidgetName.replaceAll("[^a-zA-Z0-9]", "_");
ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mAppWidgetView.setLayoutParams(p);
mAppWidgetFrame.removeAllViews();
mAppWidgetHost.deleteAppWidgetId(mAppWidgetId);
mAppWidgetFrame.addView(mAppWidgetView, mPreviewWidth, mPreviewHeight);
mAppWidgetId = appWidgetId;
}
}
// Taken from CellLayout.java
public int[] getLauncherCellDimensions(int width, int height) {
// Always assume we're working with the smallest span to make sure we
// reserve enough space in both orientations.
Resources resources = getResources();
int cellWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
int cellHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
int widthGap = resources.getDimensionPixelSize(R.dimen.workspace_width_gap);
int heightGap = resources.getDimensionPixelSize(R.dimen.workspace_height_gap);
int previewCellSize = resources.getDimensionPixelSize(R.dimen.preview_cell_size);
// This logic imitates Launcher's CellLayout.rectToCell.
// Always round up to next largest cell
int smallerSize = Math.min(cellWidth, cellHeight);
int spanX = (width + smallerSize) / smallerSize;
int spanY = (height + smallerSize) / smallerSize;
// We use a fixed preview cell size so that you get the same preview image for
// the same cell-sized widgets across all devices
width = spanX * previewCellSize + ((spanX - 1) * widthGap);
height = spanY * previewCellSize + ((spanY - 1) * heightGap);
return new int[] { width, height };
}
private File buildFile(String name) {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return null;
}
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
int orientationCode = getResources().getConfiguration().orientation;
String orientation;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
orientation = "landscape";
} else if (orientationCode == Configuration.ORIENTATION_PORTRAIT) {
orientation = "portrait";
} else if (orientationCode == Configuration.ORIENTATION_SQUARE) {
orientation = "square";
} else {
orientation = "undefined";
}
return new File(path, name + "_ori_" + orientation + ".png");
}
public Bitmap getPreviewBitmap() {
mAppWidgetView.invalidate();
Bitmap bmp = Bitmap.createBitmap(
mAppWidgetView.getWidth(), mAppWidgetView.getHeight(), Config.ARGB_8888);
Canvas c = new Canvas(bmp);
mAppWidgetView.draw(c);
return bmp;
}
private boolean saveImage(Bitmap bmp, String name) {
File pic = buildFile(mAppWidgetName);
if (pic == null) {
Log.d(LOG_TAG, "External storage not present");
return false;
}
pic.getParentFile().mkdirs();
FileOutputStream fout = null;
try {
fout = new FileOutputStream(pic);
if (!bmp.compress(CompressFormat.PNG, 100, fout)) {
Log.d(LOG_TAG, "Failed to compress image");
return false;
}
return true;
} catch (IOException e) {
Log.d(LOG_TAG, "Error writing to disk: " + e);
} finally {
try {
if (fout != null) {
fout.close();
}
} catch (IOException e) {
Log.d(LOG_TAG, "Could not close file: " + e);
}
}
return false;
}
@Override
public void onBackPressed() {
if (!getFragmentManager().popBackStackImmediate()) {
startChooseActivity();
}
}
@Override
public void onClick(View v) {
if (v == mSnapshotButton) {
int textId = R.string.saving_preview;
Toast preToast = Toast.makeText(getBaseContext(), textId, Toast.LENGTH_SHORT);
preToast.show();
Bitmap bmp = getPreviewBitmap();
if (saveImage(bmp, mAppWidgetName)) {
textId = R.string.preview_saved;
} else {
textId = R.string.preview_save_error;
}
Toast postToast = Toast.makeText(getBaseContext(), textId, Toast.LENGTH_SHORT);
postToast.show();
} else if (v == mEmailButton) {
File file = buildFile(mAppWidgetName);
if (file.exists()) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("image/png");
emailIntent.putExtra(Intent.EXTRA_SUBJECT,
getResources().getString(R.string.email_subject));
emailIntent.putExtra(Intent.EXTRA_TEXT,
getResources().getString(R.string.email_body));
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(emailIntent);
} else {
Toast postToast = Toast.makeText(
getBaseContext(), R.string.no_preview, Toast.LENGTH_SHORT);
postToast.show();
}
}
}
}

View File

@@ -171,6 +171,7 @@ development/samples/SpinnerTest samples/${PLATFORM_NAME}/SpinnerTes
development/samples/TicTacToeLib samples/${PLATFORM_NAME}/TicTacToeLib development/samples/TicTacToeLib samples/${PLATFORM_NAME}/TicTacToeLib
development/samples/TicTacToeMain samples/${PLATFORM_NAME}/TicTacToeMain development/samples/TicTacToeMain samples/${PLATFORM_NAME}/TicTacToeMain
development/samples/VoiceRecognitionService samples/${PLATFORM_NAME}/VoiceRecognitionService development/samples/VoiceRecognitionService samples/${PLATFORM_NAME}/VoiceRecognitionService
development/apps/WidgetPreview samples/${PLATFORM_NAME}/WidgetPreview
development/samples/Wiktionary samples/${PLATFORM_NAME}/Wiktionary development/samples/Wiktionary samples/${PLATFORM_NAME}/Wiktionary
development/samples/WiktionarySimple samples/${PLATFORM_NAME}/WiktionarySimple development/samples/WiktionarySimple samples/${PLATFORM_NAME}/WiktionarySimple
development/samples/XmlAdapters samples/${PLATFORM_NAME}/XmlAdapters development/samples/XmlAdapters samples/${PLATFORM_NAME}/XmlAdapters