-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support register ContentProviders in host, fix #253
- Loading branch information
Showing
4 changed files
with
210 additions
and
67 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
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
93 changes: 93 additions & 0 deletions
93
Android/DevSample/small/src/main/java/net/wequick/small/SetUpProvider.java
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,93 @@ | ||
package net.wequick.small; | ||
|
||
import android.app.Application; | ||
import android.app.Instrumentation; | ||
import android.content.ContentProvider; | ||
import android.content.ContentValues; | ||
import android.content.Context; | ||
import android.content.pm.ProviderInfo; | ||
import android.database.Cursor; | ||
import android.net.Uri; | ||
import android.support.annotation.Nullable; | ||
|
||
import net.wequick.small.util.ReflectAccelerator; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
|
||
public class SetUpProvider extends ContentProvider { | ||
|
||
public SetUpProvider() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public boolean onCreate() { | ||
Application application = (Application) getContext().getApplicationContext(); | ||
Object/*ActivityThread*/ thread; | ||
List<ProviderInfo> providers; | ||
Instrumentation base; | ||
ApkBundleLauncher.InstrumentationWrapper wrapper; | ||
Field f; | ||
|
||
// Get activity thread | ||
thread = ReflectAccelerator.getActivityThread(application); | ||
|
||
// Replace instrumentation | ||
try { | ||
f = thread.getClass().getDeclaredField("mInstrumentation"); | ||
f.setAccessible(true); | ||
base = (Instrumentation) f.get(thread); | ||
wrapper = new ApkBundleLauncher.InstrumentationWrapper(base); | ||
f.set(thread, wrapper); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to replace instrumentation for thread: " + thread); | ||
} | ||
|
||
// Get providers | ||
try { | ||
f = thread.getClass().getDeclaredField("mBoundApplication"); | ||
f.setAccessible(true); | ||
Object/*AppBindData*/ data = f.get(thread); | ||
f = data.getClass().getDeclaredField("providers"); | ||
f.setAccessible(true); | ||
providers = (List<ProviderInfo>) f.get(data); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to get providers from thread: " + thread); | ||
} | ||
|
||
ApkBundleLauncher.sActivityThread = thread; | ||
ApkBundleLauncher.sProviders = providers; | ||
ApkBundleLauncher.sHostInstrumentation = base; | ||
ApkBundleLauncher.sBundleInstrumentation = wrapper; | ||
return false; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getType(Uri uri) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Uri insert(Uri uri, ContentValues values) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int delete(Uri uri, String selection, String[] selectionArgs) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { | ||
return 0; | ||
} | ||
} |
Oops, something went wrong.