Advertisement
Guest User

Untitled

a guest
Dec 27th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. AppWidgetHost widget_host = new AppWidgetHost(this, 1);
  2. AppWidgetManager widget_manager = AppWidgetManager.getInstance(this);
  3.  
  4. int widget_id = widget_host.allocateAppWidgetId();
  5. AppWidgetProviderInfo widget_provider = ... //from an array;
  6.  
  7. Intent bindIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
  8. bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
  9. bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, widget_provider.provider);
  10. startActivityForResult(bindIntent, REQUEST_BIND_APPWIDGET);
  11.  
  12. if (widget_provider.configure != null) {
  13. Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
  14. intent.setComponent(widget_provider.configure);
  15. intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
  16. startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
  17. } else {
  18. createWidget(widget_id);
  19. }
  20.  
  21. Intent bindIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
  22. bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
  23. bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, widget_provider.provider);
  24. startActivityForResult(bindIntent, REQUEST_BIND_APPWIDGET);
  25.  
  26. Boolean callProviderIntent = false;
  27. if (checkCallProviderIntent)
  28. {
  29. callProviderIntent = true;
  30. Method m = null;
  31. try
  32. {
  33. m = AppWidgetManager.class
  34. .getMethod("bindAppWidgetIdIfAllowed", new Class[]
  35. { Integer.TYPE, ComponentName.class });
  36. }
  37. catch (NoSuchMethodException e)
  38. {
  39. }
  40. if (m != null)
  41. {
  42. try
  43. {
  44. callProviderIntent = !(Boolean) m
  45. .invoke(mAppWidgetManager,
  46. appWidgetId,
  47. launcherAppWidgetInfo.provider);
  48. }
  49. catch (Exception e)
  50. {
  51. }
  52. }
  53. }
  54.  
  55. AppWidgetManager manager = m.getAppWidgetManager();
  56. AppWidgetHost host = m.getWidgetHost();
  57.  
  58. List<AppWidgetProviderInfo> widgetList = manager.getInstalledProviders();
  59.  
  60. AppWidgetProviderInfo provider = null;
  61. for(AppWidgetProviderInfo info : widgetList){
  62. //To get the google search box
  63. if(info.provider.getClassName().equals("com.google.android.googlequicksearchbox.SearchWidgetProvider")){
  64. provider = info;
  65. break;
  66. }
  67. }
  68. if(provider != null){
  69. int id = host.allocateAppWidgetId();
  70.  
  71. boolean success = false;
  72. success = manager.bindAppWidgetIdIfAllowed(id, provider.provider);
  73. if (success) {
  74. AppWidgetHostView hostView = host.createView(getActivity(), id, provider);
  75. AppWidgetProviderInfo appWidgetInfo = manager.getAppWidgetInfo(id);
  76.  
  77. LauncherAppWidgetInfo info = new LauncherAppWidgetInfo(id);
  78. info.hostView = hostView;
  79. info.hostView.setAppWidget(id, appWidgetInfo);
  80. attachWidget(info);
  81. } else {
  82. Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
  83. intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
  84. intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER,
  85. provider.provider);
  86. // TODO: we need to make sure that this accounts for the options
  87. // bundle.
  88. // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS,
  89. // options);
  90. m.startActivityForResult(intent, Main.REQUEST_BIND_APPWIDGET);
  91. }
  92.  
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement