未禁止前
Launcher3/res/layout/launcher.xml
launcher.xml
显示移除、卸载的布局
<include
android:id="@+id/drop_target_bar"
layout="@layout/drop_target_bar" />
Launcher3/res/layout/drop_target_bar.xml
drop_target_bar.xml
<com.android.launcher3.DropTargetBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/dynamic_grid_drop_target_size"
android:layout_gravity="center_horizontal|top"
android:focusable="false"
android:alpha="0"
android:theme="@style/HomeScreenElementTheme"
android:visibility="invisible">
<!-- Delete target -->
<com.android.launcher3.DeleteDropTarget
android:id="@+id/delete_target_text"
style="@style/DropTargetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/remove_drop_target_label" />
<!-- Uninstall target -->
<com.android.launcher3.SecondaryDropTarget
android:id="@+id/uninstall_target_text"
style="@style/DropTargetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/uninstall_drop_target_label" />
</com.android.launcher3.DropTargetBar>
Launcher3/src/com/android/launcher3/SecondaryDropTarget.java
Launcher3/src/com/android/launcher3/ButtonDropTarget.java
SecondaryDropTarget继承自ButtonDropTarget
ButtonDropTarget
//监听开始拖拽后是否显示卸载按钮
@Override
public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
if (options.isKeyboardDrag) {
mActive = false;
} else {
setupItemInfo(dragObject.dragInfo);
//这里判断是否要显示
mActive = supportsDrop(dragObject.dragInfo);
}
setVisibility(mActive ? View.VISIBLE : View.GONE);
mAccessibleDrag = options.isAccessibleDrag;
setOnClickListener(mAccessibleDrag ? this : null);
}
//SecondaryDropTarget负责具体实现
protected abstract boolean supportsDrop(ItemInfo info);
SecondaryDropTarget
@Override
protected boolean supportsDrop(ItemInfo info) {
return getButtonType(info, getViewUnderDrag(info)) != INVALID;
}
private int getButtonType(ItemInfo info, View view) {
//这里测试APP名可以
if(null != info.title && "My Application".equals(info.title.toString())){
return INVALID;
}
if (view instanceof AppWidgetHostView) {
if (getReconfigurableWidgetId(view) != INVALID_APPWIDGET_ID) {
return RECONFIGURE;
}
return INVALID;
} else if (info.isPredictedItem()) {
return DISMISS_PREDICTION;
}
Boolean uninstallDisabled = mUninstallDisabledCache.get(info.user);
if (uninstallDisabled == null) {
UserManager userManager =
(UserManager) getContext().getSystemService(Context.USER_SERVICE);
Bundle restrictions = userManager.getUserRestrictions(info.user);
uninstallDisabled = restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
|| restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false);
mUninstallDisabledCache.put(info.user, uninstallDisabled);
}
// Cancel any pending alarm and set cache expiry after some time
mCacheExpireAlarm.setAlarm(CACHE_EXPIRE_TIMEOUT);
mCacheExpireAlarm.setOnAlarmListener(this);
if (uninstallDisabled) {
return INVALID;
}
if (info instanceof ItemInfoWithIcon) {
ItemInfoWithIcon iconInfo = (ItemInfoWithIcon) info;
if ((iconInfo.runtimeStatusFlags & FLAG_SYSTEM_MASK) != 0
&& (iconInfo.runtimeStatusFlags & FLAG_SYSTEM_NO) == 0) {
return INVALID;
}
}
if (getUninstallTarget(info) == null) {
return INVALID;
}
return UNINSTALL;
}
禁止卸载后效果