Java Code Examples for com.android.messaging.ui.WidgetPickConversationActivity

Following code examples demonstrate how to use com.android.messaging.ui.WidgetPickConversationActivityfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use com.android.messaging.ui.WidgetPickConversationActivityand various code implementation of this class.

    public void onReceive(final Context context, final Intent intent) {
        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "WidgetConversationProvider onReceive intent: " + intent);
        }
        final String action = intent.getAction();

        if (getAction().equals(action)) {
            final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context,
                    this.getClass()));

            if (appWidgetIds.length == 0) {
                if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                    LogUtil.v(TAG, "WidgetConversationProvider onReceive no widget ids");
                }
                return;
            }
            final String conversationId = intent.getExtras()
                    .getString(UIIntents.UI_INTENT_EXTRA_CONVERSATION_ID);

            for (final int widgetId : appWidgetIds) {
                final String widgetConvId =
                        WidgetPickConversationActivity.getConversationIdPref(widgetId);
                if (conversationId == null || TextUtils.equals(conversationId, widgetConvId)) {
                    appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, getListId());
                }
            }
        } else {
            super.onReceive(context, intent);
        }
    } 


    
    public static void notifyConversationDeleted(final Context context,
            final String conversationId) {
        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "notifyConversationDeleted convId: " + conversationId);
        }

        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        for (final int appWidgetId : appWidgetManager.getAppWidgetIds(new ComponentName(context,
                WidgetConversationProvider.class))) {
            final String widgetConvId =
                    WidgetPickConversationActivity.getConversationIdPref(appWidgetId);

            if (widgetConvId == null || widgetConvId.equals(conversationId)) {
                if (widgetConvId != null) {
                    WidgetPickConversationActivity.deleteConversationIdPref(appWidgetId);
                }
                rebuildWidget(context, appWidgetId);
            }
        }
    } 

    public static boolean isWidgetConfigured(final int appWidgetId) {
        final String conversationId =
                WidgetPickConversationActivity.getConversationIdPref(appWidgetId);
        return !TextUtils.isEmpty(conversationId);
    } 

    public static void rebuildWidget(final Context context, final int appWidgetId) {
        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "WidgetConversationProvider.rebuildWidget appWidgetId: " + appWidgetId);
        }
        final RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_conversation);
        PendingIntent clickIntent;
        final UIIntents uiIntents = UIIntents.get();
        if (!isWidgetConfigured(appWidgetId)) {
            remoteViews.setViewVisibility(R.id.widget_label, View.GONE);
            remoteViews.setViewVisibility(R.id.message_list, View.GONE);
            remoteViews.setViewVisibility(R.id.launcher_icon, View.VISIBLE);
            remoteViews.setViewVisibility(R.id.widget_configuration, View.VISIBLE);

            remoteViews.setOnClickPendingIntent(R.id.widget_configuration,
                    uiIntents.getWidgetPendingIntentForConfigurationActivity(context, appWidgetId));

            clickIntent = uiIntents.getWidgetPendingIntentForConversationListActivity(context);
            remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);

            if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                LogUtil.v(TAG, "WidgetConversationProvider.rebuildWidget appWidgetId: " +
                        appWidgetId + " going into configure state");
            }
        } else {
            remoteViews.setViewVisibility(R.id.widget_label, View.VISIBLE);
            remoteViews.setViewVisibility(R.id.message_list, View.VISIBLE);
            remoteViews.setViewVisibility(R.id.launcher_icon, View.GONE);
            remoteViews.setViewVisibility(R.id.widget_configuration, View.GONE);

            final String conversationId =
                    WidgetPickConversationActivity.getConversationIdPref(appWidgetId);
            final boolean isMainThread =  Looper.myLooper() == Looper.getMainLooper();
            final ConversationListItemData convData = isMainThread ?
                    null : getConversationData(context, conversationId);

            final Intent intent = new Intent(context, WidgetConversationService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            intent.putExtra(UIIntents.UI_INTENT_EXTRA_CONVERSATION_ID, conversationId);
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            remoteViews.setRemoteAdapter(appWidgetId, R.id.message_list, intent);

            remoteViews.setTextViewText(R.id.widget_label, convData != null ?
                    convData.getName() : context.getString(R.string.app_name));

            clickIntent = uiIntents.getWidgetPendingIntentForConversationListActivity(context);
            remoteViews.setOnClickPendingIntent(R.id.widget_goto_conversation_list, clickIntent);

            clickIntent = uiIntents.getWidgetPendingIntentForConversationActivity(context,
                    conversationId, WIDGET_CONVERSATION_REQUEST_CODE);
            remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);

            clickIntent = uiIntents.getWidgetPendingIntentForConversationActivity(context,
                    conversationId, WIDGET_CONVERSATION_TEMPLATE_REQUEST_CODE);
            remoteViews.setPendingIntentTemplate(R.id.message_list, clickIntent);

            if (isMainThread) {
                SafeAsyncTask.executeOnThreadPool(new Runnable() {
                    @Override
                    public void run() {
                        rebuildWidget(context, appWidgetId);
                    }
                });
            }
        }

        AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);

    } 

    
    public static void notifyConversationRenamed(final Context context,
            final String conversationId) {
        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "notifyConversationRenamed convId: " + conversationId);
        }

        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        for (final int appWidgetId : appWidgetManager.getAppWidgetIds(new ComponentName(context,
                WidgetConversationProvider.class))) {
            final String widgetConvId =
                    WidgetPickConversationActivity.getConversationIdPref(appWidgetId);

            if (widgetConvId != null && widgetConvId.equals(conversationId)) {
                rebuildWidget(context, appWidgetId);
            }
        }
    }