Skip to content

Commit

Permalink
拆包
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed May 1, 2020
1 parent e6d82fd commit 9122249
Show file tree
Hide file tree
Showing 19 changed files with 222 additions and 188 deletions.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,82 @@ use a transparent fragment to handle callbacks of activity in listener style api



# gradle

Add the JitPack repository in your build.gradle (top level module):

```
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
```

And add next dependencies in the build.gradle of the module:

```
dependencies {
implementation 'com.github.hss01248:transfragment'
}
```

# BaseTransFragment

define a class extends BaseTransFragment

define your own api in the fragment,and use it

just like:

```
new GoOutOfAppForResultFragment(fragment,info).goOutApp(listener);
```



# startActivity and getCallback

```
StartActivityUtil.startActivity(this, ActivityDemo2.class, null,true,
new TheActivityListener<ActivityDemo2>() {
@Override
public void onActivityCreated(@NonNull ActivityDemo2 activity, @Nullable Bundle savedInstanceState) {
//这里设置数据,然后在具体activityOnCreate()方法的super.onCreate()之后直接拿数据
activity.setData(666);
Toast.makeText(activity, "activity oncreate 回调", Toast.LENGTH_LONG).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(MainActivity.this, "activity onActivityResult 回调", Toast.LENGTH_LONG).show();
}
});
```

#

### start a intent to open a third party app and get result in callback:

```
StartActivityUtil.goOutAppForResult(this, intent, new OutActivityResultListener() {
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
boolean hasPermission = NotificationManagerCompat.from(MainActivity.this).areNotificationsEnabled();
Toast.makeText(MainActivity.this,"通知栏权限:"+hasPermission,Toast.LENGTH_SHORT).show();
}
@Override
public void onActivityNotFound(Throwable e) {
}
});
```



1 change: 1 addition & 0 deletions activityresult/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions activityresult/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 29
buildToolsVersion "29.0.1"


defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation project (':transfrag')
}
Empty file.
21 changes: 21 additions & 0 deletions activityresult/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.hss01248.activityresult;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

assertEquals("com.hss01248.activityresult.test", appContext.getPackageName());
}
}
2 changes: 2 additions & 0 deletions activityresult/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hss01248.activityresult" />
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.hss01248.transfrag;
package com.hss01248.activityresult;

import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.hss01248.transfrag.BaseTransFragment;

import java.util.Random;

/**
* time:2020/4/30
* author:hss
* desription:
*/
public class GoOutOfAppForResultFragment extends BaseTransFragment<Intent> {
class GoOutOfAppForResultFragment extends BaseTransFragment<Intent> {

int requestCode;
OutActivityResultListener listener;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.hss01248.transfrag;
package com.hss01248.activityresult;

import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.hss01248.transfrag.BaseTransFragment;

import java.util.Random;

/**
* time:2020/5/1
* author:hss
* desription:
*/
public class InAppResultFragment extends BaseTransFragment<Intent> {
class InAppResultFragment extends BaseTransFragment<Intent> {
public InAppResultFragment(FragmentActivity activity, Intent intent) {
super(activity, intent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hss01248.transfrag;
package com.hss01248.activityresult;

import android.app.Activity;
import android.content.Intent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hss01248.transfrag;
package com.hss01248.activityresult;

import android.annotation.SuppressLint;
import android.app.Activity;
Expand Down Expand Up @@ -82,30 +82,9 @@ public static <T extends Activity> void startActivity(@NonNull final Activity ac
}
if(activity instanceof FragmentActivity){
new InAppResultFragment((FragmentActivity) activity,intent).startActivityForResult(listener);
}else {
activity.startActivityForResult(intent,898);
}

/* RxActivityResult.on(activity)
.startIntent(intent)
.subscribe(new Consumer<Result<Activity>>() {
@Override
public void accept(Result<Activity> t) throws Exception {
if (debugable) {
Log.i("onActivityResult", "req:" + t.requestCode() + ",result:" + t.resultCode() + ",data:" + t.data());
}
listener.onActivityResult(t.requestCode(),
t.resultCode(), t.data());
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
if (debugable) {
throwable.printStackTrace();
}
listener.onActivityNotFound(throwable);
}
});*/

}

private static <T extends Activity> void registerCallback(final Application application, final Class<T> targetClazz,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hss01248.transfrag;
package com.hss01248.activityresult;

import android.app.Activity;
import android.content.Context;
Expand All @@ -14,7 +14,7 @@
* author:hss
* desription:
*/
public class TheActivityListener<T extends Activity> {
public class TheActivityListener<T extends Activity> implements OutActivityResultListener{



Expand All @@ -33,7 +33,8 @@ protected void onActivityResumed(@NonNull T activity) {
}


protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(resultCode == Activity.RESULT_OK){
onResultOK(data);
}else if(resultCode == Activity.RESULT_CANCELED){
Expand All @@ -56,7 +57,8 @@ protected void onResultOK(Intent data) {
}


protected void onActivityNotFound(Throwable e) {
@Override
public void onActivityNotFound(Throwable e) {


}
Expand Down
3 changes: 3 additions & 0 deletions activityresult/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">activityresult</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hss01248.activityresult;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation project (':transfrag')
implementation project (':activityresult')
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
Expand All @@ -14,9 +13,10 @@
import android.view.View;
import android.widget.Toast;

import com.hss01248.transfrag.OutActivityResultListener;
import com.hss01248.transfrag.TheActivityListener;
import com.hss01248.transfrag.StartActivityUtil;
import com.hss01248.activityresult.OutActivityResultListener;
import com.hss01248.activityresult.StartActivityUtil;
import com.hss01248.activityresult.TheActivityListener;


public class MainActivity extends AppCompatActivity {

Expand Down Expand Up @@ -89,11 +89,11 @@ private Intent getNotificationIntent() {
}

private void allCallback() {
//new TransFragmentUtil<AllListenerFragment,String>(this,"").getFragment();
StartActivityUtil.startActivity(this, ActivityDemo2.class, null,true,
new TheActivityListener<ActivityDemo2>() {
@Override
public void onActivityCreated(@NonNull ActivityDemo2 activity, @Nullable Bundle savedInstanceState) {
//可以在这里传递数据
activity.setData(666);
Toast.makeText(activity, "activity oncreate 回调", Toast.LENGTH_LONG).show();
}
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include ':app', ':transfrag'
include ':app', ':transfrag', ':activityresult'
rootProject.name='TransparentFragmentdemo'
Loading

0 comments on commit 9122249

Please sign in to comment.