Search in sources :

Example 1 with WidgetView

use of rocks.tbog.tblauncher.ui.WidgetView in project TBLauncher by TBog.

the class WidgetManager method getConfigPopup.

/**
 * Popup with options for all widgets
 *
 * @param activity used to start the widget select popup
 * @return the popup menu
 */
public ListPopup getConfigPopup(Activity activity) {
    LinearAdapter adapter = new LinearAdapter();
    adapter.add(new LinearAdapter.ItemTitle(activity, R.string.menu_widget_title));
    adapter.add(new LinearAdapter.Item(activity, R.string.menu_widget_add));
    if (widgetCount() > 0) {
        adapter.add(new LinearAdapter.Item(activity, R.string.menu_widget_configure));
        adapter.add(new LinearAdapter.Item(activity, R.string.menu_widget_remove));
    }
    adapter.add(new LinearAdapter.Item(activity, R.string.menu_popup_launcher_settings));
    ListPopup menu = ListPopup.create(activity, adapter);
    menu.setOnItemClickListener((a, v, pos) -> {
        LinearAdapter.MenuItem item = ((LinearAdapter) a).getItem(pos);
        @StringRes int stringId = 0;
        if (item instanceof LinearAdapter.Item) {
            stringId = ((LinearAdapter.Item) a.getItem(pos)).stringId;
        }
        switch(stringId) {
            case R.string.menu_widget_add:
                TBApplication.widgetManager(activity).showSelectWidget(activity);
                break;
            case R.string.menu_widget_configure:
                {
                    ListPopup configWidgetPopup = TBApplication.widgetManager(activity).getWidgetListPopup(R.string.menu_widget_configure);
                    configWidgetPopup.setOnItemClickListener((a1, v1, pos1) -> {
                        Object item1 = a1.getItem(pos1);
                        if (item1 instanceof WidgetPopupItem) {
                            AppWidgetHostView widgetView = mLayout.getWidget(((WidgetPopupItem) item1).appWidgetId);
                            if (widgetView == null)
                                return;
                            ListPopup popup = getConfigPopup((WidgetView) widgetView);
                            TBApplication.getApplication(mLayout.getContext()).registerPopup(popup);
                            popup.show(widgetView, 0.f);
                        } else if (item1 instanceof PlaceholderPopupItem) {
                            PlaceholderWidgetRecord placeholder = ((PlaceholderPopupItem) item1).placeholder;
                            View placeholderView = mLayout.getPlaceholder(placeholder.provider);
                            if (placeholderView != null)
                                placeholderView.performClick();
                        }
                    });
                    TBApplication.getApplication(activity).registerPopup(configWidgetPopup);
                    configWidgetPopup.showCenter(activity.getWindow().getDecorView());
                    break;
                }
            case R.string.menu_widget_remove:
                showRemoveWidgetPopup();
                break;
            case R.string.menu_popup_launcher_settings:
                // TBApplication.behaviour(v.getContext()).beforeLaunchOccurred();
                mLayout.postDelayed(() -> {
                    Context c = mLayout.getContext();
                    c.startActivity(new Intent(c, SettingsActivity.class));
                // TBApplication.behaviour(c).afterLaunchOccurred();
                }, Behaviour.LAUNCH_DELAY);
                break;
        }
    });
    return menu;
}
Also used : RequiresApi(androidx.annotation.RequiresApi) Context(android.content.Context) PointF(android.graphics.PointF) PlaceholderWidgetRecord(rocks.tbog.tblauncher.db.PlaceholderWidgetRecord) Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) ResourcesCompat(androidx.core.content.res.ResourcesCompat) PackageManager(android.content.pm.PackageManager) LinearAdapter(rocks.tbog.tblauncher.ui.LinearAdapter) AppWidgetHost(android.appwidget.AppWidgetHost) NonNull(androidx.annotation.NonNull) ImageView(android.widget.ImageView) Intent(android.content.Intent) DrawableUtils(rocks.tbog.tblauncher.drawable.DrawableUtils) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) Drawable(android.graphics.drawable.Drawable) DBHelper(rocks.tbog.tblauncher.db.DBHelper) ArrayList(java.util.ArrayList) WidgetView(rocks.tbog.tblauncher.ui.WidgetView) ListPopup(rocks.tbog.tblauncher.ui.ListPopup) AppWidgetHostView(android.appwidget.AppWidgetHostView) Toast(android.widget.Toast) ContextThemeWrapper(android.view.ContextThemeWrapper) View(android.view.View) WidgetLayout(rocks.tbog.tblauncher.ui.WidgetLayout) Build(android.os.Build) Log(android.util.Log) ArrayMap(android.util.ArrayMap) WidgetRecord(rocks.tbog.tblauncher.db.WidgetRecord) Iterator(java.util.Iterator) ComponentName(android.content.ComponentName) LayoutInflater(android.view.LayoutInflater) ViewGroup(android.view.ViewGroup) AppWidgetManager(android.appwidget.AppWidgetManager) StringRes(androidx.annotation.StringRes) TextView(android.widget.TextView) Nullable(androidx.annotation.Nullable) ListAdapter(android.widget.ListAdapter) ActivityNotFoundException(android.content.ActivityNotFoundException) Utilities(rocks.tbog.tblauncher.utils.Utilities) Activity(android.app.Activity) DebugInfo(rocks.tbog.tblauncher.utils.DebugInfo) LinearAdapterPlus(rocks.tbog.tblauncher.ui.LinearAdapterPlus) Context(android.content.Context) PlaceholderWidgetRecord(rocks.tbog.tblauncher.db.PlaceholderWidgetRecord) StringRes(androidx.annotation.StringRes) ListPopup(rocks.tbog.tblauncher.ui.ListPopup) WidgetView(rocks.tbog.tblauncher.ui.WidgetView) Intent(android.content.Intent) ImageView(android.widget.ImageView) WidgetView(rocks.tbog.tblauncher.ui.WidgetView) AppWidgetHostView(android.appwidget.AppWidgetHostView) View(android.view.View) TextView(android.widget.TextView) LinearAdapter(rocks.tbog.tblauncher.ui.LinearAdapter) AppWidgetHostView(android.appwidget.AppWidgetHostView)

