Skip to content

Commit

Permalink
add search widget
Browse files Browse the repository at this point in the history
  • Loading branch information
saulhdev committed Jul 25, 2020
1 parent 3a45dcd commit 080cb0b
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 24 deletions.
1 change: 1 addition & 0 deletions Omega/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<!-- PREFERENCES DOCK -->
<string name="title__dock_hide">Dock Hide</string>
<string name="title__dock_hide_labels">Hide Labels</string>
<string name="title__dock_colored_icons">Colored icons</string>
<string name="title__dock_search_bar">Show Search Bar</string>

<!-- PREFERENCES DRAWER -->
Expand Down
35 changes: 35 additions & 0 deletions Omega/res/xml/custom_widgets.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2020 Omega Launcher
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->

<widgets xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:launcher="http://schemas.android.com/apk/res-auto">

<widget
android:icon="@mipmap/ic_launcher_home"
android:initialLayout="@layout/search_widget"
android:label="@string/cat_search_bar"
android:resizeMode="horizontal|vertical"
launcher:customizeHasPreview="false"
launcher:customizeScreen="@xml/omega_preferences_search"
launcher:customizeTitle="@string/cat_search_bar"
launcher:noPadding="true"
launcher:numColumns="5"
launcher:numMinColumns="3"
launcher:numMinRows="1"
launcher:numRows="1"
launcher:providerId="2" />
</widgets>
18 changes: 16 additions & 2 deletions Omega/res/xml/omega_preferences_dock.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.saggitt.omega.preferences.StyledSwitchPreference
android:defaultValue="true"
android:key="pref_key__hide_hotseat"
android:defaultValue="false"
android:key="pref_hideHotseat"
android:persistent="true"
android:title="@string/title__dock_hide"
app:iconSpaceReserved="false" />
Expand All @@ -30,4 +30,18 @@
android:persistent="true"
android:title="@string/title__dock_hide_labels"
app:iconSpaceReserved="false" />
<!--
<com.saggitt.omega.preferences.StyledSwitchPreference
android:key="pref_dockSearchBar"
android:title="@string/title__dock_search_bar"
android:defaultValue="false"
android:persistent="true"
app:iconSpaceReserved="false" />
<com.saggitt.omega.preferences.StyledSwitchPreference
android:key="pref_dockColoredGoogle"
android:title="@string/title__dock_colored_icons"
android:dependency="pref_dockSearchBar"
android:defaultValue="true"
android:persistent="true" /> -->
</PreferenceScreen>
4 changes: 3 additions & 1 deletion Omega/src/com/saggitt/omega/OmegaPreferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ class OmegaPreferences(val context: Context) : SharedPreferences.OnSharedPrefere
val homeLabelRows get() = if (homeMultilineLabel) 2 else 1

/* --DOCK-- */
var dockHide by BooleanPref("pref_key__hide_hotseat", false, recreate)
var dockHide by BooleanPref("pref_hideHotseat", false, recreate)
val dockTextScale by FloatPref("pref_dockTextScale", -1f, restart)
private val dockMultilineLabel by BooleanPref("pref_dockIconLabelsInTwoLines", false, recreate)
val dockLabelRows get() = if (dockMultilineLabel) 2 else 1
val hideDockLabels by BooleanPref("pref_hideDockLabels", true, restart)
var dockScale by FloatPref("pref_dockScale", -1f, recreate)
var dockSearchBarPref by BooleanPref("pref_dockSearchBar", false, recreate)
inline val dockSearchBar get() = !dockHide && dockSearchBarPref

