Search in sources :

Example 1 with PlaceholderWidgetRecord

use of rocks.tbog.tblauncher.db.PlaceholderWidgetRecord in project TBLauncher by TBog.

the class WidgetManager method addPlaceholderToLayout.

private void addPlaceholderToLayout(@NonNull PlaceholderWidgetRecord rec) {
    final Context context = mLayout.getContext();
    Drawable preview = DrawableUtils.getBitmapDrawable(context, rec.preview);
    View placeholder = LayoutInflater.from(context).inflate(R.layout.widget_placeholder, mLayout, false);
    {
        WidgetLayout.LayoutParams 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;
        placeholder.setLayoutParams(params);
    }
    {
        TextView text = placeholder.findViewById(android.R.id.text1);
        text.setText(context.getString(R.string.widget_placeholder, rec.name));
    }
    {
        ImageView icon = placeholder.findViewById(android.R.id.icon);
        icon.setImageDrawable(preview);
    }
    final ComponentName provider = rec.provider != null ? rec.provider : new ComponentName("null", "null");
    placeholder.setOnClickListener(v -> {
        Activity activity = Utilities.getActivity(v);
        if (activity == null)
            return;
        int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
        boolean hasPermission;
        try {
            hasPermission = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, provider);
        } catch (Throwable ignored) {
            hasPermission = false;
        }
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, provider);
        if (hasPermission) {
            configureWidget(activity, intent);
        } else {
            activity.startActivityForResult(intent, REQUEST_PICK_APPWIDGET);
        }
    // Toast.makeText(activity, provider.flattenToString(), Toast.LENGTH_SHORT).show();
    });
    placeholder.setOnLongClickListener(placeholderView -> {
        Activity activity = Utilities.getActivity(placeholderView);
        if (activity == null)
            return false;
        TextView text = placeholderView.findViewById(android.R.id.text1);
        final CharSequence placeholderName = text != null ? text.getText() : "null";
        ContextThemeWrapper ctxDialog = new ContextThemeWrapper(activity, R.style.TitleDialogTheme);
        new AlertDialog.Builder(ctxDialog).setTitle(R.string.widget_placeholder_remove).setMessage(placeholderName + "\n" + provider.flattenToShortString()).setPositiveButton(android.R.string.ok, (dialog, which) -> {
            mLayout.removeView(placeholderView);
            for (Iterator<PlaceholderWidgetRecord> iterator = mPlaceholders.iterator(); iterator.hasNext(); ) {
                PlaceholderWidgetRecord placeholderWidgetRecord = iterator.next();
                if (provider.equals(placeholderWidgetRecord.provider)) {
                    DBHelper.removeWidgetPlaceholder(activity, INVALID_WIDGET_ID, placeholderWidgetRecord.provider.flattenToString());
                    iterator.remove();
                }
            }
            dialog.dismiss();
        }).setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss()).show();
        return true;
    });
    mLayout.addPlaceholder(placeholder, provider);
}
Also used : Context(android.content.Context) WidgetLayout(rocks.tbog.tblauncher.ui.WidgetLayout) AlertDialog(androidx.appcompat.app.AlertDialog) 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) PlaceholderWidgetRecord(rocks.tbog.tblauncher.db.PlaceholderWidgetRecord) Drawable(android.graphics.drawable.Drawable) Activity(android.app.Activity) 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) ContextThemeWrapper(android.view.ContextThemeWrapper) TextView(android.widget.TextView) ComponentName(android.content.ComponentName) ImageView(android.widget.ImageView)

Example 2 with PlaceholderWidgetRecord

use of rocks.tbog.tblauncher.db.PlaceholderWidgetRecord in project TBLauncher by TBog.

the class WidgetManager method loadFromDB.

private void loadFromDB(Context context) {
    ArrayList<WidgetRecord> widgets = DBHelper.getWidgets(context);
    mWidgets.clear();
    mPlaceholders.clear();
    // we expect no placeholders
    mWidgets.ensureCapacity(widgets.size());
    for (WidgetRecord record : widgets) {
        if (record instanceof PlaceholderWidgetRecord) {
            mPlaceholders.add((PlaceholderWidgetRecord) record);
        } else {
            mWidgets.put(record.appWidgetId, record);
        }
    }
}
Also used : PlaceholderWidgetRecord(rocks.tbog.tblauncher.db.PlaceholderWidgetRecord) PlaceholderWidgetRecord(rocks.tbog.tblauncher.db.PlaceholderWidgetRecord) WidgetRecord(rocks.tbog.tblauncher.db.WidgetRecord)

Example 3 with PlaceholderWidgetRecord

use of rocks.tbog.tblauncher.db.PlaceholderWidgetRecord in project TBLauncher by TBog.

