Reputation: 3455
I have widgets that are automatically added to the launcher, but some of these widgets need to be configured so I set an onClick listener to get the id and get the widget info to launch the Activity to configure the widget, but the problem I am having is when I return from the Activity of say a weather widget, the part where it is failing is in the create method , when I try get the appWidgetInfo by the widget id, but the id comes back with the intent. I dont know what is going wrong can anyone help me with this problem please??
AppWidgetManager mAppWidgetManager;
AppWidgetHost mAppWidgetHost;
LinearLayout mainlayout;
ComponentName cn = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainlayout = (LinearLayout) findViewById(R.id.mylauncher);
mainlayout.setOnClickListener(this);
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);
// search widgets
searchWidgets();
}
public void searchWidgets() {
String widget = "com.weather.Weather.widgets.WeatherWidgetProvider4x1";
try {
mAppWidgetManager = AppWidgetManager.getInstance(getBaseContext());
final List<AppWidgetProviderInfo> infos = mAppWidgetManager
.getInstalledProviders();
AppWidgetProviderInfo appWidgetInfo = null;
for (final AppWidgetProviderInfo info : infos) {
Log.v("Available Widgets", "label " + info.label + " ::"
+ info.provider.getPackageName() + " / "
+ info.provider.getClassName());
}
for (final AppWidgetProviderInfo info : infos) {
Log.v("Searching for Widget class",
info.provider.getPackageName() + " / "
+ info.provider.getClassName());
if (widget.equals(info.provider.getClassName())) {
String packageName = info.provider.getPackageName()
.toString();
cn = new ComponentName(packageName, widget);
// found it
appWidgetInfo = info;
Log.d("FOUND !!!!!!!!!", "Widget class " + packageName);
break;
}
}
mAppWidgetHost = new AppWidgetHost(getBaseContext(),
R.id.APPWIDGET_HOST_ID);
if (appWidgetInfo == null) {
Log.d(TAG, cn + " No such widget available returning view");
// return; // stop here
} else {
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
AppWidgetHostView hostView = mAppWidgetHost.createView(this,
appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
AppWidgetHostView hostViewsecond = mAppWidgetHost.createView(
this, appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
hostView.setId(appWidgetInfo.previewImage);
Drawable draw = null;
String packageName = appWidgetInfo.provider.getPackageName();
try {
draw = this.getPackageManager().getDrawable(packageName,
appWidgetInfo.previewImage,
this.getPackageManager().getApplicationInfo(packageName,
0));
} catch (NameNotFoundException e) {
Log.d(TAG, "drawable not found",e);
}
ImageView preViewWigdet = new ImageView(this);
preViewWigdet.setImageDrawable(draw);
preViewWigdet.setId(appWidgetInfo.previewImage);
mainlayout.addView(preViewWigdet);
}
} catch (SecurityException securityEx) {
Log.e("Bind Widget",
"Error device not rooted, returning view, Security exception:- ",
securityEx);
}
}
@Override
public void onClick(View v) {
AppWidgetProviderInfo widgetItem = null;
mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
if (view.getClass().equals(ImageView.class)){
// putting installed Providers in a list
final List<AppWidgetProviderInfo> infos = mAppWidgetManager
.getInstalledProviders();
for (int i = 0; i < infos.size(); i++) {
if (infos.get(i).previewImage == view.getId()) {
widgetItem = (AppWidgetProviderInfo) infos.get(i);
}
}
ComponentName cn = new ComponentName(
widgetItem.provider.getPackageName(),
widgetItem.provider.getClassName());
int[] ids;
ids = mAppWidgetManager.getAppWidgetIds(cn);
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
intent.setComponent(widgetItem.configure);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
if (widgetItem.configure != null) {
Log.d(TAG, "widget to be configured " + widgetItem.label);
((Activity) this).startActivityForResult(intent,
R.id.REQUEST_CREATE_APPWIDGET);
}
}
}
/**
* If the user has selected an widget, the result will be in the 'data' when
* this function is called.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "" + resultCode);
if (resultCode == RESULT_OK) {
if (requestCode == R.id.REQUEST_PICK_APPWIDGET) {
configureWidget(data);
}
if (requestCode == R.id.REQUEST_CREATE_APPWIDGET) {
mAppWidgetManager = AppWidgetManager.getInstance(this);
createWidget(data);
}
} else if (resultCode == RESULT_CANCELED && data != null) {
int appWidgetId = data.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
if (appWidgetId != -1) {
mAppWidgetHost.deleteAppWidgetId(appWidgetId);
}
}
}
/**
* Checks if the widget needs any configuration. If it needs, launches the
* configuration activity.
*/
private void configureWidget(Intent data) {
Bundle extras = data.getExtras();
int appWidgetId = extras
.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager
.getAppWidgetInfo(appWidgetId);
if (appWidgetInfo.configure != null) {
Intent intent = new Intent(
AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
intent.setComponent(appWidgetInfo.configure);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
startActivityForResult(intent, R.id.REQUEST_CREATE_APPWIDGET);
} else {
createWidget(data);
}
}
/**
* Creates the widget and adds to our view layout.
*/
public void createWidget(Intent data) {
Bundle extras = data.getExtras();
int appWidgetId = extras
.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
//this is where I am having the problem, appWidgetInfo returns null
AppWidgetProviderInfo appWidgetInfo = this.mAppWidgetManager
.getAppWidgetInfo(appWidgetId);
AppWidgetHostView hostView = mAppWidgetHost.createView(this,
appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
mainlayout.addView(hostView);
Log.i(TAG, "The widget size is: " + appWidgetInfo.minWidth + "*"
+ appWidgetInfo.minHeight);
}
Upvotes: 0
Views: 1014
Reputation: 11
i'd guess the line "this.mAppWidgetManager.getAppWidgetInfo(appWidgetId)" returns null as i cant see anywhere in your code where you bind the Appwidget id to the AppwidgethostInfo provider
try googleing the bindAppWidgetIdIfAllowed function to see how it works and you may need to make another Activity call to ACTION_APPWIDGET_BIND
Upvotes: 1