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);
}
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);
}
}
}
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);
}
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;
}
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);
}
}
Aggregations