/* --THEME-- */
var launcherTheme by StringIntPref("pref_launcherTheme", 1) { ThemeManager.getInstance(context).updateTheme() }
Expand Down
21 changes: 11 additions & 10 deletions Omega/src/com/saggitt/omega/qsb/HotseatQsbWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public HotseatQsbWidget(Context context, AttributeSet attributeSet, int i) {
static void a(HotseatQsbWidget hotseatQsbWidget) {
if (hotseatQsbWidget.mIsGoogleColored != hotseatQsbWidget.isGoogleColored()) {
hotseatQsbWidget.mIsGoogleColored = !hotseatQsbWidget.mIsGoogleColored;
hotseatQsbWidget.onChangeListener();
hotseatQsbWidget.onChange();
}
}

Expand Down Expand Up @@ -105,6 +105,9 @@ public void setWidgetMode(boolean widgetMode) {
}

protected void onAttachedToWindow() {
Utilities.getOmegaPrefs(getContext())
.addOnPreferenceChangeListener(this, KEY_DOCK_COLORED_GOOGLE, KEY_DOCK_SEARCHBAR);

dW();
super.onAttachedToWindow();
Ds.addListener(this);
Expand All @@ -114,17 +117,20 @@ protected void onAttachedToWindow() {

protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
Utilities.getOmegaPrefs(getContext())
.removeOnPreferenceChangeListener(this, KEY_DOCK_COLORED_GOOGLE,
KEY_DOCK_SEARCHBAR);
Ds.removeListener(this);
}

@Override
public void onValueChanged(@NotNull String key, @NotNull OmegaPreferences prefs, boolean force) {
if (key.equals(KEY_DOCK_COLORED_GOOGLE)) {
mIsGoogleColored = isGoogleColored();
onChangeListener();
onChange();
} else if (!widgetMode && (key.equals(KEY_DOCK_SEARCHBAR) || key.equals(KEY_DOCK_HIDE))) {
//boolean visible = prefs.getDockSearchBar() && !prefs.getDockHide();
//setVisibility(visible ? View.VISIBLE : View.GONE);
boolean visible = prefs.getDockSearchBar() && !prefs.getDockHide();
setVisibility(visible ? View.VISIBLE : View.GONE);
}
}

Expand All @@ -138,7 +144,7 @@ protected Drawable getMicIcon() {
return getMicIcon(mIsGoogleColored);
}

public final void onChangeListener() {
public final void onChange() {
removeAllViews();
setColors();
dW();
Expand Down Expand Up @@ -324,9 +330,4 @@ public void setAlpha(float alpha) {
super.setAlpha(alpha);
mLauncher.findViewById(R.id.scrim_view).invalidate();
}

@Override
public void onChange() {

}
}
6 changes: 6 additions & 0 deletions Omega/src/com/saggitt/omega/qsb/QsbConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ private QsbConfiguration(Context context) {

}

private void notifyListeners() {
for (QsbChangeListener listener : mListeners) {
listener.onChange();
}
}

public static QsbConfiguration getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new QsbConfiguration(context.getApplicationContext());
Expand Down
149 changes: 149 additions & 0 deletions Omega/src/com/saggitt/omega/widget/CustomWidgetParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2020 Omega Launcher
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.saggitt.omega.widget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.os.Parcel;
import android.os.Process;
import android.util.SparseArray;
import android.util.Xml;

import com.android.launcher3.LauncherAppWidgetInfo;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.R;
import com.android.launcher3.widget.custom.CustomAppWidgetProviderInfo;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.android.launcher3.LauncherAppWidgetProviderInfo.CLS_CUSTOM_WIDGET_PREFIX;

/**
* Utility class to parse {@ink CustomAppWidgetProviderInfo} definitions from xml
*/
public class CustomWidgetParser {

private static List<LauncherAppWidgetProviderInfo> sCustomWidgets;
private static SparseArray<ComponentName> sWidgetsIdMap;

public static List<LauncherAppWidgetProviderInfo> getCustomWidgets(Context context) {
if (sCustomWidgets == null) {
// Synchronization not needed as it it safe to load multiple times
parseCustomWidgets(context);
}

return sCustomWidgets;
}

public static int getWidgetIdForCustomProvider(Context context, ComponentName provider) {
if (sWidgetsIdMap == null) {
parseCustomWidgets(context);
}
int index = sWidgetsIdMap.indexOfValue(provider);
if (index >= 0) {
return LauncherAppWidgetInfo.CUSTOM_WIDGET_ID - sWidgetsIdMap.keyAt(index);
} else {
return AppWidgetManager.INVALID_APPWIDGET_ID;
}
}

public static LauncherAppWidgetProviderInfo getWidgetProvider(Context context, int widgetId) {
if (sWidgetsIdMap == null || sCustomWidgets == null) {
parseCustomWidgets(context);
}
ComponentName cn = sWidgetsIdMap.get(LauncherAppWidgetInfo.CUSTOM_WIDGET_ID - widgetId);
for (LauncherAppWidgetProviderInfo info : sCustomWidgets) {
if (info.provider.equals(cn)) {
return info;
}
}
return null;
}

private static void parseCustomWidgets(Context context) {
ArrayList<LauncherAppWidgetProviderInfo> widgets = new ArrayList<>();
SparseArray<ComponentName> idMap = new SparseArray<>();

List<AppWidgetProviderInfo> providers = AppWidgetManager.getInstance(context)
.getInstalledProvidersForProfile(Process.myUserHandle());
if (providers.isEmpty()) {
sCustomWidgets = widgets;
sWidgetsIdMap = idMap;
return;
}

Parcel parcel = Parcel.obtain();
providers.get(0).writeToParcel(parcel, 0);

try (XmlResourceParser parser = context.getResources().getXml(R.xml.custom_widgets)) {
final int depth = parser.getDepth();
int type;

while (((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if ((type == XmlPullParser.START_TAG) && "widget".equals(parser.getName())) {
TypedArray a = context.obtainStyledAttributes(
Xml.asAttributeSet(parser), R.styleable.CustomAppWidgetProviderInfo);

parcel.setDataPosition(0);
CustomAppWidgetProviderInfo info = newInfo(a, parcel, context);
widgets.add(info);
a.recycle();

idMap.put(info.providerId, info.provider);
}
}
} catch (IOException | XmlPullParserException e) {
throw new RuntimeException(e);
}
parcel.recycle();
sCustomWidgets = widgets;
sWidgetsIdMap = idMap;
}

private static CustomAppWidgetProviderInfo newInfo(TypedArray a, Parcel parcel, Context context) {
int providerId = a.getInt(R.styleable.CustomAppWidgetProviderInfo_providerId, 0);
boolean noPadding = a.getBoolean(R.styleable.CustomAppWidgetProviderInfo_noPadding, false);
CustomAppWidgetProviderInfo info = new CustomAppWidgetProviderInfo(parcel, false, providerId, noPadding);
info.provider = new ComponentName(context.getPackageName(), CLS_CUSTOM_WIDGET_PREFIX + providerId);
info.customizeTitle = a.getResourceId(R.styleable.CustomAppWidgetProviderInfo_customizeTitle, 0);
info.customizeScreen = a.getResourceId(R.styleable.CustomAppWidgetProviderInfo_customizeScreen, 0);
info.customizeHasPreview = a.getBoolean(R.styleable.CustomAppWidgetProviderInfo_customizeHasPreview, false);

info.label = a.getString(R.styleable.CustomAppWidgetProviderInfo_android_label);
info.initialLayout = a.getResourceId(R.styleable.CustomAppWidgetProviderInfo_android_initialLayout, 0);
info.icon = a.getResourceId(R.styleable.CustomAppWidgetProviderInfo_android_icon, 0);
info.previewImage = a.getResourceId(R.styleable.CustomAppWidgetProviderInfo_android_previewImage, 0);
info.resizeMode = a.getInt(R.styleable.CustomAppWidgetProviderInfo_android_resizeMode, 0);

info.spanX = a.getInt(R.styleable.CustomAppWidgetProviderInfo_numColumns, 1);
info.spanY = a.getInt(R.styleable.CustomAppWidgetProviderInfo_numRows, 1);
info.minSpanX = a.getInt(R.styleable.CustomAppWidgetProviderInfo_numMinColumns, 1);
info.minSpanY = a.getInt(R.styleable.CustomAppWidgetProviderInfo_numMinRows, 1);
return info;
}
}
6 changes: 3 additions & 3 deletions res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<dimen name="dynamic_grid_cell_padding_x">8dp</dimen>

<!-- Hotseat -->
<dimen name="dynamic_grid_hotseat_top_padding">4dp</dimen>
<dimen name="dynamic_grid_hotseat_bottom_padding">2dp</dimen>
<dimen name="dynamic_grid_hotseat_top_padding">8dp</dimen>
<dimen name="dynamic_grid_hotseat_bottom_padding">62dp</dimen>
<!-- Extra bottom padding for non-tall devices. -->
<dimen name="dynamic_grid_hotseat_bottom_non_tall_padding">0dp</dimen>
<dimen name="dynamic_grid_hotseat_extra_vertical_size">34dp</dimen>
Expand All @@ -41,7 +41,7 @@
<dimen name="vertical_drag_handle_size">24dp</dimen>
<dimen name="vertical_drag_handle_overlap_workspace">0dp</dimen>

<!-- Drop target bar -->
<!-- Drop target bar -->
<dimen name="dynamic_grid_drop_target_size">48dp</dimen>
<dimen name="drop_target_vertical_gap">20dp</dimen>

Expand Down
Loading

0 comments on commit 080cb0b

Please sign in to comment.