Skip to content

Commit

Permalink
add search widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornaco committed May 17, 2024
1 parent dc72c73 commit ccff658
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 5 deletions.
13 changes: 12 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
android:theme="@style/AppThemeNeedRestart" />
<activity
android:name="now.fortuitous.thanos.power.SmartFreezeActivity"
android:exported="false" />
android:exported="true" />
<activity
android:name="now.fortuitous.thanos.power.ShortcutStubActivity"
android:excludeFromRecents="true"
Expand Down Expand Up @@ -372,6 +372,17 @@
android:authorities="${applicationId}.XposedService"
android:exported="true"
tools:ignore="ExportedContentProvider" />

<receiver
android:name="now.fortuitous.thanos.widget.SmartFreezeAppWidgetProvider"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/smart_freeze_widget_info" />
</receiver>
</application>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.fragment.app.Fragment
import com.elvishew.xlog.XLog
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.tabs.TabLayout
import github.tornaco.android.thanos.BaseFragment
Expand All @@ -49,6 +50,8 @@ class SmartFreezeActivity : ThemeActivity() {
private val viewModel: SmartFreezeBottomNavViewModel by viewModels()

companion object Starter {
const val EXTRA_EXPAND_SEARCH = "expand.search"

fun start(context: Context) {
ActivityUtils.startActivity(context, SmartFreezeActivity::class.java)
}
Expand All @@ -67,6 +70,16 @@ class SmartFreezeActivity : ThemeActivity() {
setContentView(binding.root)

inflateTabs()

binding.bottomNavigation.post {
val hasExpandSearchIntent = intent?.hasExtra(EXTRA_EXPAND_SEARCH) ?: false
XLog.i("SmartFreezeActivity hasExpandSearchIntent: $hasExpandSearchIntent")
if (hasExpandSearchIntent) {
val fragment = supportFragmentManager.findFragmentByTag("SMART_FREEZE_FRAGMENT")
XLog.i("SmartFreezeActivity fragment: $fragment")
(fragment as? SmartFreezeAppListFragment)?.showSearch()
}
}
}

private fun inflateTabs() {
Expand Down Expand Up @@ -94,7 +107,6 @@ class SmartFreezeActivity : ThemeActivity() {

binding.bottomNavigation.selectTab(binding.bottomNavigation.getTabAt(0))
binding.composeSort.setContent {

val darkTheme =
if (appTheme.shouldApplyDynamic) isSystemInDarkTheme() else !appTheme.isLight
ThanoxTheme(darkTheme) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import java.util.List;
import java.util.Objects;


import github.tornaco.android.thanos.BaseFragment;
import github.tornaco.android.thanos.R;
import github.tornaco.android.thanos.common.AppItemActionListener;
Expand Down Expand Up @@ -118,9 +117,9 @@ public static SmartFreezeAppListFragment newInstance(@Nullable String pkgSetId)
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = ActivitySmartFreezeAppsBinding.inflate(inflater, container, false);
createOptionsMenu();
setupView();
Bundle data = getArguments();
String pkgSetId = Objects.requireNonNull(data, "Missing arg: " + ARG_PKG_SET).getString(ARG_PKG_SET, null);
setupView();
setupViewModel(pkgSetId);
onSetupSorter(binding.sortChipContainer.sortChip);
return binding.getRoot();
Expand Down Expand Up @@ -184,6 +183,10 @@ public void onAppItemSwitchStateChange(AppInfo appInfo, boolean checked) {
});
}

public void showSearch() {
binding.searchView.showSearch(true);
}

@SuppressLint("RestrictedApi")
protected void onSetupSorter(Chip sorterAnchor) {
AppSort[] appSortArray = AppSort.values();
Expand Down Expand Up @@ -290,7 +293,6 @@ private void showItemPopMenu(@NonNull View anchor, @NonNull AppListModel model)
}



private void setupViewModel(String pkgSetId) {
viewModel = obtainViewModel(requireActivity());
viewModel.setPkgSetId(pkgSetId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package now.fortuitous.thanos.widget

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
import github.tornaco.android.thanos.R
import now.fortuitous.thanos.power.SmartFreezeActivity

class SmartFreezeAppWidgetProvider : AppWidgetProvider() {

override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
appWidgetIds.forEach { appWidgetId ->
val pendingIntent: PendingIntent = PendingIntent.getActivity(
/* context = */ context,
/* requestCode = */ 0,
/* intent = */ Intent(context, SmartFreezeActivity::class.java).apply {
putExtra(SmartFreezeActivity.EXTRA_EXPAND_SEARCH, true)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
},
/* flags = */ PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val views: RemoteViews = RemoteViews(
context.packageName,
R.layout.smart_freeze_appwidget
).apply {
setOnClickPendingIntent(R.id.search_layout, pendingIntent)
}

appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions android/app/src/main/res/drawable/app_widget_search_bg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<!-- 背景颜色 -->
<solid android:color="@color/app_widget_search_bg_color" />

<!-- 圆角半径 -->
<corners android:radius="20dp" />

<!-- 内边距 -->
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />

<!-- 边框 -->
<stroke
android:width="2dp"
android:color="@color/app_widget_search_stroke_color" />

</shape>
24 changes: 24 additions & 0 deletions android/app/src/main/res/layout/smart_freeze_appwidget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/search_layout"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="center"
android:background="@drawable/app_widget_search_bg"
android:padding="16dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_remix_search_line"
android:tint="@color/app_widget_search_icon_tint_color"
tools:ignore="UseAppTint" />

</LinearLayout>
</LinearLayout>
10 changes: 10 additions & 0 deletions android/app/src/main/res/layout/smart_freeze_appwidget_preview.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Preview" />
</LinearLayout>
6 changes: 6 additions & 0 deletions android/app/src/main/res/values-night-v31/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="app_widget_search_bg_color">#ea000000</color>
<color name="app_widget_search_stroke_color">#ea000000</color>
<color name="app_widget_search_icon_tint_color">#eaffffff</color>
</resources>
4 changes: 4 additions & 0 deletions android/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
<color name="nav_icon_guide">#ffb74d</color>
<color name="nav_icon_wakelock_remover">#f06292</color>
<color name="nav_icon_launch_other_app">#a1887f</color>

<color name="app_widget_search_bg_color">#eaffffff</color>
<color name="app_widget_search_stroke_color">#eaffffff</color>
<color name="app_widget_search_icon_tint_color">#66000000</color>
</resources>
17 changes: 17 additions & 0 deletions android/app/src/main/res/xml/smart_freeze_widget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:description="@string/feature_title_smart_app_freeze"
android:initialLayout="@layout/smart_freeze_appwidget"
android:maxResizeWidth="250dp"
android:maxResizeHeight="80dp"
android:minWidth="40dp"
android:minHeight="40dp"
android:previewImage="@drawable/search_preview"
android:resizeMode="horizontal|vertical"
android:targetCellWidth="1"
android:targetCellHeight="1"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen"
android:widgetFeatures="reconfigurable|configuration_optional"
tools:ignore="UnusedAttribute" />

0 comments on commit ccff658

Please sign in to comment.