Skip to content

Commit

Permalink
updated: AutoInitLibrary example by implementing content provider and…
Browse files Browse the repository at this point in the history
… setting it up so that the library is init on app start
  • Loading branch information
nisrulz committed Aug 19, 2018
1 parent 6a9bf47 commit 5705c59
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class MainActivity : AppCompatActivity() {
// Get Instance
val awesomeLib = AwesomeLib.getInstance()

// Init the Library
awesomeLib.init(this)
// No need to init the Library anymore, as it is being done so on app start

// Set the textview
textView.text = awesomeLib.stringData
Expand Down
13 changes: 12 additions & 1 deletion AutoInitLibrary/awesomelib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="github.nisrulz.sample.awesomelib" />
package="github.nisrulz.sample.awesomelib">

<application>
<provider
android:name=".AwesomeLibInitProvider"
android:authorities="${applicationId}.awesomelibinitprovider"
android:enabled="true"
android:exported="false"></provider>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package github.nisrulz.sample.awesomelib;

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;

public class AwesomeLibInitProvider extends ContentProvider {

public AwesomeLibInitProvider() {
}

@Override
public void attachInfo(Context context, ProviderInfo providerInfo) {
if (providerInfo == null) {
throw new NullPointerException("YourLibraryInitProvider ProviderInfo cannot be null.");
}
// So if the authorities equal the library internal ones, the developer forgot to set his applicationId
String libAuthorityString = "github.nisrulz.sample.awesomelib.awesomelibinitprovider";
if (libAuthorityString.equals(providerInfo.authority)) {
throw new IllegalStateException("Incorrect provider authority[" + libAuthorityString + "]" +
" in manifest. " +
"Most likely due to a "
+ "missing applicationId variable in application\'s build.gradle.");
}
super.attachInfo(context, providerInfo);
}

@Override
public boolean onCreate() {

// get the context (Application context)
Context context = getContext();
// initialize AwesomeLib here
AwesomeLib.getInstance().init(context);

return false;
}


@Override
public String getType(Uri uri) {
return null;
}


@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
}

0 comments on commit 5705c59

Please sign in to comment.