Skip to content

Commit 95f4bf9

Browse files
add app market
1 parent 2b61141 commit 95f4bf9

File tree

85 files changed

+2126
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2126
-0
lines changed

app_market/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
.dart_tool/
3+
4+
.packages
5+
.pub/
6+
7+
build/

app_market/.metadata

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: 84f3d28555368a70270e9ac8390a9441df95e752
8+
channel: stable
9+
10+
project_type: plugin

app_market/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.0.1
2+
3+
* TODO: Describe initial release.

app_market/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: Add your license here.

app_market/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# app_market
2+
3+
A new Flutter plugin.
4+
5+
## Getting Started
6+
7+
This project is a starting point for a Flutter
8+
[plug-in package](https://flutter.dev/developing-packages/),
9+
a specialized package that includes platform-specific implementation code for
10+
Android and/or iOS.
11+
12+
For help getting started with Flutter, view our
13+
[online documentation](https://flutter.dev/docs), which offers tutorials,
14+
samples, guidance on mobile development, and a full API reference.
15+

app_market/android/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures

app_market/android/build.gradle

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
group 'com.flutter.app_market'
2+
version '1.0-SNAPSHOT'
3+
4+
buildscript {
5+
ext.kotlin_version = '1.3.50'
6+
repositories {
7+
google()
8+
jcenter()
9+
}
10+
11+
dependencies {
12+
classpath 'com.android.tools.build:gradle:3.5.0'
13+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
14+
}
15+
}
16+
17+
rootProject.allprojects {
18+
repositories {
19+
google()
20+
jcenter()
21+
}
22+
}
23+
24+
apply plugin: 'com.android.library'
25+
apply plugin: 'kotlin-android'
26+
27+
android {
28+
compileSdkVersion 29
29+
30+
sourceSets {
31+
main.java.srcDirs += 'src/main/kotlin'
32+
}
33+
defaultConfig {
34+
minSdkVersion 16
35+
}
36+
lintOptions {
37+
disable 'InvalidPackage'
38+
}
39+
}
40+
41+
dependencies {
42+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
43+
}

app_market/android/gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.gradle.jvmargs=-Xmx1536M
2+
android.useAndroidX=true
3+
android.enableJetifier=true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
zipStoreBase=GRADLE_USER_HOME
4+
zipStorePath=wrapper/dists
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip

app_market/android/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'app_market'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.flutter.app_market">
3+
</manifest>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.flutter.app_market
2+
3+
import android.content.ActivityNotFoundException
4+
import android.content.Context
5+
import android.content.Intent
6+
import android.content.pm.PackageManager
7+
import android.net.Uri
8+
import android.os.Build
9+
import androidx.annotation.NonNull
10+
import androidx.core.content.FileProvider
11+
import io.flutter.embedding.engine.plugins.FlutterPlugin
12+
import io.flutter.plugin.common.MethodCall
13+
import io.flutter.plugin.common.MethodChannel
14+
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
15+
import io.flutter.plugin.common.MethodChannel.Result
16+
import java.io.File
17+
18+
19+
/** AppMarketPlugin */
20+
class AppMarketPlugin : FlutterPlugin, MethodCallHandler {
21+
/// The MethodChannel that will the communication between Flutter and native Android
22+
///
23+
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
24+
/// when the Flutter Engine is detached from the Activity
25+
private lateinit var channel: MethodChannel
26+
private lateinit var context: Context
27+
28+
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
29+
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "com.flutter.app_market")
30+
channel.setMethodCallHandler(this)
31+
32+
context = flutterPluginBinding.applicationContext
33+
}
34+
35+
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
36+
if (call.method == "getInstallMarkets") {
37+
result.success(getInstallMarkets(context))
38+
} else if (call.method == "toMarket") {
39+
val packageName = call.argument<String>("packageName")
40+
toMarket(context, packageName)
41+
} else if (call.method == "exist") {
42+
val packageName = call.argument<String>("packageName")
43+
packageName?.also {
44+
result.success(exist(context, it))
45+
}
46+
} else if (call.method == "installApk") {
47+
val path = call.argument<String>("path")
48+
path?.also {
49+
installApk(context, it)
50+
}
51+
}
52+
}
53+
54+
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
55+
channel.setMethodCallHandler(null)
56+
}
57+
58+
/**
59+
* 获取手机上安装的所有应用商店
60+
*/
61+
private fun getInstallMarkets(context: Context): List<String> {
62+
val intent = Intent()
63+
intent.action = "android.intent.action.VIEW"
64+
intent.addCategory(Intent.CATEGORY_DEFAULT)
65+
intent.data = Uri.parse("market://details?id=")
66+
val infos = context.packageManager.queryIntentActivities(intent, 0)
67+
val list = arrayListOf<String>()
68+
infos.forEach {
69+
list.add(it.activityInfo.packageName)
70+
}
71+
return list
72+
}
73+
74+
/**
75+
* 跳转到应用市场
76+
*/
77+
private fun toMarket(context: Context, packageName: String?) {
78+
try {
79+
var packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
80+
val uri = Uri.parse("market://details?id=${packageInfo.packageName}")
81+
val goToMarket = Intent(Intent.ACTION_VIEW, uri)
82+
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
83+
packageName?.also {
84+
if (it.isNotEmpty()) {
85+
goToMarket.setPackage(it)
86+
}
87+
}
88+
context.startActivity(goToMarket)
89+
} catch (e: ActivityNotFoundException) {
90+
e.printStackTrace()
91+
}
92+
}
93+
94+
/**
95+
* 是否存在当前应用市场
96+
*
97+
*/
98+
private fun exist(context: Context, packageName: String): Boolean {
99+
val manager = context.packageManager
100+
val intent = Intent().setPackage(packageName)
101+
val infos = manager.queryIntentActivities(intent,
102+
PackageManager.GET_INTENT_FILTERS)
103+
return infos.size > 0
104+
}
105+
106+
/**
107+
* 安装app,android 7.0及以上和以下方式不同
108+
*/
109+
private fun installApk(context: Context, path: String) {
110+
val file = File(path)
111+
if (!file.exists()) {
112+
return
113+
}
114+
115+
val intent = Intent(Intent.ACTION_VIEW)
116+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
117+
//7.0及以上
118+
119+
val contentUri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
120+
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
121+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
122+
intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
123+
context.startActivity(intent)
124+
} else {
125+
//7.0以下
126+
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive")
127+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
128+
context.startActivity(intent)
129+
}
130+
}
131+
}

