GCX-HCI / tray

a SharedPreferences replacement for Android with multiprocess support
Apache License 2.0
2.29k stars 273 forks source link

ApplicationId as authority and add ability to override it #100

Closed StefMa closed 7 years ago

StefMa commented 7 years ago

This build on top of #81 but add the ability to override the default authority of applicatonId.tray. You can check each commit of me. Hopefully I have separated in a good and readable way.

In case you miss something: We will now use always the applicaitonId.tray as authority. This will be set inside the TrayContentProvider in attachInfo(). Since ContentProviders lifecycle is as long as the process lives, it is a clean solution to cache the authority info inside the class.

You have the ability to override the default authority inside the AndroidManifest. I've added the instructions part how to do it inside the AndroidManifest.

Note: You can't override the ContentProvider in each flavor inside the build.gradle anymore. If you want to override it for each flavor (or build-type) you have to create a new package with the specific AndroidManifest in it. I hope that is clear. If not, inform me. Maybe we have to update the Readme with more information about that behaviour...?!

I've also changed the "behaviour" if the TrayContentProvider#mAuthority is empty. We will now throw a RuntimeException here. It is a "safety" feature, in my option. Since the ContentProvider starts as a "first part" of you App it should never happen. Otherwise we implemented something wrong....

How to test

Push that PR to your mavenLocal (or something like that). (Note: Change the library version. Otherwise you will get in trouble and/or you have maybe version conflicts.) Create a new project and include tray like any other app. Attach the debugger in TrayContract#getAuthority and take a look what they return. The first run: Without any specific setup(!) will return your default applicationId + .tray. Now create some build flavors and add applicationIdSuffix and run. The second run: TrayContract#getAuthority should return applicationId + applicationIdSuffix + .tray.

For legacy testing: Add resValue "string", "tray__authority", "my.custom.authority" into your build.gradle. The third run: You will notice that we will use the same authority like above. But inform the user via Logcat that they use a legacy method to override the default authority.

Overriding: To override just add the following code to your AndoridManifest:

        <provider
            android:name="net.grandcentrix.tray.provider.TrayContentProvider"
            android:authorities="your.custom.authority"
            android:exported="false"
            tools:replace="android:authorities" />

The fourth run: getAuthority will now return your.custom.authoity. Overriding in build-flavors: Create a new AndroidManifest inside app/src/flavorName/ and put the following inside:

<manifest package="net.grandcentrix.testtray"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application>

        <provider
            android:name="net.grandcentrix.tray.provider.TrayContentProvider"
            android:authorities="another.custom.provider"
            android:exported="false"
            tools:replace="android:authorities" />

    </application>

</manifest>

Run the two different flavors you have created. The fifhst run (build falvor without the specific AndroidManifest: Should return the "default" overriding value. Which means your.custom.authority (from the src/main/AndroidManifest) The second (and last) run (flavor with the specific AndroidManifest: Should another.custom.provider (from the src/buildFlavor/AndroidManifest).

Hope I haven't miss something. Otherwise: Feel free to merge 👍

codecov-io commented 7 years ago

Codecov Report

Merging #100 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##             master   #100   +/-   ##
=======================================
  Coverage       100%   100%           
- Complexity      271    274    +3     
=======================================
  Files            22     22           
  Lines           659    670   +11     
  Branches         57     60    +3     
=======================================
+ Hits            659    670   +11
Impacted Files Coverage Δ Complexity Δ
...randcentrix/tray/provider/TrayContentProvider.java 100% <100%> (ø) 35 <1> (+1) :arrow_up:
...a/net/grandcentrix/tray/provider/TrayContract.java 100% <100%> (ø) 11 <7> (+2) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 4ece993...a73f3b4. Read the comment docs.

StefMa commented 7 years ago

Don't know if it a good idea to move the info to the wiki. Because ... no one read a github wiki 😄

Rest is working fine with your changes 👍

But what happen if someone have overridden TrayContentProvider? 🤔 Is that anyhow possible?

passsy commented 7 years ago

The content provider is an internal component. Extending it will not work. Also using tray to share data between apps could be harder. But that's ok, tray was never designed for this.