the class WidgetManager method showRemoveWidgetPopup.

public void showRemoveWidgetPopup() {
    Context context = mLayout.getContext();
    ListPopup removeWidgetPopup = TBApplication.widgetManager(context).getWidgetListPopup(R.string.menu_widget_remove);
    removeWidgetPopup.setOnItemClickListener((a1, v1, pos1) -> {
        Object item1 = a1.getItem(pos1);
        if (item1 instanceof WidgetPopupItem) {
            removeWidget(((WidgetPopupItem) item1).appWidgetId);
        } else if (item1 instanceof PlaceholderPopupItem) {
            PlaceholderWidgetRecord placeholder = ((PlaceholderPopupItem) item1).placeholder;
            View placeholderView = mLayout.getPlaceholder(placeholder.provider);
            if (placeholderView != null)
                placeholderView.performLongClick();
        }
    });
    TBApplication.getApplication(context).registerPopup(removeWidgetPopup);
    removeWidgetPopup.showCenter(mLayout);
}
Also used : Context(android.content.Context) PlaceholderWidgetRecord(rocks.tbog.tblauncher.db.PlaceholderWidgetRecord) ListPopup(rocks.tbog.tblauncher.ui.ListPopup) ImageView(android.widget.ImageView) WidgetView(rocks.tbog.tblauncher.ui.WidgetView) AppWidgetHostView(android.appwidget.AppWidgetHostView) View(android.view.View) TextView(android.widget.TextView)

Example 4 with PlaceholderWidgetRecord

use of rocks.tbog.tblauncher.db.PlaceholderWidgetRecord 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 5 with PlaceholderWidgetRecord

use of rocks.tbog.tblauncher.db.PlaceholderWidgetRecord in project TBLauncher by TBog.

the class WidgetManager method onAfterRestoreFromBackup.

public void onAfterRestoreFromBackup(boolean clearExtra) {
    if (clearExtra) {
        // remove all widgets not found in mLayout
        int[] appWidgetIds = new int[mWidgets.size()];
        // Arrays.fill(appWidgetIds, INVALID_WIDGET_ID);
        // copy widget ids we may need to remove
        {
            int idx = 0;
            for (WidgetRecord rec : mWidgets.values()) appWidgetIds[idx++] = rec.appWidgetId;
        }
        for (int appWidgetId : appWidgetIds) {
            AppWidgetHostView widgetHostView = mLayout.getWidget(appWidgetId);
            if (widgetHostView != null)
                removeWidget(widgetHostView);
        }
    } else {
        // restore widgets from mWidgets that are not in mLayout yet
        for (WidgetRecord rec : mWidgets.values()) {
            if (mLayout.getWidget(rec.appWidgetId) == null)
                restoreWidget(rec);
        }
    }
    Context ctx = mLayout.getContext();
    for (WidgetRecord rec : mWidgets.values()) {
        DBHelper.setWidgetProperties(ctx, rec);
    }
    // remove all placeholders
    DBHelper.removeWidget(ctx, INVALID_WIDGET_ID);
    // add back all placeholders
    for (PlaceholderWidgetRecord placeholder : mPlaceholders) {
        DBHelper.addWidget(ctx, placeholder);
    }
}
Also used : Context(android.content.Context) AppWidgetHostView(android.appwidget.AppWidgetHostView) PlaceholderWidgetRecord(rocks.tbog.tblauncher.db.PlaceholderWidgetRecord) PlaceholderWidgetRecord(rocks.tbog.tblauncher.db.PlaceholderWidgetRecord) WidgetRecord(rocks.tbog.tblauncher.db.WidgetRecord)

Aggregations

PlaceholderWidgetRecord (rocks.tbog.tblauncher.db.PlaceholderWidgetRecord)9 WidgetRecord (rocks.tbog.tblauncher.db.WidgetRecord)7 Context (android.content.Context)6 AppWidgetHostView (android.appwidget.AppWidgetHostView)5 View (android.view.View)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 ListPopup (rocks.tbog.tblauncher.ui.ListPopup)4 WidgetView (rocks.tbog.tblauncher.ui.WidgetView)4 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)3 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 LinearAdapter (rocks.tbog.tblauncher.ui.LinearAdapter)3 LinearAdapterPlus (rocks.tbog.tblauncher.ui.LinearAdapterPlus)3 Activity (android.app.Activity)2 AppWidgetHost (android.appwidget.AppWidgetHost)2 AppWidgetManager (android.appwidget.AppWidgetManager)2 ActivityNotFoundException (android.content.ActivityNotFoundException)2 ComponentName (android.content.ComponentName)2 Intent (android.content.Intent)2