├── .gitignore ├── LICENCE ├── README.md ├── SharedContentProviders.iml ├── app1 ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── owlr │ │ └── app1 │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── owlr │ │ └── app1 │ │ └── MainActivity.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── app2 ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── owlr │ │ └── app2 │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── owlr │ │ └── app2 │ │ └── MainActivity.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── provider ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── owlr │ │ │ └── provider │ │ │ ├── MetaDataUtils.java │ │ │ ├── SharedContentChangedReceiver.java │ │ │ ├── SharedCursorUtils.java │ │ │ ├── SharedPreferencesProducer.java │ │ │ ├── SharedProvider.java │ │ │ ├── SharedProviderFinder.java │ │ │ ├── SharedSharedPreferences.java │ │ │ └── Types.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── owlr │ └── provider │ ├── MetaDataUtilsTest.java │ ├── SharedContentChangedReceiverTest.java │ └── SharedProviderFinderTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | /.idea 9 | *.iml 10 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Work in progress / Concept Piece 2 | 3 | Have multiple app? Need to share state between them (user details, favourites etc..). This is a 4 | distributed `SharedPreferences` backed by a `ContentProvider`. 5 | 6 | # Setup 7 | 8 | Include the `providers` module in your project. 9 | 10 | Add the following to your core app build config: 11 | ``` 12 | android { 13 | //... 14 | defaultConfig{ 15 | manifestPlaceholders = [sharedPermission: "com.base.package.name.PERMISSION", sharedAuthorityMatcher: "com\\\\.base\\\\.package\\\\.(?:[a-z]{1,}\\\\.)*provider"] 16 | } 17 | //... 18 | } 19 | ``` 20 | Missing these from your build config will break your build, they are **mandatory**. 21 | 22 | Then use the `SharedSharedPreference` class as you would normally and the library will handle 23 | distributing data. 24 | 25 | ``` 26 | SharedPreferences mPrefs = new SharedSharedPreference(mApplication); 27 | ``` 28 | 29 | ### ManifestPlaceholders 30 | 31 | We use the placeholders over Android Meta-data elements as the buildTools will fail at compile time 32 | if you forget to include these. 33 | 34 | **THESE MUST BE THE __SAME__ IN EVERY APP YOU PUBLISH** 35 | 36 | - `sharedPermission` The unique permission to use. This is used to identify which providers can talk 37 | to each other, this is combined with your app signature to make sure your apps can only talk to each 38 | other. 39 | - `sharedAuthorityMatcher` We use a regex method to find other providers. If your provider authority 40 | matches the regex then your apps will talk to each other. Worth noting these need to be common before 41 | deployment. E.g. use something like `com.company.{appname}`. That way you can find all your providers by default. 42 | 43 | ### ContentProvider SharedPreferences 44 | 45 | The SharedProvider is backed by a normal SharedPreferences, you can override this by implementing 46 | `SharedPreferencesProducer` on your `Application` object. 47 | 48 | ``` 49 | @Override 50 | public @Nullable SharedPreferences provideSharedPreferences(){ 51 | return context.getSharedPreferences("this_apps_local_store", Context.MODE_PRIVATE); 52 | } 53 | ``` 54 | 55 | ### Example Regex 56 | 57 | To find each provider use package names which have common elements, now and in the future. 58 | 59 | The default regex should be enough for you: `com\\\\.base\\\\.package\\\\.(?:[a-z]{1,}\\\\.)*provider`. 60 | This matches: 61 | - **com.base.package.**app1.**provider** 62 | - **com.base.package.**provider 63 | - **com.base.package.**app2.something.**provider** 64 | 65 | Of course make sure you swap out the necessary parts of your common package name. 66 | 67 | ### Providers 68 | 69 | Each app you create generates inherits a provider from the Library. (See snippet below). 70 | 71 | ``` 72 | 78 | ``` 79 | 80 | ### Permissions & Signatures 81 | 82 | ** All your apps need to be signed by the same Keystore** 83 | 84 | The placeholder you provide for `sharedPermission` needs to be unique to your group of apps. The 85 | `providers` and `BroadcastReceivers` rely on this `permission` and the fact that each app has the same 86 | signature. 87 | 88 | 89 | # Licence 90 | > Copyright 2016 OWLR Technologies Limited 91 | > 92 | > Licensed under the Apache License, Version 2.0 (the "License"); 93 | > you may not use this file except in compliance with the License. 94 | > You may obtain a copy of the License at 95 | > 96 | > http://www.apache.org/licenses/LICENSE-2.0 97 | > 98 | > Unless required by applicable law or agreed to in writing, software 99 | > distributed under the License is distributed on an "AS IS" BASIS, 100 | > WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 101 | > See the License for the specific language governing permissions and 102 | > limitations under the License. 103 | 104 | -------------------------------------------------------------------------------- /SharedContentProviders.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app1/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app1/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "23.0.0 rc3" 6 | 7 | defaultConfig { 8 | applicationId "com.owlr.app1" 9 | minSdkVersion 16 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | manifestPlaceholders = 14 | [sharedPermission: "com.owlr.anotherpermission.PERMISSION", sharedAuthorityMatcher: "com\\\\.owlr\\\\.(.*?\\\\.)provider"] 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | compile project(':provider') 27 | compile 'com.android.support:appcompat-v7:22.2.0' 28 | } 29 | -------------------------------------------------------------------------------- /app1/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /usr/local/opt/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app1/src/androidTest/java/com/owlr/app1/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.owlr.app1; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app1/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app1/src/main/java/com/owlr/app1/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.owlr.app1; 2 | 3 | import android.os.Bundle; 4 | import android.os.Handler; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.Menu; 7 | import android.view.MenuItem; 8 | import com.owlr.provider.SharedSharedPreferences; 9 | 10 | public class MainActivity extends AppCompatActivity { 11 | 12 | @Override protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_main); 15 | 16 | Handler handler = new Handler(); 17 | handler.postDelayed(new Runnable() { 18 | @Override public void run() { 19 | SharedSharedPreferences sharedSharedPreferences = 20 | new SharedSharedPreferences(getApplication()); 21 | sharedSharedPreferences.edit().putString("something", "Hello").commit(); 22 | } 23 | }, 2000); 24 | } 25 | 26 | @Override public boolean onCreateOptionsMenu(Menu menu) { 27 | // Inflate the menu; this adds items to the action bar if it is present. 28 | getMenuInflater().inflate(R.menu.menu_main, menu); 29 | return true; 30 | } 31 | 32 | @Override public boolean onOptionsItemSelected(MenuItem item) { 33 | // Handle action bar item clicks here. The action bar will 34 | // automatically handle clicks on the Home/Up button, so long 35 | // as you specify a parent activity in AndroidManifest.xml. 36 | int id = item.getItemId(); 37 | 38 | //noinspection SimplifiableIfStatement 39 | if (id == R.id.action_settings) { 40 | return true; 41 | } 42 | 43 | return super.onOptionsItemSelected(item); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /app1/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app1/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /app1/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owlr-com/SharedContentProviders/c6d28ab53c8fec11bb67d81e99cb1ddc954aa8fe/app1/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app1/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owlr-com/SharedContentProviders/c6d28ab53c8fec11bb67d81e99cb1ddc954aa8fe/app1/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app1/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owlr-com/SharedContentProviders/c6d28ab53c8fec11bb67d81e99cb1ddc954aa8fe/app1/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app1/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owlr-com/SharedContentProviders/c6d28ab53c8fec11bb67d81e99cb1ddc954aa8fe/app1/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app1/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app1/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app1/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | App1 3 | 4 | Hello world! 5 | Settings 6 | 7 | -------------------------------------------------------------------------------- /app1/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app2/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app2/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "23.0.0 rc3" 6 | 7 | defaultConfig { 8 | applicationId "com.owlr.app2" 9 | minSdkVersion 16 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | manifestPlaceholders = 14 | [sharedPermission: "com.owlr.anotherpermission.PERMISSION", sharedAuthorityMatcher: "com\\\\.owlr\\\\.(.*?\\\\.)provider"] 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | compile project(':provider') 27 | compile 'com.android.support:appcompat-v7:22.2.0' 28 | } 29 | -------------------------------------------------------------------------------- /app2/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /usr/local/opt/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app2/src/androidTest/java/com/owlr/app2/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.owlr.app2; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app2/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app2/src/main/java/com/owlr/app2/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.owlr.app2; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.Menu; 6 | import android.view.MenuItem; 7 | import com.owlr.provider.SharedProviderFinder; 8 | 9 | public class MainActivity extends AppCompatActivity { 10 | 11 | @Override protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_main); 14 | 15 | SharedProviderFinder finder = SharedProviderFinder.get(this); 16 | finder.findMasterProvider(finder.findProviders()); 17 | } 18 | 19 | @Override public boolean onCreateOptionsMenu(Menu menu) { 20 | // Inflate the menu; this adds items to the action bar if it is present. 21 | getMenuInflater().inflate(R.menu.menu_main, menu); 22 | return true; 23 | } 24 | 25 | @Override public boolean onOptionsItemSelected(MenuItem item) { 26 | // Handle action bar item clicks here. The action bar will 27 | // automatically handle clicks on the Home/Up button, so long 28 | // as you specify a parent activity in AndroidManifest.xml. 29 | int id = item.getItemId(); 30 | 31 | //noinspection SimplifiableIfStatement 32 | if (id == R.id.action_settings) { 33 | return true; 34 | } 35 | 36 | return super.onOptionsItemSelected(item); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app2/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app2/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /app2/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owlr-com/SharedContentProviders/c6d28ab53c8fec11bb67d81e99cb1ddc954aa8fe/app2/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app2/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owlr-com/SharedContentProviders/c6d28ab53c8fec11bb67d81e99cb1ddc954aa8fe/app2/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app2/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owlr-com/SharedContentProviders/c6d28ab53c8fec11bb67d81e99cb1ddc954aa8fe/app2/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app2/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owlr-com/SharedContentProviders/c6d28ab53c8fec11bb67d81e99cb1ddc954aa8fe/app2/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app2/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app2/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app2/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | App2 3 | 4 | Hello world! 5 | Settings 6 | 7 | -------------------------------------------------------------------------------- /app2/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.3.0-beta4' 9 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2' 10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | jcenter() 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | org.gradle.daemon=true 19 | org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owlr-com/SharedContentProviders/c6d28ab53c8fec11bb67d81e99cb1ddc954aa8fe/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jun 10 14:11:40 BST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /provider/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /provider/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | apply plugin: "com.jfrog.bintray" 4 | 5 | 6 | // This is the library version used when deploying the artifact 7 | version = "1.0.0-beta7" 8 | group = 'com.owlr' 9 | def siteUrl = 'https://github.com/owlr-com/SharedContentProviders' 10 | // Homepage URL of the library 11 | def gitUrl = 'https://github.com/owlr-com/SharedContentProviders.git' 12 | // Git repository URL 13 | 14 | android { 15 | compileSdkVersion 22 16 | buildToolsVersion "23.0.0 rc3" 17 | testBuildType "placeholder" 18 | 19 | defaultConfig { 20 | minSdkVersion 15 21 | targetSdkVersion 22 22 | versionCode 7 23 | versionName version 24 | manifestPlaceholders = 25 | [sharedPermission: '${sharedPermission}', sharedAuthorityMatcher: '${sharedAuthorityMatcher}'] 26 | } 27 | buildTypes { 28 | placeholder { 29 | manifestPlaceholders = 30 | [sharedPermission: 'com.test.PERMISSION', sharedAuthorityMatcher: 'com\\\\.test\\\\.provider'] 31 | } 32 | release { 33 | minifyEnabled false 34 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 35 | } 36 | } 37 | testOptions { 38 | unitTests.returnDefaultValues = false 39 | } 40 | } 41 | 42 | dependencies { 43 | compile fileTree(dir: 'libs', include: ['*.jar']) 44 | compile 'com.android.support:support-annotations:22.2.0' 45 | 46 | testCompile 'junit:junit:4.12' 47 | testCompile "org.mockito:mockito-core:1.10.19" 48 | testCompile ('org.powermock:powermock-api-mockito:1.6.2') { 49 | exclude module: 'hamcrest-core' 50 | exclude module: 'objenesis' 51 | } 52 | testCompile ('org.powermock:powermock-module-junit4:1.6.2') { 53 | exclude module: 'hamcrest-core' 54 | exclude module: 'objenesis' 55 | } 56 | testCompile 'com.squareup.assertj:assertj-android:1.0.0' 57 | } 58 | 59 | bintray { 60 | user = getProperty("bintray.user") 61 | key = getProperty("bintray.apikey") 62 | 63 | configurations = ['archives'] 64 | pkg { 65 | repo = "maven" 66 | name = "SharedContentProviders" 67 | websiteUrl = siteUrl 68 | vcsUrl = gitUrl 69 | licenses = ["Apache-2.0"] 70 | publish = true 71 | } 72 | } 73 | 74 | task sourcesJar(type: Jar) { 75 | from android.sourceSets.main.java.srcDirs 76 | classifier = 'sources' 77 | } 78 | task javadoc(type: Javadoc) { 79 | source = android.sourceSets.main.java.srcDirs 80 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 81 | } 82 | task javadocJar(type: Jar, dependsOn: javadoc) { 83 | classifier = 'javadoc' 84 | from javadoc.destinationDir 85 | } 86 | artifacts { 87 | archives javadocJar 88 | archives sourcesJar 89 | } 90 | 91 | install { 92 | repositories.mavenInstaller { 93 | // This generates POM.xml with proper parameters 94 | pom { 95 | project { 96 | packaging 'aar' 97 | 98 | // Add your description here 99 | name 'SharedContentProviders' 100 | url siteUrl 101 | version version 102 | 103 | // Set your license 104 | licenses { 105 | license { 106 | name 'The Apache Software License, Version 2.0' 107 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 108 | } 109 | } 110 | developers { 111 | developer { 112 | id 'chrisjenx' 113 | name 'Christopher Jenkins' 114 | email 'chris@owlr.com' 115 | } 116 | } 117 | scm { 118 | connection gitUrl 119 | developerConnection gitUrl 120 | url siteUrl 121 | } 122 | } 123 | } 124 | } 125 | } 126 | 127 | -------------------------------------------------------------------------------- /provider/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /usr/local/opt/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /provider/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 11 | 12 | 13 | 17 | 23 | 24 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 40 | 44 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /provider/src/main/java/com/owlr/provider/MetaDataUtils.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | import android.content.Context; 4 | import android.content.pm.ApplicationInfo; 5 | import android.content.pm.PackageManager; 6 | import android.os.Bundle; 7 | import android.support.annotation.Nullable; 8 | 9 | /** 10 | * Created by chris on 29/06/15. 11 | * For project SharedProviders 12 | */ 13 | public final class MetaDataUtils { 14 | 15 | public static final String APP_AUTHORITY_MATCHER = "app_authority_matcher"; 16 | public static final String APP_SHARED_PERMISSION = "app_shared_permission"; 17 | public static final String APP_AUTHORITY = "app_authority"; 18 | 19 | private MetaDataUtils() { 20 | } 21 | 22 | @Nullable public static String getSharedAuthorityMatcher(Context context) { 23 | Bundle metaData = getMetaData(context); 24 | return getMetaValue(metaData, APP_AUTHORITY_MATCHER); 25 | } 26 | 27 | @Nullable public static String getSharedPermission(Context context) { 28 | Bundle metaData = getMetaData(context); 29 | return getMetaValue(metaData, APP_SHARED_PERMISSION); 30 | } 31 | 32 | @Nullable public static String getAppAuthority(Context context) { 33 | Bundle metaData = getMetaData(context); 34 | if (metaData != null) { 35 | return metaData.getString(APP_AUTHORITY); 36 | } 37 | return null; 38 | } 39 | 40 | @Nullable protected static String getMetaValue(Bundle metaData, String appSharedPermission) { 41 | if (metaData != null) { 42 | return metaData.getString(appSharedPermission); 43 | } 44 | return null; 45 | } 46 | 47 | @Nullable protected static Bundle getMetaData(Context context) { 48 | try { 49 | ApplicationInfo applicationInfo = context.getPackageManager() 50 | .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); 51 | return applicationInfo.metaData; 52 | } catch (PackageManager.NameNotFoundException ignored) { 53 | } 54 | return null; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /provider/src/main/java/com/owlr/provider/SharedContentChangedReceiver.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import android.support.annotation.NonNull; 8 | import android.text.TextUtils; 9 | import android.util.Log; 10 | import java.util.Iterator; 11 | import java.util.Map; 12 | import java.util.Set; 13 | 14 | public class SharedContentChangedReceiver extends BroadcastReceiver implements Types { 15 | 16 | public static final String SENDER_AUTHORITY_KEY = "sender_authority"; 17 | 18 | /** 19 | * Send a broadcast with the dump of the master SharedPreference to do a dump of the masters 20 | * SharedPreference store to the slaves. 21 | */ 22 | public static void sendBroadcast(@NonNull Context context, @NonNull Map data) { 23 | //We use the shared permission as the Action as they are both linked. 24 | String sharedPermission = MetaDataUtils.getSharedPermission(context); 25 | String senderAuthority = MetaDataUtils.getAppAuthority(context); 26 | Intent intent = new Intent(sharedPermission); 27 | intent.putExtra(SENDER_AUTHORITY_KEY, senderAuthority); 28 | putDataIntoIntent(intent, data); 29 | context.sendBroadcast(intent, sharedPermission); 30 | } 31 | 32 | public SharedContentChangedReceiver() { 33 | } 34 | 35 | @Override public void onReceive(Context context, Intent intent) { 36 | final String appAuthority = MetaDataUtils.getAppAuthority(context); 37 | final String appSender = intent.getStringExtra(SENDER_AUTHORITY_KEY); 38 | Log.d("SharedProvider", "Received Data Changed Event, SentByAuth [" + appSender + "]"); 39 | 40 | // Ignore me as I sent this! 41 | if (TextUtils.isEmpty(appAuthority) || appAuthority.equalsIgnoreCase(appSender)) { 42 | Log.v("SharedProvider", "Skipped DataChange, SentByAuth [" + appSender + "]"); 43 | return; 44 | } 45 | putIntentIntoSharedPreferences(context, intent.getExtras()); 46 | } 47 | 48 | private static void putDataIntoIntent(Intent intent, Map data) { 49 | Set> entries = data.entrySet(); 50 | for (Map.Entry entry : entries) { 51 | String key = entry.getKey(); 52 | Object value = entry.getValue(); 53 | if (value instanceof String) { 54 | intent.putExtra(key, (String) value); 55 | } else if (value instanceof Boolean) { 56 | intent.putExtra(key, (Boolean) value); 57 | } else if (value instanceof Long) { 58 | intent.putExtra(key, (Long) value); 59 | } else if (value instanceof Integer) { 60 | intent.putExtra(key, (Integer) value); 61 | } else if (value instanceof Float) { 62 | intent.putExtra(key, (Float) value); 63 | } 64 | } 65 | } 66 | 67 | private static void putIntentIntoSharedPreferences(Context context, Bundle bundle) { 68 | final Iterator keysIter = bundle.keySet().iterator(); 69 | final String appAuthority = MetaDataUtils.getAppAuthority(context); 70 | if (TextUtils.isEmpty(appAuthority)) return; 71 | //We update OUR content provider! 72 | SharedSharedPreferences.SharedEditor edit = 73 | new SharedSharedPreferences.SharedEditor(context, appAuthority); 74 | 75 | Object value; 76 | String key; 77 | while (keysIter.hasNext()) { 78 | key = keysIter.next(); 79 | switch (key) { 80 | case MASTER_KEY: 81 | case SENDER_AUTHORITY_KEY: 82 | continue; 83 | } 84 | value = bundle.get(key); 85 | if (value instanceof String) { 86 | edit.putString(key, (String) value); 87 | } else if (value instanceof Boolean) { 88 | edit.putBoolean(key, (Boolean) value); 89 | } else if (value instanceof Long) { 90 | edit.putLong(key, (Long) value); 91 | } else if (value instanceof Integer) { 92 | edit.putInt(key, (Integer) value); 93 | } else if (value instanceof Float) { 94 | edit.putFloat(key, (Float) value); 95 | } 96 | } 97 | edit.apply(); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /provider/src/main/java/com/owlr/provider/SharedCursorUtils.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | import android.database.Cursor; 4 | 5 | /** 6 | * Created by chris on 29/06/15. 7 | * For project SharedProviders 8 | */ 9 | public final class SharedCursorUtils { 10 | private SharedCursorUtils() { 11 | } 12 | 13 | public static String getStringValue(Cursor cursor, String def) { 14 | if (cursor == null) return def; 15 | String value = def; 16 | if (cursor.moveToFirst()) { 17 | value = cursor.getString(0); 18 | } 19 | cursor.close(); 20 | return value; 21 | } 22 | 23 | public static boolean getBooleanValue(Cursor cursor, boolean def) { 24 | if (cursor == null) return def; 25 | boolean value = def; 26 | if (cursor.moveToFirst()) { 27 | value = cursor.getInt(0) > 0; 28 | } 29 | cursor.close(); 30 | return value; 31 | } 32 | 33 | public static int getIntValue(Cursor cursor, int def) { 34 | if (cursor == null) return def; 35 | int value = def; 36 | if (cursor.moveToFirst()) { 37 | value = cursor.getInt(0); 38 | } 39 | cursor.close(); 40 | return value; 41 | } 42 | 43 | public static long getLongValue(Cursor cursor, long def) { 44 | if (cursor == null) return def; 45 | long value = def; 46 | if (cursor.moveToFirst()) { 47 | value = cursor.getLong(0); 48 | } 49 | cursor.close(); 50 | return value; 51 | } 52 | 53 | public static float getFloatValue(Cursor cursor, float def) { 54 | if (cursor == null) return def; 55 | float value = def; 56 | if (cursor.moveToFirst()) { 57 | value = cursor.getFloat(0); 58 | } 59 | cursor.close(); 60 | return value; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /provider/src/main/java/com/owlr/provider/SharedPreferencesProducer.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | import android.content.SharedPreferences; 4 | import android.support.annotation.Nullable; 5 | 6 | /** 7 | * Implement this on the Application class to provide a custom shared prefs to the content 8 | * provider. 9 | * 10 | * Created by chris on 29/06/15. 11 | * For project SharedProviders 12 | */ 13 | public interface SharedPreferencesProducer { 14 | 15 | /** 16 | * Due to the nature of SharedPrefs, its difficult to pass your custom shared prefs to the 17 | * content provider. It's safer for it to request if from you. 18 | * 19 | * @return SharedPreference, can be null 20 | */ 21 | @Nullable SharedPreferences provideSharedPreferences(); 22 | } 23 | -------------------------------------------------------------------------------- /provider/src/main/java/com/owlr/provider/SharedProvider.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | import android.content.ContentProvider; 4 | import android.content.ContentResolver; 5 | import android.content.ContentValues; 6 | import android.content.Context; 7 | import android.content.SharedPreferences; 8 | import android.content.UriMatcher; 9 | import android.database.Cursor; 10 | import android.database.MatrixCursor; 11 | import android.net.Uri; 12 | import android.os.Build; 13 | import android.util.Log; 14 | import java.util.Map; 15 | 16 | /** 17 | * Created by chris on 11/06/15. 18 | */ 19 | public class SharedProvider extends ContentProvider implements Types { 20 | 21 | public static String AUTHORITY; 22 | public static Uri BASE_URI; 23 | private static UriMatcher sUriMatcher = null; 24 | 25 | private static final int MATCH_DATA = UriMatcher.NO_MATCH + 1; 26 | 27 | /** 28 | * Sets up this content provider with this this apps AUTHORITY pulled from the meta tag. 29 | * This will do nothing if {@link #BASE_URI} is not null. It is safe to assume everything as been 30 | * initialized if {@link #BASE_URI} is not null. 31 | */ 32 | static void init(Context context) { 33 | if (BASE_URI != null) return; 34 | AUTHORITY = MetaDataUtils.getAppAuthority(context); 35 | sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 36 | sUriMatcher.addURI(AUTHORITY, "*/*", MATCH_DATA); 37 | BASE_URI = Uri.parse("content://" + AUTHORITY); 38 | } 39 | 40 | /** 41 | * This is a normal shared prefs actually related locally to the app. We use this as our 42 | * DataStorage. 43 | * //TODO allow overriding with BackedSharedPrefs. 44 | */ 45 | private SharedPreferences mSharedPrefs; 46 | 47 | @Override public boolean onCreate() { 48 | init(getContext()); 49 | mSharedPrefs = getSharedPreferences(getContext()); 50 | return true; 51 | } 52 | 53 | @Override 54 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 55 | String sortOrder) { 56 | MatrixCursor cursor; 57 | switch (sUriMatcher.match(uri)) { 58 | case MATCH_DATA: 59 | final String key = uri.getPathSegments().get(0); 60 | final String type = uri.getPathSegments().get(1); 61 | cursor = new MatrixCursor(new String[] { key }); 62 | if (!mSharedPrefs.contains(key)) return cursor; 63 | MatrixCursor.RowBuilder rowBuilder = cursor.newRow(); 64 | Object object; 65 | if (STRING_TYPE.equals(type)) { 66 | object = mSharedPrefs.getString(key, null); 67 | } else if (BOOLEAN_TYPE.equals(type)) { 68 | object = mSharedPrefs.getBoolean(key, false) ? 1 : 0; 69 | } else if (LONG_TYPE.equals(type)) { 70 | object = mSharedPrefs.getLong(key, 0l); 71 | } else if (INT_TYPE.equals(type)) { 72 | object = mSharedPrefs.getInt(key, 0); 73 | } else if (FLOAT_TYPE.equals(type)) { 74 | object = mSharedPrefs.getFloat(key, 0f); 75 | } else { 76 | throw new IllegalArgumentException("Unsupported type " + uri); 77 | } 78 | rowBuilder.add(object); 79 | break; 80 | default: 81 | throw new IllegalArgumentException("Unsupported uri " + uri); 82 | } 83 | return cursor; 84 | } 85 | 86 | @Override public String getType(Uri uri) { 87 | //This could be the authority, but thought a constant package name makes more sense. 88 | return ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd." + BuildConfig.APPLICATION_ID + ".item"; 89 | } 90 | 91 | @Override public Uri insert(Uri uri, ContentValues values) { 92 | switch (sUriMatcher.match(uri)) { 93 | case MATCH_DATA: 94 | SharedPreferences.Editor editor = mSharedPrefs.edit(); 95 | for (Map.Entry entry : values.valueSet()) { 96 | final Object value = entry.getValue(); 97 | final String key = entry.getKey(); 98 | if (value == null) { 99 | editor.remove(key); 100 | } else if (value instanceof String) { 101 | editor.putString(key, (String) value); 102 | } else if (value instanceof Boolean) { 103 | editor.putBoolean(key, (Boolean) value); 104 | } else if (value instanceof Long) { 105 | editor.putLong(key, (Long) value); 106 | } else if (value instanceof Integer) { 107 | editor.putInt(key, (Integer) value); 108 | } else if (value instanceof Float) { 109 | editor.putFloat(key, (Float) value); 110 | } else { 111 | throw new IllegalArgumentException("Unsupported type " + uri); 112 | } 113 | } 114 | if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) { 115 | editor.apply(); 116 | } else { 117 | editor.commit(); 118 | } 119 | updateSlaves(getContext(), mSharedPrefs); 120 | break; 121 | default: 122 | throw new IllegalArgumentException("Unsupported uri " + uri); 123 | } 124 | return null; 125 | } 126 | 127 | @Override public int delete(Uri uri, String selection, String[] selectionArgs) { 128 | switch (sUriMatcher.match(uri)) { 129 | case MATCH_DATA: 130 | mSharedPrefs.edit().clear().commit(); 131 | updateSlaves(getContext(), mSharedPrefs); 132 | break; 133 | default: 134 | throw new IllegalArgumentException("Unsupported uri " + uri); 135 | } 136 | return 0; 137 | } 138 | 139 | @Override 140 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 141 | throw new UnsupportedOperationException(); 142 | } 143 | 144 | /** 145 | * Asks your App's local Application object for a SharedPreferences you want to provide one. 146 | */ 147 | private SharedPreferences getSharedPreferences(Context context) { 148 | SharedPreferences sharedPreferences = null; 149 | if (context.getApplicationContext() instanceof SharedPreferencesProducer) { 150 | sharedPreferences = 151 | ((SharedPreferencesProducer) context.getApplicationContext()).provideSharedPreferences(); 152 | } 153 | if (sharedPreferences == null) { 154 | sharedPreferences = getDefaultSharedPreferences(context); 155 | } 156 | if (!sharedPreferences.contains("created")) { 157 | sharedPreferences.edit().putLong("created", System.currentTimeMillis()).apply(); 158 | } 159 | return sharedPreferences; 160 | } 161 | 162 | private SharedPreferences getDefaultSharedPreferences(Context context) { 163 | return context.getSharedPreferences("local_shared_prefs", Context.MODE_PRIVATE); 164 | } 165 | 166 | private static void updateSlaves(Context context, SharedPreferences sharedPreferences) { 167 | if (sharedPreferences.getBoolean(MASTER_KEY, false)) { 168 | Log.d("SharedProviders", "UpdateSlaves Called from [" + AUTHORITY + "]"); 169 | SharedContentChangedReceiver.sendBroadcast(context, sharedPreferences.getAll()); 170 | } 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /provider/src/main/java/com/owlr/provider/SharedProviderFinder.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | import android.content.ContentResolver; 4 | import android.content.ContentValues; 5 | import android.content.Context; 6 | import android.content.pm.PackageInfo; 7 | import android.content.pm.PackageManager; 8 | import android.content.pm.ProviderInfo; 9 | import android.net.Uri; 10 | import android.text.TextUtils; 11 | import android.util.Log; 12 | import java.util.ArrayList; 13 | import java.util.Collections; 14 | import java.util.List; 15 | import java.util.regex.Matcher; 16 | import java.util.regex.Pattern; 17 | 18 | import static com.owlr.provider.SharedCursorUtils.getBooleanValue; 19 | 20 | /** 21 | * Handles app interaction, you should never need to use this manually, more if you want to pull 22 | * out providers yourself. 23 | * 24 | * Created by chris on 11/06/15. 25 | */ 26 | public class SharedProviderFinder implements Types { 27 | 28 | private static SharedProviderFinder sharedProviderFinder; 29 | 30 | private static SharedProviderFinder initDefaultFinder(Context context) { 31 | String sharedPermission = MetaDataUtils.getSharedPermission(context); 32 | final String matcherPattern = MetaDataUtils.getSharedAuthorityMatcher(context); 33 | if (TextUtils.isEmpty(matcherPattern)) { 34 | throw new IllegalStateException( 35 | "You need to define the \"app_authority_matcher\" meta-data in your ApplicationManifest.xml"); 36 | } 37 | Pattern pattern = Pattern.compile(matcherPattern); 38 | return new SharedProviderFinder(context, pattern, sharedPermission, 39 | context.getContentResolver()); 40 | } 41 | 42 | /** 43 | * Get a pre-configured finder instance. 44 | */ 45 | public static SharedProviderFinder get(Context context) { 46 | if (sharedProviderFinder == null) { 47 | sharedProviderFinder = initDefaultFinder(context); 48 | } 49 | return sharedProviderFinder; 50 | } 51 | 52 | private final Context context; 53 | private final Pattern authorityMatcherPattern; 54 | private final String sharedPermission; 55 | private final ContentResolver contentResolver; 56 | 57 | /** 58 | * Generally used for testing. You should use {@link #get(Context)} 59 | */ 60 | protected SharedProviderFinder(Context context, Pattern authorityMatcherPattern, 61 | String sharedPermission, ContentResolver contentResolver) { 62 | this.context = context; 63 | this.authorityMatcherPattern = authorityMatcherPattern; 64 | this.sharedPermission = sharedPermission; 65 | this.contentResolver = contentResolver; 66 | } 67 | 68 | /** 69 | * Finds providers that can work with this app. 70 | * 71 | * @return this could be empty. 72 | */ 73 | public List findProviders() { 74 | final Pattern authorityMatcherPattern = getAuthorityMatcherPattern(); 75 | Log.i("SharedProviders", 76 | "Find Authorities using: " + authorityMatcherPattern.pattern() + " Permission: " 77 | + getSharedPermission()); 78 | 79 | final List installedProviders = getInstalledProviders(); 80 | final int count = installedProviders.size(); 81 | final List matchedProviders = new ArrayList<>(count); 82 | final String sharedPermission = getSharedPermission(); 83 | 84 | Matcher matcher; 85 | ProviderInfo provider; 86 | for (int i = 0; i < count; i++) { 87 | provider = installedProviders.get(i); 88 | //#4 Fixes null auths (Facebook). 89 | if (TextUtils.isEmpty(provider.authority)) { 90 | continue; 91 | } 92 | matcher = authorityMatcherPattern.matcher(provider.authority); 93 | // #2/3 Skip if authority is null. Skip as we can't match it. 94 | if (matcher.matches() && sharedPermission.equalsIgnoreCase(provider.writePermission)) { 95 | Log.d("SharedProviders", 96 | "provider: " + provider.authority + " Matches: " + matcher.matches()); 97 | // It's matched add this. 98 | matchedProviders.add(provider); 99 | } 100 | } 101 | Log.d("SharedProviders", "Found " + matchedProviders.size() + " providers."); 102 | return matchedProviders; 103 | } 104 | 105 | /** 106 | * Finds the master authority. 107 | * 108 | * @param providerInfos Non-null list of found providers to check 109 | * @return authority to use for the Uri for the master. That could be you as in we can delegate 110 | * ourselves as master. 111 | */ 112 | public String findMasterProvider(List providerInfos) { 113 | if (providerInfos == null || providerInfos.size() < 1) { 114 | throw new IllegalStateException( 115 | "There should be at least one Provider registered for this to work."); 116 | } 117 | ProviderInfo providerInfo; 118 | String authority; 119 | boolean isMaster; 120 | String masterAuthority = null; 121 | for (int i = 0, size = providerInfos.size(); i < size; i++) { 122 | providerInfo = providerInfos.get(i); 123 | authority = providerInfo.authority; 124 | //#3 Some shitty apps produce providers with null (or the user match is wrong) 125 | if (TextUtils.isEmpty(authority)) continue; 126 | 127 | isMaster = isProviderMaster(getContentUri(authority), contentResolver); 128 | Log.d("SharedProviders", "Auth " + authority + " isMaster: " + isMaster); 129 | //Select the first Master Auth then we un delegate the rest. 130 | if (isMaster && TextUtils.isEmpty(masterAuthority)) { 131 | masterAuthority = authority; 132 | } else if (isMaster && !TextUtils.isEmpty(masterAuthority)) { 133 | // We un-delegate other masters. This can be if for some reason other masters were delegated 134 | // by themselves.. 135 | Log.d("SharedProviders", "Un-Delegate Auth: " + authority); 136 | delegateMaster(authority, false); 137 | } 138 | } 139 | if (!TextUtils.isEmpty(masterAuthority)) { 140 | return masterAuthority; 141 | } 142 | // If we reach here, then there are no masters so we delegate one. (Top of the list in the current impl) 143 | masterAuthority = providerInfos.get(0).authority; 144 | if (TextUtils.isEmpty(masterAuthority)) { 145 | throw new IllegalStateException("There are no valid providers to delegate. " 146 | + "Are you sure you have your permissions and authorityMatcher set correctly"); 147 | } 148 | return delegateMaster(masterAuthority, true); 149 | } 150 | 151 | /** 152 | * @see SharedProviderFinder#findMasterProvider(List) 153 | */ 154 | public String findMasterProvider() { 155 | return findMasterProvider(findProviders()); 156 | } 157 | 158 | /** 159 | * Calls the content provider and asks the remote provider if it has been assigned master. 160 | */ 161 | boolean isProviderMaster(Uri contentUri, ContentResolver resolver) { 162 | return getBooleanValue(resolver.query(contentUri, null, null, null, null), false); 163 | } 164 | 165 | Uri getContentUri(String authority) { 166 | return SharedSharedPreferences.getContentUri(authority, MASTER_KEY, BOOLEAN_TYPE); 167 | } 168 | 169 | String getSharedPermission() { 170 | return sharedPermission; 171 | } 172 | 173 | Pattern getAuthorityMatcherPattern() { 174 | return authorityMatcherPattern; 175 | } 176 | 177 | /** 178 | * Tell this Provider if it should be master or not. 179 | */ 180 | String delegateMaster(String authority, boolean isMaster) { 181 | ContentValues contentValues = new ContentValues(); 182 | contentValues.put(MASTER_KEY, isMaster); 183 | context.getContentResolver() 184 | .insert(SharedSharedPreferences.getContentUri(authority, KEY, TYPE), contentValues); 185 | return authority; 186 | } 187 | 188 | /** 189 | * Finds ALL installed Providers on the Device 190 | */ 191 | List getInstalledProviders() { 192 | final List installedPackages = 193 | context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS); 194 | final List providerInfoList = new ArrayList<>(); 195 | PackageInfo packageInfo; 196 | for (int i = 0; i < installedPackages.size(); i++) { 197 | packageInfo = installedPackages.get(i); 198 | if (packageInfo.providers != null) { 199 | Collections.addAll(providerInfoList, packageInfo.providers); 200 | } 201 | } 202 | return providerInfoList; 203 | } 204 | } 205 | -------------------------------------------------------------------------------- /provider/src/main/java/com/owlr/provider/SharedSharedPreferences.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | import android.content.ContentValues; 4 | import android.content.Context; 5 | import android.content.SharedPreferences; 6 | import android.database.Cursor; 7 | import android.net.Uri; 8 | import android.support.annotation.NonNull; 9 | import android.support.annotation.Nullable; 10 | import java.util.Map; 11 | import java.util.Set; 12 | 13 | import static com.owlr.provider.SharedCursorUtils.getBooleanValue; 14 | import static com.owlr.provider.SharedCursorUtils.getFloatValue; 15 | import static com.owlr.provider.SharedCursorUtils.getIntValue; 16 | import static com.owlr.provider.SharedCursorUtils.getLongValue; 17 | import static com.owlr.provider.SharedCursorUtils.getStringValue; 18 | 19 | /** 20 | * Created by chris on 18/06/15. 21 | */ 22 | public class SharedSharedPreferences implements SharedPreferences, Types { 23 | 24 | /** 25 | * Builds a Uri for the any provider authority content you are trying to access, this makes the 26 | * entire content provider flexible around different key/value/types at runtime. 27 | * 28 | * This allows other shared providers to no care about what they store, just just do. 29 | */ 30 | static Uri getContentUri(String authority, String key, String type) { 31 | return Uri.parse("content://" + authority).buildUpon().appendPath(key).appendPath(type).build(); 32 | } 33 | 34 | private final Context context; 35 | private String authority; 36 | 37 | /** 38 | * Get a SharedPreference instance, this will pull data from the distributed state. 39 | * There is a delegated master which this "talks to", it then syncs with all the slaves to make 40 | * sure if the master is removed one of the children will have the same data and re-delegate. 41 | * 42 | * There could be a case where the master changes and this points at the old master. Therefor 43 | * do not hold onto {@code Editor} instances. 44 | * 45 | * Make sure you get editors and commit them in one chain. 46 | * {@code 47 | * mPrefs.edit().putString("key","value").commit(); 48 | * } 49 | * 50 | * The {@code Editor} will refresh the master list on init. 51 | * 52 | * Getting and Setting StringSets are not supported, getAll is also not supported. 53 | * Also worth noting that observers are not supported either. 54 | * 55 | * @see #refreshAuthority() 56 | */ 57 | public SharedSharedPreferences(@NonNull Context context) { 58 | this.context = context.getApplicationContext(); 59 | refreshAuthority(); 60 | } 61 | 62 | /** 63 | * Manually set this Authority to talk to a specific Provider, generally used to target this 64 | * content 65 | * provider. 66 | */ 67 | protected SharedSharedPreferences(@NonNull Context context, @NonNull String authority) { 68 | this.context = context.getApplicationContext(); 69 | this.authority = authority; 70 | } 71 | 72 | public SharedSharedPreferences refreshAuthority() { 73 | SharedProviderFinder finder = SharedProviderFinder.get(context); 74 | authority = finder.findMasterProvider(); 75 | return this; 76 | } 77 | 78 | @Override public Map getAll() { 79 | return null; 80 | } 81 | 82 | @Nullable @Override public Set getStringSet(String key, Set defValues) { 83 | throw new UnsupportedOperationException("Not implemented."); 84 | } 85 | 86 | @Override public String getString(String key, String def) { 87 | Cursor cursor = context.getContentResolver() 88 | .query(getContentUri(authority, key, STRING_TYPE), null, null, null, null); 89 | return getStringValue(cursor, def); 90 | } 91 | 92 | public long getLong(String key, long def) { 93 | Cursor cursor = context.getContentResolver() 94 | .query(getContentUri(authority, key, LONG_TYPE), null, null, null, null); 95 | return getLongValue(cursor, def); 96 | } 97 | 98 | public float getFloat(String key, float def) { 99 | Cursor cursor = context.getContentResolver() 100 | .query(getContentUri(authority, key, FLOAT_TYPE), null, null, null, null); 101 | return getFloatValue(cursor, def); 102 | } 103 | 104 | public boolean getBoolean(String key, boolean def) { 105 | Cursor cursor = context.getContentResolver() 106 | .query(getContentUri(authority, key, BOOLEAN_TYPE), null, null, null, null); 107 | return getBooleanValue(cursor, def); 108 | } 109 | 110 | public int getInt(String key, int def) { 111 | Cursor cursor = context.getContentResolver() 112 | .query(getContentUri(authority, key, INT_TYPE), null, null, null, null); 113 | return getIntValue(cursor, def); 114 | } 115 | 116 | @Override public boolean contains(String key) { 117 | return false; 118 | } 119 | 120 | @Override public SharedEditor edit() { 121 | refreshAuthority(); 122 | return new SharedEditor(context, authority); 123 | } 124 | 125 | @Override 126 | public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { 127 | throw new UnsupportedOperationException("Not implemented."); 128 | } 129 | 130 | @Override public void unregisterOnSharedPreferenceChangeListener( 131 | OnSharedPreferenceChangeListener listener) { 132 | throw new UnsupportedOperationException("Not implemented."); 133 | } 134 | 135 | public static class SharedEditor implements SharedPreferences.Editor { 136 | 137 | private final Context context; 138 | private final String authority; 139 | 140 | SharedEditor(Context context, String authority) { 141 | this.context = context; 142 | this.authority = authority; 143 | } 144 | 145 | private ContentValues values = new ContentValues(); 146 | 147 | @Override public void apply() { 148 | context.getContentResolver().insert(getContentUri(authority, KEY, TYPE), values); 149 | values.clear(); 150 | } 151 | 152 | @Override public boolean commit() { 153 | apply(); 154 | return true; 155 | } 156 | 157 | @Override public SharedEditor putString(String key, String value) { 158 | values.put(key, value); 159 | return this; 160 | } 161 | 162 | @Override public SharedEditor putStringSet(String key, Set values) { 163 | throw new UnsupportedOperationException("Not implemented."); 164 | } 165 | 166 | @Override public SharedEditor putLong(String key, long value) { 167 | values.put(key, value); 168 | return this; 169 | } 170 | 171 | @Override public SharedEditor putBoolean(String key, boolean value) { 172 | values.put(key, value); 173 | return this; 174 | } 175 | 176 | @Override public SharedEditor putInt(String key, int value) { 177 | values.put(key, value); 178 | return this; 179 | } 180 | 181 | @Override public SharedEditor putFloat(String key, float value) { 182 | values.put(key, value); 183 | return this; 184 | } 185 | 186 | @Override public SharedEditor remove(String key) { 187 | values.putNull(key); 188 | return this; 189 | } 190 | 191 | /** 192 | * Call content provider method immediately. apply or commit is not required for this case 193 | * So it's sync method. 194 | */ 195 | @Override public SharedEditor clear() { 196 | context.getContentResolver().delete(getContentUri(authority, KEY, TYPE), null, null); 197 | return this; 198 | } 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /provider/src/main/java/com/owlr/provider/Types.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | /** 4 | * Created by chris on 17/06/15. 5 | */ 6 | public interface Types { 7 | 8 | String KEY = "key"; 9 | String TYPE = "type"; 10 | String MASTER_KEY = "master"; 11 | 12 | String INT_TYPE = "integer"; 13 | String LONG_TYPE = "long"; 14 | String FLOAT_TYPE = "float"; 15 | String BOOLEAN_TYPE = "boolean"; 16 | String STRING_TYPE = "string"; 17 | } 18 | -------------------------------------------------------------------------------- /provider/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | provider 3 | com.owlr.permission.group.PROVIDER 4 | 5 | -------------------------------------------------------------------------------- /provider/src/test/java/com/owlr/provider/MetaDataUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | import android.content.Context; 4 | import android.content.pm.ApplicationInfo; 5 | import android.content.pm.PackageManager; 6 | import android.os.Bundle; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.mockito.Mock; 10 | import org.mockito.MockitoAnnotations; 11 | 12 | import static org.assertj.core.api.Assertions.assertThat; 13 | import static org.mockito.Mockito.when; 14 | 15 | /** 16 | * Created by chris on 15/07/15. 17 | * For SharedContentProviders. 18 | */ 19 | public class MetaDataUtilsTest { 20 | 21 | @Mock Bundle bundle; 22 | @Mock Context context; 23 | @Mock PackageManager packageManager; 24 | @Mock ApplicationInfo applicationInfo; 25 | 26 | @Before public void setUp() throws Exception { 27 | MockitoAnnotations.initMocks(this); 28 | setupContext(); 29 | } 30 | 31 | /** 32 | * Lets you use the mock bundle. 33 | * 34 | * @throws PackageManager.NameNotFoundException 35 | */ 36 | private void setupContext() throws PackageManager.NameNotFoundException { 37 | when(context.getPackageName()).thenReturn("com.owlr.test"); 38 | when(context.getPackageManager()).thenReturn(packageManager); 39 | when(packageManager.getApplicationInfo("com.owlr.test", 40 | PackageManager.GET_META_DATA)).thenReturn(applicationInfo); 41 | applicationInfo.metaData = bundle; 42 | } 43 | 44 | @Test public void testGetSharedAuthorityMatcher() throws Exception { 45 | String expected = "com\\.owlr"; 46 | when(bundle.getString(MetaDataUtils.APP_AUTHORITY_MATCHER)).thenReturn(expected); 47 | assertThat(MetaDataUtils.getSharedAuthorityMatcher(context)).isEqualTo(expected); 48 | } 49 | 50 | @Test public void testGetSharedPermission() throws Exception { 51 | String expected = "com.owlr.PERMISSION"; 52 | when(bundle.getString(MetaDataUtils.APP_SHARED_PERMISSION)).thenReturn(expected); 53 | assertThat(MetaDataUtils.getSharedPermission(context)).isEqualTo(expected); 54 | } 55 | 56 | @Test public void testGetAppAuthority() throws Exception { 57 | String expected = "com.owlr"; 58 | when(bundle.getString(MetaDataUtils.APP_AUTHORITY)).thenReturn(expected); 59 | assertThat(MetaDataUtils.getAppAuthority(context)).isEqualTo(expected); 60 | } 61 | 62 | @Test public void testGetMetaValue() throws Exception { 63 | when(bundle.getString("app_key")).thenReturn("result"); 64 | final String result = MetaDataUtils.getMetaValue(bundle, "app_key"); 65 | assertThat(result).isEqualTo("result"); 66 | } 67 | 68 | @Test public void testGetMetaData() throws Exception { 69 | assertThat(MetaDataUtils.getMetaData(context)).isEqualTo(bundle); 70 | } 71 | } -------------------------------------------------------------------------------- /provider/src/test/java/com/owlr/provider/SharedContentChangedReceiverTest.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | import android.os.Bundle; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.mockito.Mock; 7 | import org.mockito.MockitoAnnotations; 8 | 9 | /** 10 | * Created by chris on 15/07/15. 11 | * For SharedContentProviders. 12 | */ 13 | public class SharedContentChangedReceiverTest { 14 | 15 | @Mock Bundle context; 16 | 17 | @Before public void setUp() throws Exception { 18 | MockitoAnnotations.initMocks(this); 19 | } 20 | 21 | @Test public void testSendBroadcast() throws Exception { 22 | 23 | } 24 | 25 | @Test public void testOnReceive() throws Exception { 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /provider/src/test/java/com/owlr/provider/SharedProviderFinderTest.java: -------------------------------------------------------------------------------- 1 | package com.owlr.provider; 2 | 3 | import android.content.ContentResolver; 4 | import android.content.pm.ProviderInfo; 5 | import android.database.Cursor; 6 | import android.net.Uri; 7 | import android.text.TextUtils; 8 | import android.util.Log; 9 | import java.util.Arrays; 10 | import java.util.Collections; 11 | import java.util.List; 12 | import java.util.regex.Pattern; 13 | import org.junit.After; 14 | import org.junit.Before; 15 | import org.junit.Test; 16 | import org.junit.runner.RunWith; 17 | import org.mockito.Mock; 18 | import org.mockito.MockitoAnnotations; 19 | import org.mockito.invocation.InvocationOnMock; 20 | import org.mockito.stubbing.Answer; 21 | import org.powermock.api.mockito.PowerMockito; 22 | import org.powermock.core.classloader.annotations.PrepareForTest; 23 | import org.powermock.modules.junit4.PowerMockRunner; 24 | 25 | import static org.assertj.core.api.Assertions.assertThat; 26 | import static org.assertj.core.api.Assertions.fail; 27 | import static org.mockito.Matchers.any; 28 | import static org.mockito.Matchers.anyListOf; 29 | import static org.mockito.Matchers.anyString; 30 | import static org.mockito.Mockito.mock; 31 | import static org.mockito.Mockito.never; 32 | import static org.mockito.Mockito.verify; 33 | import static org.mockito.Mockito.when; 34 | 35 | /** 36 | * Created by chris on 14/07/15. 37 | * For SharedContentProviders. 38 | */ 39 | @RunWith(PowerMockRunner.class) @PrepareForTest({ TextUtils.class, Log.class }) 40 | public class SharedProviderFinderTest { 41 | 42 | @Mock SharedProviderFinder sharedProviderFinder; 43 | @Mock ContentResolver contentResolver; 44 | @Mock Cursor cursor; 45 | 46 | @Before public void setUp() throws Exception { 47 | MockitoAnnotations.initMocks(this); 48 | PowerMockito.mockStatic(TextUtils.class); 49 | PowerMockito.mockStatic(Log.class); 50 | //when(TextUtils.isEmpty(null)).thenReturn(true); 51 | when(TextUtils.isEmpty(anyString())).thenAnswer(new Answer() { 52 | @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { 53 | final String s = invocation.getArgumentAt(0, String.class); 54 | return s == null || "".equalsIgnoreCase(s); 55 | } 56 | }); 57 | when(Log.d(anyString(), anyString())).thenReturn(0); 58 | //new SharedProviderFinder(mock(Context.class), Pattern.compile("com\\.test\\.provider"), 59 | // "com.test.PERMISSION", contentResolver); 60 | 61 | when(sharedProviderFinder.getContentUri(any(String.class))).thenReturn(Uri.EMPTY); 62 | when(sharedProviderFinder.isProviderMaster(Uri.EMPTY, contentResolver)).thenCallRealMethod(); 63 | } 64 | 65 | @After public void tearDown() throws Exception { 66 | sharedProviderFinder = null; 67 | contentResolver = null; 68 | cursor = null; 69 | } 70 | 71 | @Test public void testFindProviders_nullProvider() throws Exception { 72 | final ProviderInfo providerInfo = mock(ProviderInfo.class); 73 | providerInfo.authority = null; 74 | // Pass in a bad ProviderInfo 75 | when(sharedProviderFinder.getInstalledProviders()).thenReturn( 76 | Collections.singletonList(providerInfo)); 77 | when(sharedProviderFinder.getAuthorityMatcherPattern()).thenReturn( 78 | Pattern.compile("com\\.owlr\\.test")); 79 | when(sharedProviderFinder.findProviders()).thenCallRealMethod(); 80 | 81 | List results = sharedProviderFinder.findProviders(); 82 | assertThat(results).isNotNull().isEmpty(); 83 | } 84 | 85 | @Test public void testFindProviders_findTestProvider() throws Exception { 86 | final ProviderInfo providerInfo = mock(ProviderInfo.class); 87 | providerInfo.authority = "com.owlr.test"; 88 | providerInfo.writePermission = "com.owlr.test.PERMISSION"; 89 | 90 | // Pass in a normal ProviderInfo 91 | when(sharedProviderFinder.getInstalledProviders()).thenReturn( 92 | Collections.singletonList(providerInfo)); 93 | when(sharedProviderFinder.getAuthorityMatcherPattern()).thenReturn( 94 | Pattern.compile("com\\.owlr\\.test")); 95 | when(sharedProviderFinder.getSharedPermission()).thenReturn("com.owlr.test.PERMISSION"); 96 | when(sharedProviderFinder.findProviders()).thenCallRealMethod(); 97 | 98 | List results = sharedProviderFinder.findProviders(); 99 | assertThat(results).isNotNull().hasSize(1); 100 | } 101 | 102 | @Test public void testFindProviders_badProviderDifferentPermission() throws Exception { 103 | final ProviderInfo providerInfo = mock(ProviderInfo.class); 104 | providerInfo.authority = "com.owlr.test"; 105 | providerInfo.writePermission = "com.owlr.test.OTHER_PERMISSION"; 106 | 107 | // Pass in a normal ProviderInfo 108 | when(sharedProviderFinder.getInstalledProviders()).thenReturn( 109 | Collections.singletonList(providerInfo)); 110 | when(sharedProviderFinder.getAuthorityMatcherPattern()).thenReturn( 111 | Pattern.compile("com\\.owlr\\.test")); 112 | when(sharedProviderFinder.getSharedPermission()).thenReturn("com.owlr.test.PERMISSION"); 113 | when(sharedProviderFinder.findProviders()).thenCallRealMethod(); 114 | 115 | List results = sharedProviderFinder.findProviders(); 116 | assertThat(results).isNotNull().hasSize(0); 117 | } 118 | 119 | @Test public void testFindMasterProvider_throwException() throws Exception { 120 | when(sharedProviderFinder.findMasterProvider( 121 | anyListOf(ProviderInfo.class))).thenCallRealMethod(); 122 | try { 123 | sharedProviderFinder.findMasterProvider(null); 124 | } catch (Exception e) { 125 | assertThat(e).isInstanceOf(IllegalStateException.class); 126 | return; 127 | } 128 | fail("Should throw IllegalStateException"); 129 | } 130 | 131 | @Test public void testFindMasterProvider_throwException2() throws Exception { 132 | when(sharedProviderFinder.findMasterProvider( 133 | anyListOf(ProviderInfo.class))).thenCallRealMethod(); 134 | try { 135 | sharedProviderFinder.findMasterProvider(Collections.emptyList()); 136 | } catch (Exception e) { 137 | assertThat(e).isInstanceOf(IllegalStateException.class); 138 | return; 139 | } 140 | fail("Should throw IllegalStateException"); 141 | } 142 | 143 | @Test public void testFindMasterProvider_throwException3_nullProviders() throws Exception { 144 | when(sharedProviderFinder.findMasterProvider( 145 | anyListOf(ProviderInfo.class))).thenCallRealMethod(); 146 | final ProviderInfo providerInfo = mock(ProviderInfo.class); 147 | providerInfo.authority = null; 148 | 149 | // Passed in provider is not Master it should get delegated. 150 | //when(contentResolver.query(Uri.EMPTY, null, null, null, null)).thenReturn(null); 151 | 152 | try { 153 | sharedProviderFinder.findMasterProvider(Collections.singletonList(providerInfo)); 154 | } catch (Exception e) { 155 | assertThat(e).isInstanceOf(IllegalStateException.class); 156 | return; 157 | } 158 | fail("Should throw IllegalStateException"); 159 | } 160 | 161 | @Test public void testFindMasterProvider_assignThisProvider() throws Exception { 162 | final String auth = "com.owlr.test.provider"; 163 | 164 | when(sharedProviderFinder.findMasterProvider( 165 | anyListOf(ProviderInfo.class))).thenCallRealMethod(); 166 | final ProviderInfo providerInfo = mock(ProviderInfo.class); 167 | providerInfo.authority = auth; 168 | 169 | // Passed in provider is not Master it should get delegated. 170 | when(contentResolver.query(Uri.EMPTY, null, null, null, null)).thenReturn(null); 171 | when(sharedProviderFinder.delegateMaster(auth, true)).thenReturn(auth); 172 | final String masterProvider = 173 | sharedProviderFinder.findMasterProvider(Collections.singletonList(providerInfo)); 174 | 175 | //Should of tried to delegate this. 176 | verify(sharedProviderFinder, never()).delegateMaster(auth, false); 177 | verify(sharedProviderFinder).delegateMaster(auth, true); 178 | assertThat(masterProvider).isEqualTo(auth); 179 | } 180 | 181 | @Test public void testFindMasterProvider_multipleProviders_assignThisProvider() throws Exception { 182 | final String auth = "com.owlr.test.provider"; 183 | final String auth2 = "com.owlr.test2.provider"; 184 | 185 | when(sharedProviderFinder.findMasterProvider( 186 | anyListOf(ProviderInfo.class))).thenCallRealMethod(); 187 | final ProviderInfo providerInfo = mock(ProviderInfo.class); 188 | final ProviderInfo providerInfo2 = mock(ProviderInfo.class); 189 | providerInfo.authority = auth; 190 | providerInfo2.authority = auth2; 191 | 192 | // Passed in providers are not set to master. 193 | when(contentResolver.query(Uri.EMPTY, null, null, null, null)).thenReturn(null); 194 | when(sharedProviderFinder.delegateMaster(auth, true)).thenReturn(auth); 195 | final String masterProvider = 196 | sharedProviderFinder.findMasterProvider(Arrays.asList(providerInfo, providerInfo2)); 197 | 198 | //Should of tried to delegate this. 199 | verify(sharedProviderFinder, never()).delegateMaster(auth, false); 200 | verify(sharedProviderFinder, never()).delegateMaster(auth2, false); 201 | verify(sharedProviderFinder).delegateMaster(auth, true); 202 | assertThat(masterProvider).isEqualTo(auth); 203 | } 204 | 205 | @Test public void testFindMasterProvider_secondIsAlreadyMaster() throws Exception { 206 | final String auth = "com.owlr.test.provider"; 207 | final String auth2 = "com.owlr.test2.provider"; 208 | 209 | when(sharedProviderFinder.findMasterProvider( 210 | anyListOf(ProviderInfo.class))).thenCallRealMethod(); 211 | final ProviderInfo providerInfo = mock(ProviderInfo.class); 212 | final ProviderInfo providerInfo2 = mock(ProviderInfo.class); 213 | providerInfo.authority = auth; 214 | providerInfo2.authority = auth2; 215 | 216 | // Auth1 is not master, auth2 is. 217 | when(sharedProviderFinder.isProviderMaster(any(Uri.class), 218 | any(ContentResolver.class))).thenReturn(false, true); 219 | final String masterProvider = 220 | sharedProviderFinder.findMasterProvider(Arrays.asList(providerInfo, providerInfo2)); 221 | 222 | //Should of tried to delegate this. 223 | verify(sharedProviderFinder, never()).delegateMaster(auth, false); 224 | verify(sharedProviderFinder, never()).delegateMaster(auth2, false); 225 | verify(sharedProviderFinder, never()).delegateMaster(auth2, true); 226 | assertThat(masterProvider).isEqualTo(auth2); 227 | } 228 | 229 | @Test public void testFindMasterProvider_bothAreMaster() throws Exception { 230 | final String auth = "com.owlr.test.provider"; 231 | final String auth2 = "com.owlr.test2.provider"; 232 | 233 | when(sharedProviderFinder.findMasterProvider( 234 | anyListOf(ProviderInfo.class))).thenCallRealMethod(); 235 | final ProviderInfo providerInfo = mock(ProviderInfo.class); 236 | final ProviderInfo providerInfo2 = mock(ProviderInfo.class); 237 | providerInfo.authority = auth; 238 | providerInfo2.authority = auth2; 239 | 240 | // Auth1 is not master, auth2 is. 241 | when(sharedProviderFinder.isProviderMaster(any(Uri.class), 242 | any(ContentResolver.class))).thenReturn(true); 243 | final String masterProvider = 244 | sharedProviderFinder.findMasterProvider(Arrays.asList(providerInfo, providerInfo2)); 245 | 246 | //Should of tried to delegate this. 247 | verify(sharedProviderFinder, never()).delegateMaster(auth, false); 248 | verify(sharedProviderFinder).delegateMaster(auth2, false); 249 | verify(sharedProviderFinder, never()).delegateMaster(auth, true); 250 | verify(sharedProviderFinder, never()).delegateMaster(auth2, true); 251 | assertThat(masterProvider).isEqualTo(auth); 252 | } 253 | 254 | @Test public void testIsProviderMaster_notSet_returnFalse() throws Exception { 255 | when(cursor.moveToFirst()).thenReturn(false); 256 | when(contentResolver.query(Uri.EMPTY, null, null, null, null)).thenReturn(cursor); 257 | final boolean master = sharedProviderFinder.isProviderMaster(Uri.EMPTY, contentResolver); 258 | assertThat(master).isFalse(); 259 | } 260 | 261 | @Test public void testIsProviderMaster_isFalse_returnFalse() throws Exception { 262 | when(cursor.moveToFirst()).thenReturn(true); 263 | when(cursor.getInt(0)).thenReturn(0); 264 | when(contentResolver.query(Uri.EMPTY, null, null, null, null)).thenReturn(cursor); 265 | final boolean master = sharedProviderFinder.isProviderMaster(Uri.EMPTY, contentResolver); 266 | assertThat(master).isFalse(); 267 | } 268 | 269 | @Test public void testIsProviderMaster_isTrue_returnTrue() throws Exception { 270 | when(cursor.moveToFirst()).thenReturn(true); 271 | when(cursor.getInt(0)).thenReturn(1); 272 | when(contentResolver.query(Uri.EMPTY, null, null, null, null)).thenReturn(cursor); 273 | final boolean master = sharedProviderFinder.isProviderMaster(Uri.EMPTY, contentResolver); 274 | assertThat(master).isTrue(); 275 | } 276 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':provider', ':app1', ':app2' 2 | --------------------------------------------------------------------------------