Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
akliuxingyuan committed Jan 22, 2024
1 parent e10ca41 commit 4a8baa4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,69 +1,47 @@
import android.content.Context
import android.content.Intent
import android.net.Uri
import io.github.forkmaintainers.iceraven.components.getSafeString
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.webextension.WebExtension
import mozilla.components.feature.intent.processing.IntentProcessor
import mozilla.components.support.ktx.android.net.getFileName
import org.json.JSONException
import org.json.JSONObject
import java.io.File
import java.io.FileOutputStream
import java.net.URLEncoder
import java.util.Base64
import java.util.zip.ZipFile


class AddonInstallIntentProcessor(private val context: Context, private val engine: Engine) : IntentProcessor {
override fun process(intent: Intent): Boolean {
if(intent.data == null) {
return false
}
val iuri = intent.data as Uri
if(!iuri.scheme.equals("content")) {
return false
}
val file = fromUri(iuri)
if(file == null) {
return false
}
val ext = file.let { parseExtension(it) }
installExtension(ext.get(0), ext.get(1), null)
return true
}
fun installExtension(id: String, b64: String, onSuccess: ((WebExtension) -> Unit)?) {
engine.installWebExtension(id, b64, if(onSuccess != null) {
onSuccess
} else {
{ }
})
}
fun parseExtension(inp: File): List<String> {
val file = ZipFile(inp)
val mis = file.getInputStream(file.getEntry("manifest.json"))
val t = org.json.JSONObject(String(mis.readBytes()))
val al = ArrayList<String>()
val bss = try {
t.getJSONObject("browser_specific_settings")
} catch(e:JSONException) {
t.getJSONObject("applications")
}
al.add(bss.getJSONObject("gecko").getSafeString("id") )
al.add(Uri.fromFile(inp.absoluteFile).toString())
file.close()
mis.close()
return al
}
fun fromUri(uri: Uri): File? {
val name = uri.getFileName(context.contentResolver)
val file: File = File(context.externalCacheDir, name)
file.createNewFile()
val ostream = FileOutputStream(file.absolutePath)
val istream = context.contentResolver.openInputStream(uri)!!
istream.copyTo(ostream)
ostream.close()
istream.close()
return file
}
}
import android.content.Context
import android.content.Intent
import android.net.Uri
import mozilla.components.concept.engine.webextension.InstallationMethod
import mozilla.components.concept.engine.webextension.WebExtension
import mozilla.components.concept.engine.webextension.WebExtensionRuntime
import mozilla.components.feature.intent.processing.IntentProcessor
import mozilla.components.support.ktx.android.net.getFileName
import java.io.File
import java.io.FileOutputStream


class AddonInstallIntentProcessor(private val context: Context, private val runtime: WebExtensionRuntime) : IntentProcessor {
override fun process(intent: Intent): Boolean {
if(intent.data == null) {
return false
}
val iuri = intent.data as Uri
if(!iuri.scheme.equals("content")) {
return false
}
val file = fromUri(iuri)
val extURI = parseExtension(file)
installExtension(extURI) {}
return true
}

fun installExtension(b64: String, onSuccess: ((WebExtension) -> Unit)) {
runtime.installWebExtension(b64, InstallationMethod.FROM_FILE, onSuccess)
}

fun parseExtension(inp: File): String {
return Uri.fromFile(inp.absoluteFile).toString()
}

fun fromUri(uri: Uri): File {
val name = uri.getFileName(context.contentResolver)
val file = File(context.externalCacheDir, name)
file.createNewFile()
val ostream = FileOutputStream(file.absolutePath)
val istream = context.contentResolver.openInputStream(uri)!!
istream.copyTo(ostream)
ostream.close()
istream.close()
return file
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class AddonsManagementFragment : Fragment(R.layout.fragment_add_ons_management)
private var addons: List<Addon> = emptyList()

private var adapter: AddonsManagerAdapter? = null

private var addonImportFilePicker: ActivityResultLauncher<Intent>? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
logger.info("View created for AddonsManagementFragment")
super.onViewCreated(view, savedInstanceState)
Expand All @@ -86,25 +88,26 @@ class AddonsManagementFragment : Fragment(R.layout.fragment_add_ons_management)
result: ActivityResult ->
if(result.resultCode == Activity.RESULT_OK) {
result.data?.data?.let{uri ->
requireComponents.intentProcessors.addonInstallIntentProcessor.fromUri(uri)?.let{tmp ->
val ext = requireComponents.intentProcessors.addonInstallIntentProcessor.parseExtension(tmp)
requireComponents.intentProcessors.addonInstallIntentProcessor.installExtension(
ext[0], ext[1],
onSuccess = {
val ao = Addon.newFromWebExtension(it)
runIfFragmentIsAttached {
adapter?.updateAddon(ao)
binding?.addonProgressOverlay?.overlayCardView?.visibility = View.GONE
requireComponents.intentProcessors.addonInstallIntentProcessor.fromUri(uri)
.let{ tmpFile ->
val extURI = requireComponents.intentProcessors.addonInstallIntentProcessor.parseExtension(tmpFile)
requireComponents.intentProcessors.addonInstallIntentProcessor.installExtension(
extURI,
onSuccess = {
val installedState = provideAddonManger().toInstalledState(it)
val ao = Addon.newFromWebExtension(it, installedState)
runIfFragmentIsAttached {
adapter?.updateAddon(ao)
binding?.addonProgressOverlay?.overlayCardView?.visibility = View.GONE
}
}
}
)
}
)
}
}
}
}
}


private fun setupMenu() {
val menuHost = requireActivity() as MenuHost

Expand Down Expand Up @@ -153,13 +156,15 @@ class AddonsManagementFragment : Fragment(R.layout.fragment_add_ons_management)
viewLifecycleOwner, Lifecycle.State.RESUMED,
)
}