Example 2 with WidgetView

use of rocks.tbog.tblauncher.ui.WidgetView in project TBLauncher by TBog.

the class WidgetManager method addWidgetToLayout.

private void addWidgetToLayout(AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo, WidgetRecord rec) {
    View placeholder = mLayout.getPlaceholder(appWidgetInfo.provider);
    WidgetLayout.LayoutParams params = null;
    if (placeholder != null)
        params = (WidgetLayout.LayoutParams) placeholder.getLayoutParams();
    if (params == null) {
        params = new WidgetLayout.LayoutParams(rec.width, rec.height);
        params.leftMargin = rec.left;
        params.topMargin = rec.top;
        params.screenPage = rec.screen;
        params.placement = WidgetLayout.LayoutParams.Placement.MARGIN_TL_AS_POSITION;
    }
    hostView.setMinimumWidth(appWidgetInfo.minWidth);
    hostView.setMinimumHeight(appWidgetInfo.minHeight);
    hostView.setAppWidget(rec.appWidgetId, appWidgetInfo);
    hostView.updateAppWidgetSize(null, rec.width, rec.height, rec.width, rec.height);
    hostView.setOnLongClickListener(v -> {
        if (v instanceof WidgetView) {
            ListPopup menu = getConfigPopup((WidgetView) v);
            TBApplication.getApplication(v.getContext()).registerPopup(menu);
            menu.show(v, 0.f);
            return true;
        }
        return false;
    });
    // replace placeholder (if it exists) with the widget
    {
        int insertPosition = mLayout.indexOfChild(placeholder);
        if (insertPosition != -1)
            mLayout.removeViewAt(insertPosition);
        mLayout.addView(hostView, insertPosition, params);
    }
    Context context = mLayout.getContext();
    // remove from `mPlaceholders`
    {
        for (Iterator<PlaceholderWidgetRecord> iterator = mPlaceholders.iterator(); iterator.hasNext(); ) {
            PlaceholderWidgetRecord placeholderWidget = iterator.next();
            if (placeholderWidget.provider.equals(appWidgetInfo.provider)) {
                DBHelper.removeWidgetPlaceholder(context, INVALID_WIDGET_ID, placeholderWidget.provider.flattenToString());
                iterator.remove();
            }
        }
    }
}
Also used : WidgetLayout(rocks.tbog.tblauncher.ui.WidgetLayout) Context(android.content.Context) PlaceholderWidgetRecord(rocks.tbog.tblauncher.db.PlaceholderWidgetRecord) ListPopup(rocks.tbog.tblauncher.ui.ListPopup) Iterator(java.util.Iterator) WidgetView(rocks.tbog.tblauncher.ui.WidgetView) ImageView(android.widget.ImageView) WidgetView(rocks.tbog.tblauncher.ui.WidgetView) AppWidgetHostView(android.appwidget.AppWidgetHostView) View(android.view.View) TextView(android.widget.TextView)

Aggregations

AppWidgetHostView (android.appwidget.AppWidgetHostView)2 Context (android.content.Context)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 Iterator (java.util.Iterator)2 PlaceholderWidgetRecord (rocks.tbog.tblauncher.db.PlaceholderWidgetRecord)2 ListPopup (rocks.tbog.tblauncher.ui.ListPopup)2 WidgetLayout (rocks.tbog.tblauncher.ui.WidgetLayout)2 Activity (android.app.Activity)1 AppWidgetHost (android.appwidget.AppWidgetHost)1 AppWidgetManager (android.appwidget.AppWidgetManager)1 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 PointF (android.graphics.PointF)1 Drawable (android.graphics.drawable.Drawable)1 Build (android.os.Build)1