-
Notifications
You must be signed in to change notification settings - Fork 24
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
1 parent
9e9b626
commit 99c00e5
Showing
14 changed files
with
331 additions
and
17 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
128 changes: 128 additions & 0 deletions
128
app/src/main/java/io/github/saeeddev94/xray/activity/ExcludeActivity.kt
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,128 @@ | ||
package io.github.saeeddev94.xray.activity | ||
|
||
import android.Manifest | ||
import android.annotation.SuppressLint | ||
import android.content.pm.PackageManager | ||
import android.os.Bundle | ||
import android.view.Menu | ||
import android.view.MenuItem | ||
import android.view.View | ||
import android.widget.ImageView | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.appcompat.widget.SearchView | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
import io.github.saeeddev94.xray.R | ||
import io.github.saeeddev94.xray.Settings | ||
import io.github.saeeddev94.xray.adapter.ExcludeAdapter | ||
import io.github.saeeddev94.xray.databinding.ActivityExcludeBinding | ||
import io.github.saeeddev94.xray.dto.AppList | ||
|
||
class ExcludeActivity : AppCompatActivity() { | ||
|
||
private lateinit var binding: ActivityExcludeBinding | ||
private lateinit var appsList: RecyclerView | ||
private lateinit var excludeAdapter: ExcludeAdapter | ||
private lateinit var apps: ArrayList<AppList> | ||
private lateinit var filtered: MutableList<AppList> | ||
private lateinit var excludedApps: MutableSet<String> | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
title = "" | ||
binding = ActivityExcludeBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
setSupportActionBar(binding.toolbar) | ||
supportActionBar?.setDisplayHomeAsUpEnabled(true) | ||
|
||
binding.search.focusable = View.NOT_FOCUSABLE | ||
binding.search.setOnQueryTextListener(object : SearchView.OnQueryTextListener { | ||
override fun onQueryTextChange(newText: String?): Boolean { | ||
search(newText) | ||
return false | ||
} | ||
|
||
override fun onQueryTextSubmit(query: String?): Boolean { | ||
binding.search.clearFocus() | ||
search(query) | ||
return false | ||
} | ||
}) | ||
binding.search.findViewById<ImageView>(androidx.appcompat.R.id.search_close_btn) | ||
?.setOnClickListener { | ||
binding.search.setQuery("", false) | ||
binding.search.clearFocus() | ||
} | ||
|
||
getApps() | ||
} | ||
|
||
override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||
menuInflater.inflate(R.menu.menu_exclude, menu) | ||
return true | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
when (item.itemId) { | ||
R.id.saveExcludedApps -> saveExcludedApps() | ||
else -> finish() | ||
} | ||
return true | ||
} | ||
|
||
@SuppressLint("NotifyDataSetChanged") | ||
private fun search(query: String?) { | ||
val keyword = query?.trim()?.lowercase() ?: "" | ||
if (keyword.isEmpty()) { | ||
if (apps.size > filtered.size) { | ||
filtered.clear() | ||
filtered.addAll(apps.toMutableList()) | ||
excludeAdapter.notifyDataSetChanged() | ||
} | ||
return | ||
} | ||
val list = ArrayList<AppList>() | ||
apps.forEach { | ||
if (it.appName.lowercase().contains(keyword) || it.packageName.contains(keyword)) { | ||
list.add(it) | ||
} | ||
} | ||
filtered.clear() | ||
filtered.addAll(list.toMutableList()) | ||
excludeAdapter.notifyDataSetChanged() | ||
} | ||
|
||
private fun getApps() { | ||
Thread { | ||
val selected = ArrayList<AppList>() | ||
val unselected = ArrayList<AppList>() | ||
packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS).forEach { | ||
val permissions = it.requestedPermissions | ||
if (permissions == null || !permissions.contains(Manifest.permission.INTERNET)) return@forEach | ||
val appIcon = it.applicationInfo.loadIcon(packageManager) | ||
val appName = it.applicationInfo.loadLabel(packageManager).toString() | ||
val packageName = it.packageName | ||
val app = AppList(appIcon, appName, packageName) | ||
val isSelected = Settings.excludedApps.contains(packageName) | ||
if (isSelected) selected.add(app) else unselected.add(app) | ||
} | ||
runOnUiThread { | ||
apps = ArrayList(selected + unselected) | ||
filtered = apps.toMutableList() | ||
excludedApps = Settings.excludedApps.split("\n").toMutableSet() | ||
appsList = binding.appsList | ||
excludeAdapter = ExcludeAdapter(this@ExcludeActivity, filtered, excludedApps) | ||
appsList.adapter = excludeAdapter | ||
appsList.layoutManager = LinearLayoutManager(applicationContext) | ||
} | ||
}.start() | ||
} | ||
|
||
private fun saveExcludedApps() { | ||
binding.search.clearFocus() | ||
Settings.excludedApps = excludedApps.joinToString("\n") | ||
Settings.save(applicationContext) | ||
finish() | ||
} | ||
|
||
} |
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
56 changes: 56 additions & 0 deletions
56
app/src/main/java/io/github/saeeddev94/xray/adapter/ExcludeAdapter.kt
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,56 @@ | ||
package io.github.saeeddev94.xray.adapter | ||
|
||
import android.content.Context | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ImageView | ||
import android.widget.LinearLayout | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.google.android.material.checkbox.MaterialCheckBox | ||
import io.github.saeeddev94.xray.R | ||
import io.github.saeeddev94.xray.dto.AppList | ||
|
||
class ExcludeAdapter( | ||
private var context: Context, | ||
private var apps: MutableList<AppList>, | ||
private var excludedApps: MutableSet<String>, | ||
) : RecyclerView.Adapter<ExcludeAdapter.ViewHolder>() { | ||
|
||
override fun onCreateViewHolder(container: ViewGroup, type: Int): ViewHolder { | ||
val linearLayout = LinearLayout(context) | ||
val item: View = LayoutInflater.from(context).inflate(R.layout.item_recycler_exclude, linearLayout, false) | ||
return ViewHolder(item) | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return apps.size | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, index: Int) { | ||
val app = apps[index] | ||
val isSelected = excludedApps.contains(app.packageName) | ||
holder.appIcon.setImageDrawable(app.appIcon) | ||
holder.appName.text = app.appName | ||
holder.packageName.text = app.packageName | ||
holder.isSelected.isChecked = isSelected | ||
holder.appContainer.setOnClickListener { | ||
if (isSelected) { | ||
excludedApps.remove(app.packageName) | ||
holder.isSelected.isChecked = false | ||
} else { | ||
excludedApps.add(app.packageName) | ||
holder.isSelected.isChecked = true | ||
} | ||
} | ||
} | ||
|
||
class ViewHolder(item: View) : RecyclerView.ViewHolder(item) { | ||
var appContainer: LinearLayout = item.findViewById(R.id.appContainer) | ||
var appIcon: ImageView = item.findViewById(R.id.appIcon) | ||
var appName: TextView = item.findViewById(R.id.appName) | ||
var packageName: TextView = item.findViewById(R.id.packageName) | ||
var isSelected: MaterialCheckBox = item.findViewById(R.id.isSelected) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/io/github/saeeddev94/xray/component/EmptySubmitSearchView.kt
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,22 @@ | ||
package io.github.saeeddev94.xray.component | ||
|
||
import android.content.Context | ||
import android.util.AttributeSet | ||
import androidx.appcompat.widget.SearchView | ||
|
||
class EmptySubmitSearchView : SearchView { | ||
|
||
constructor(context: Context) : super(context) | ||
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) | ||
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) | ||
|
||
override fun setOnQueryTextListener(listener: OnQueryTextListener?) { | ||
super.setOnQueryTextListener(listener) | ||
val searchAutoComplete = this.findViewById<SearchAutoComplete>(androidx.appcompat.R.id.search_src_text) | ||
searchAutoComplete.setOnEditorActionListener { _, _, _ -> | ||
listener?.onQueryTextSubmit(query.toString()) | ||
return@setOnEditorActionListener true | ||
} | ||
} | ||
|
||
} |
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,9 @@ | ||
package io.github.saeeddev94.xray.dto | ||
|
||
import android.graphics.drawable.Drawable | ||
|
||
class AppList( | ||
var appIcon: Drawable, | ||
var appName: String, | ||
var packageName: String, | ||
) |
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,11 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="960" | ||
android:viewportHeight="960" | ||
android:tint="?attr/colorControlNormal" | ||
android:autoMirrored="true"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M440,880L440,680Q440,624 423,597Q406,570 378,544L435,487Q447,498 458,510.5Q469,523 480,537Q494,518 508.5,503.5Q523,489 538,475Q576,440 607,394Q638,348 640,233L577,296L520,240L680,80L840,240L784,296L720,233Q718,376 676,436.5Q634,497 592,535Q560,564 540,591.5Q520,619 520,680L520,880L440,880ZM248,327Q244,307 242.5,283Q241,259 240,233L176,296L120,240L280,80L440,240L383,296L320,234Q320,255 322,273.5Q324,292 326,308L248,327ZM334,503Q314,482 295.5,454Q277,426 263,385L340,366Q350,393 363,412Q376,431 391,446L334,503Z"/> | ||
</vector> |
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,30 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:orientation="vertical" | ||
tools:context=".activity.ExcludeActivity"> | ||
<com.google.android.material.appbar.AppBarLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
<androidx.appcompat.widget.Toolbar | ||
android:id="@+id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="?attr/actionBarSize" | ||
android:background="@android:color/black"> | ||
<io.github.saeeddev94.xray.component.EmptySubmitSearchView | ||
android:id="@+id/search" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:iconifiedByDefault="false" | ||
app:queryHint="Search" /> | ||
</androidx.appcompat.widget.Toolbar> | ||
</com.google.android.material.appbar.AppBarLayout> | ||
<androidx.recyclerview.widget.RecyclerView | ||
android:id="@+id/appsList" | ||
android:layout_width="match_parent" | ||
android:layout_height="0dp" | ||
android:layout_weight="1" /> | ||
</LinearLayout> |
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,49 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/appContainer" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="horizontal" | ||
android:gravity="center" | ||
android:baselineAligned="false" | ||
android:background="?attr/selectableItemBackground" | ||
android:clickable="true" | ||
android:focusable="true"> | ||
<LinearLayout | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
android:padding="10dp"> | ||
<ImageView | ||
android:id="@+id/appIcon" | ||
android:layout_width="60dp" | ||
android:layout_height="60dp" | ||
android:contentDescription="@string/appIcon" /> | ||
</LinearLayout> | ||
<LinearLayout | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:orientation="vertical"> | ||
<TextView | ||
android:id="@+id/appName" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" /> | ||
<TextView | ||
android:id="@+id/packageName" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" /> | ||
</LinearLayout> | ||
<LinearLayout | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
<com.google.android.material.checkbox.MaterialCheckBox | ||
android:id="@+id/isSelected" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:clickable="false" | ||
android:focusable="false" /> | ||
</LinearLayout> | ||
</LinearLayout> |
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
Oops, something went wrong.