app_market/example/.gitignore

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
**/ios/Flutter/.last_build_id
26+
.dart_tool/
27+
.flutter-plugins
28+
.flutter-plugins-dependencies
29+
.packages
30+
.pub-cache/
31+
.pub/
32+
/build/
33+
34+
# Web related
35+
lib/generated_plugin_registrant.dart
36+
37+
# Symbolication related
38+
app.*.symbols
39+
40+
# Obfuscation related
41+
app.*.map.json

app_market/example/.metadata

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: 84f3d28555368a70270e9ac8390a9441df95e752
8+
channel: stable
9+
10+
project_type: app

app_market/example/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# app_market_example
2+
3+
Demonstrates how to use the app_market plugin.
4+
5+
## Getting Started
6+
7+
This project is a starting point for a Flutter application.
8+
9+
A few resources to get you started if this is your first Flutter project:
10+
11+
- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
12+
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)
13+
14+
For help getting started with Flutter, view our
15+
[online documentation](https://flutter.dev/docs), which offers tutorials,
16+
samples, guidance on mobile development, and a full API reference.

app_market/example/android/.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
gradle-wrapper.jar
2+
/.gradle
3+
/captures/
4+
/gradlew
5+
/gradlew.bat
6+
/local.properties
7+
GeneratedPluginRegistrant.java
8+
9+
# Remember to never publicly share your keystore.
10+
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
11+
key.properties
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
def localProperties = new Properties()
2+
def localPropertiesFile = rootProject.file('local.properties')
3+
if (localPropertiesFile.exists()) {
4+
localPropertiesFile.withReader('UTF-8') { reader ->
5+
localProperties.load(reader)
6+
}
7+
}
8+
9+
def flutterRoot = localProperties.getProperty('flutter.sdk')
10+
if (flutterRoot == null) {
11+
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12+
}
13+
14+
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
15+
if (flutterVersionCode == null) {
16+
flutterVersionCode = '1'
17+
}
18+
19+
def flutterVersionName = localProperties.getProperty('flutter.versionName')
20+
if (flutterVersionName == null) {
21+
flutterVersionName = '1.0'
22+
}
23+
24+
apply plugin: 'com.android.application'
25+
apply plugin: 'kotlin-android'
26+
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
27+
28+
android {
29+
compileSdkVersion 29
30+
31+
sourceSets {
32+
main.java.srcDirs += 'src/main/kotlin'
33+
}
34+
35+
lintOptions {
36+
disable 'InvalidPackage'
37+
}
38+
39+
defaultConfig {
40+
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
41+
applicationId "com.flutter.app_market_example"
42+
minSdkVersion 16
43+
targetSdkVersion 29
44+
versionCode flutterVersionCode.toInteger()
45+
versionName flutterVersionName
46+
}
47+
48+
buildTypes {
49+
release {
50+
// TODO: Add your own signing config for the release build.
51+
// Signing with the debug keys for now, so `flutter run --release` works.
52+
signingConfig signingConfigs.debug
53+
}
54+
}
55+
}
56+
57+
flutter {
58+
source '../..'
59+
}
60+
61+
dependencies {
62+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
63+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.flutter.app_market_example">
3+
<!-- Flutter needs it to communicate with the running application
4+
to allow setting breakpoints, to provide hot reload, etc.
5+
-->
6+
<uses-permission android:name="android.permission.INTERNET"/>
7+
</manifest>

0 commit comments

Comments
 (0)