-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
334 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
Omega/src/com/saggitt/omega/widget/CustomWidgetParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.