private fun installFromFile() {
val intent = Intent()
.setType("application/x-xpinstall")
.setAction(Intent.ACTION_GET_CONTENT)

addonImportFilePicker!!.launch(intent)
}

private fun showAlertDialog() {
val builder: AlertDialog.Builder = AlertDialog.Builder(requireContext())
builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object ReviewQualityCheckMiddlewareProvider {
providePreferencesMiddleware(settings, browserStore, appStore, scope),
provideNetworkMiddleware(browserStore, context, scope),
provideNavigationMiddleware(TabsUseCases.SelectOrAddUseCase(browserStore), context),
provideTelemetryMiddleware(),
provideTelemetryMiddleware(browserStore, appStore, scope),
)

private fun providePreferencesMiddleware(
Expand Down Expand Up @@ -82,6 +82,15 @@ object ReviewQualityCheckMiddlewareProvider {
GetReviewQualityCheckSumoUrl(context),
)

private fun provideTelemetryMiddleware() =
ReviewQualityCheckTelemetryMiddleware()
private fun provideTelemetryMiddleware(
browserStore: BrowserStore,
appStore: AppStore,
scope: CoroutineScope,
) =
ReviewQualityCheckTelemetryMiddleware(
telemetryService = DefaultReviewQualityCheckTelemetryService(browserStore),
browserStore = browserStore,
appStore = appStore,
scope = scope,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import mozilla.components.lib.state.Store
import org.mozilla.fenix.GleanMetrics.Shopping
import org.mozilla.fenix.GleanMetrics.ShoppingSettings
import org.mozilla.fenix.components.AppStore
import org.mozilla.fenix.components.appstate.AppAction.ShoppingAction
import org.mozilla.fenix.components.appstate.shopping.ShoppingState
import org.mozilla.fenix.shopping.store.ReviewQualityCheckAction
import org.mozilla.fenix.shopping.store.ReviewQualityCheckMiddleware
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState.OptedIn.ProductReviewState.AnalysisPresent.AnalysisStatus

private const val ACTION_ENABLED = "enabled"
Expand Down Expand Up @@ -195,6 +198,6 @@ class ReviewQualityCheckTelemetryMiddleware(

private fun ReviewQualityCheckState.isStaleAnalysis(): Boolean =
this is ReviewQualityCheckState.OptedIn &&
this.productReviewState is ReviewQualityCheckState.OptedIn.ProductReviewState.AnalysisPresent &&
this.productReviewState.analysisStatus == AnalysisStatus.NeedsAnalysis
this.productReviewState is ReviewQualityCheckState.OptedIn.ProductReviewState.AnalysisPresent &&
this.productReviewState.analysisStatus == AnalysisStatus.NeedsAnalysis
}

0 comments on commit 4a8baa4

Please sign in to comment.