Commit b78c10e8 authored by Max Lv's avatar Max Lv

Fix content provider

parent 91be4146
...@@ -93,7 +93,7 @@ class ProfileConfigFragment extends PreferenceFragment with OnMenuItemClickListe ...@@ -93,7 +93,7 @@ class ProfileConfigFragment extends PreferenceFragment with OnMenuItemClickListe
false false
}) })
initPlugins() initPlugins()
app.listenForPackageChanges(getView.post(initPlugins)) app.listenForPackageChanges(if (getView != null) getView.post(initPlugins))
app.settings.registerOnSharedPreferenceChangeListener(this) app.settings.registerOnSharedPreferenceChangeListener(this)
} }
......
package com.github.shadowsocks.plugin package com.github.shadowsocks.plugin
import android.content.pm.{PackageManager, ResolveInfo} import android.content.pm.{PackageManager, ProviderInfo}
/** /**
* @author Mygod * @author Mygod
*/ */
class NativePlugin(resolveInfo: ResolveInfo, packageManager: PackageManager) class NativePlugin(providerInfo: ProviderInfo, packageManager: PackageManager)
extends ResolvedPlugin(resolveInfo, packageManager) { extends ResolvedPlugin(providerInfo, packageManager) {
assert(resolveInfo.providerInfo != null) assert(providerInfo != null)
assert(resolveInfo.providerInfo.authority.startsWith(PluginInterface.AUTHORITY_BASE)) assert(providerInfo.authority.startsWith(PluginInterface.AUTHORITY_BASE))
override final lazy val id: String = override final lazy val id: String =
resolveInfo.providerInfo.authority.substring(PluginInterface.AUTHORITY_BASE.length) providerInfo.authority.substring(PluginInterface.AUTHORITY_BASE.length)
override final lazy val defaultConfig: String = override final lazy val defaultConfig: String =
resolveInfo.providerInfo.metaData.getString(PluginInterface.METADATA_KEY_DEFAULT_CONFIG) providerInfo.metaData.getString(PluginInterface.METADATA_KEY_DEFAULT_CONFIG)
} }
...@@ -5,6 +5,7 @@ import java.io.{File, FileNotFoundException, FileOutputStream, IOException} ...@@ -5,6 +5,7 @@ import java.io.{File, FileNotFoundException, FileOutputStream, IOException}
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.{BroadcastReceiver, ContentResolver, Intent} import android.content.{BroadcastReceiver, ContentResolver, Intent}
import android.net.Uri import android.net.Uri
import android.text.TextUtils
import com.github.shadowsocks.ShadowsocksApplication.app import com.github.shadowsocks.ShadowsocksApplication.app
import com.github.shadowsocks.utils.CloseUtils.autoClose import com.github.shadowsocks.utils.CloseUtils.autoClose
import com.github.shadowsocks.utils.IOUtils import com.github.shadowsocks.utils.IOUtils
...@@ -22,8 +23,10 @@ object PluginManager { ...@@ -22,8 +23,10 @@ object PluginManager {
synchronized(if (cachedPlugins == null) { synchronized(if (cachedPlugins == null) {
val pm = app.getPackageManager val pm = app.getPackageManager
cachedPlugins = (NoPlugin +: cachedPlugins = (NoPlugin +:
pm.queryIntentContentProviders(new Intent().addCategory(PluginInterface.CATEGORY_NATIVE_PLUGIN), pm.queryContentProviders(null, 0, PackageManager.GET_META_DATA).asScala
PackageManager.GET_META_DATA).asScala.map(new NativePlugin(_, pm))).toArray .filter(_ != null)
.filter(_.authority.startsWith(PluginInterface.AUTHORITY_BASE))
.map(new NativePlugin(_, pm))).toArray
}) })
cachedPlugins cachedPlugins
} }
...@@ -31,7 +34,7 @@ object PluginManager { ...@@ -31,7 +34,7 @@ object PluginManager {
// the following parts are meant to be used by :bg // the following parts are meant to be used by :bg
@throws[Throwable] @throws[Throwable]
def initPlugin(id: String): String = { def initPlugin(id: String): String = {
if (id.isEmpty) return null if (TextUtils.isEmpty(id)) return null
var throwable: Throwable = null var throwable: Throwable = null
try initNativePlugin(id) match { try initNativePlugin(id) match {
...@@ -62,7 +65,11 @@ object PluginManager { ...@@ -62,7 +65,11 @@ object PluginManager {
var initialized = false var initialized = false
val pluginDir = new File(app.getFilesDir, "plugin") val pluginDir = new File(app.getFilesDir, "plugin")
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
try {
IOUtils.deleteRecursively(pluginDir) IOUtils.deleteRecursively(pluginDir)
} catch {
case ex: IOException => // Ignore
}
if (!pluginDir.mkdirs()) throw new FileNotFoundException("Unable to create plugin directory") if (!pluginDir.mkdirs()) throw new FileNotFoundException("Unable to create plugin directory")
val pluginDirPath = pluginDir.getAbsolutePath + '/' val pluginDirPath = pluginDir.getAbsolutePath + '/'
do { do {
......
package com.github.shadowsocks.plugin package com.github.shadowsocks.plugin
import android.content.pm.{PackageManager, ResolveInfo} import android.content.pm.{PackageManager, ProviderInfo}
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import com.github.shadowsocks.ShadowsocksApplication.app import com.github.shadowsocks.ShadowsocksApplication.app
...@@ -9,8 +9,8 @@ import com.github.shadowsocks.ShadowsocksApplication.app ...@@ -9,8 +9,8 @@ import com.github.shadowsocks.ShadowsocksApplication.app
* *
* @author Mygod * @author Mygod
*/ */
abstract class ResolvedPlugin(resolveInfo: ResolveInfo, packageManager: PackageManager = app.getPackageManager) abstract class ResolvedPlugin(providerInfo: ProviderInfo, packageManager: PackageManager = app.getPackageManager)
extends Plugin { extends Plugin {
override final lazy val label: CharSequence = resolveInfo.loadLabel(packageManager) override final lazy val label: CharSequence = packageManager.getApplicationLabel(providerInfo.applicationInfo)
override final lazy val icon: Drawable = resolveInfo.loadIcon(packageManager) override final lazy val icon: Drawable = packageManager.getApplicationIcon(providerInfo.applicationInfo)
} }
...@@ -39,7 +39,7 @@ class PluginConfigurationDialogFragment extends EditTextPreferenceDialogFragment ...@@ -39,7 +39,7 @@ class PluginConfigurationDialogFragment extends EditTextPreferenceDialogFragment
override def onPrepareDialogBuilder(builder: AlertDialog.Builder) { override def onPrepareDialogBuilder(builder: AlertDialog.Builder) {
super.onPrepareDialogBuilder(builder) super.onPrepareDialogBuilder(builder)
val intent = new Intent(PluginInterface.ACTION_HELP(getArguments.getString(PLUGIN_ID_FRAGMENT_TAG))) val intent = new Intent(PluginInterface.ACTION_HELP(getArguments.getString(PLUGIN_ID_FRAGMENT_TAG)))
if (intent.resolveActivity(getContext.getPackageManager) != null) builder.setNeutralButton("?", ((_, _) => if (intent.resolveActivity(getActivity.getPackageManager) != null) builder.setNeutralButton("?", ((_, _) =>
startActivityForResult(intent.putExtra(PluginInterface.EXTRA_OPTIONS, editText.getText.toString), startActivityForResult(intent.putExtra(PluginInterface.EXTRA_OPTIONS, editText.getText.toString),
REQUEST_CODE_HELP)): DialogInterface.OnClickListener) REQUEST_CODE_HELP)): DialogInterface.OnClickListener)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment