├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── xyz │ │ └── belvi │ │ └── intentmanipsample │ │ ├── MainActivity.java │ │ └── Sample.java │ └── res │ ├── layout │ ├── activity_intent_manip_sample.xml │ └── activity_main__intent_manip_sample.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── intentmanip ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── xyz │ │ └── belvi │ │ └── intentmanip │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── xyz │ │ │ └── belvi │ │ │ └── intentmanip │ │ │ ├── IntentUtils │ │ │ ├── CategorisedIntent.java │ │ │ ├── IntentAppend.java │ │ │ ├── IntentCallBack │ │ │ │ └── ResolvedIntentListener.java │ │ │ ├── IntentIgnore.java │ │ │ ├── IntentLookUp.java │ │ │ ├── ManipUtils.java │ │ │ ├── MergeIntent.java │ │ │ ├── Models │ │ │ │ ├── PreferenceType.java │ │ │ │ ├── PreparedIntent.java │ │ │ │ ├── ResolveCategory.java │ │ │ │ └── ResolveIntent.java │ │ │ ├── PreferenceIntent.java │ │ │ └── TargetIntent.java │ │ │ └── LaunchIntent.java │ └── res │ │ ├── menu │ │ └── category_menu.xml │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── xyz │ └── belvi │ └── intentmanip │ └── ExampleUnitTest.java ├── license ├── projectFilesBackup └── .idea │ └── workspace.xml ├── sample.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | IntentManip -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 1151 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | Android 39 | 40 | 41 | Android > Lint > Correctness 42 | 43 | 44 | Android > Lint > Performance 45 | 46 | 47 | Android > Lint > Security 48 | 49 | 50 | Code maturity issuesJava 51 | 52 | 53 | Code style issuesJava 54 | 55 | 56 | Data flow issuesJava 57 | 58 | 59 | Error handlingGroovy 60 | 61 | 62 | Error handlingJava 63 | 64 | 65 | Groovy 66 | 67 | 68 | Java 69 | 70 | 71 | Logging issuesJava 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 93 | 94 | 95 | 96 | 97 | 1.8 98 | 99 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IntentManip 2 | Gives more controls over implicit intents creation and the way it is presented to users. [See this](http://belvi.xyz/posts/Handling-Intents) for a more detailed explanation on why you should consider using this library. 3 | 4 | [![](https://jitpack.io/v/KingsMentor/IntentManip.svg)](https://jitpack.io/#KingsMentor/IntentManip) 5 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0) 6 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-IntentManip-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/4490) 7 | 8 | ![Lib Sample](https://github.com/KingsMentor/IntentManip/blob/master/sample.gif) 9 | 10 | # Adding to your project. 11 | 12 | ### step 1 13 | Add it in your root build.gradle at the end of repositories: 14 | ``` 15 | repositories { 16 | ... 17 | maven { url "https://jitpack.io" } 18 | } 19 | ``` 20 | 21 | ### step 2 22 | Add the dependency 23 | ``` 24 | dependencies { 25 | compile 'com.github.KingsMentor:IntentManip:v1.0' 26 | } 27 | ``` 28 | # Usage : 29 | There are a couple of operations you can do with the library. It includes: 30 | 31 | 1. Merging 32 | 2. Categorizing Intent 33 | 3. Appending Intent 34 | 4. Ignoring Component 35 | 5. Lookup Component 36 | 6. Target Component 37 | 7. Component Preference 38 | 39 | # Snippets : 40 | 41 | #### Merging - merging components of different intents 42 | ```java 43 | List resolveIntentList = mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 44 | LaunchIntent.withButtomSheetAsList(this, resolveIntentList, "launch using", new ResolvedIntentListener() { 45 | @Override 46 | public void onIntentSelected(ResolveIntent resolveIntent) { 47 | 48 | } 49 | }); 50 | ``` 51 | ##### Note 52 | 53 | *launching* *intent* *from* *resolveIntent* 54 | 55 | ```java 56 | startActivity(ManipUtils.getLaunchableIntent(resolveIntent)); 57 | ``` 58 | 59 | implement `ResolvedIntentListener` to get the item the user selected from the list 60 | 61 | #### Categorising Components 62 | 63 | for presenting merged intents in a categorised format 64 | 65 | ```java 66 | ResolveCategory pixResolveCategory = CategorisedIntent.categorized(this, MediaIntents.newSelectPictureIntent(), "picture", 1); 67 | List merge = new MergeIntent().mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 68 | ResolveCategory mergeResolveCategory = CategorisedIntent.categorized(merge, "Geo and Media", 2); 69 | List resolveCategories = new ArrayList<>(); 70 | resolveCategories.add(pixResolveCategory); 71 | resolveCategories.add(mergeResolveCategory); 72 | LaunchIntent.categorised(this, resolveCategories, "Share With", new ResolvedIntentListener() { 73 | @Override 74 | public void onIntentSelected(ResolveIntent resolveIntent) { 75 | startActivity(ManipUtils.getLaunchableIntent(resolveIntent)); 76 | } 77 | }); 78 | 79 | ``` 80 | 81 | #### Appending Explicit Intents. 82 | for adding explicit intent ( probably activities from your project ) to implicit components. 83 | 84 | ```java 85 | PreparedIntent preparedIntent = new PreparedIntent(new Intent(this, Sample.class), R.string.sample, R.mipmap.ic_launcher); 86 | List resolveIntentList = IntentAppend.appendCustomIntent(this, MediaIntents.newSelectPictureIntent(), preparedIntent); 87 | LaunchIntent.withButtomSheetAsList(this, resolveIntentList, "launch using", new ResolvedIntentListener() { 88 | @Override 89 | public void onIntentSelected(ResolveIntent resolveIntent) { 90 | startActivity(ManipUtils.getLaunchableIntent(resolveIntent)); 91 | } 92 | }); 93 | ``` 94 | #### Ignore Components 95 | to ignore or skip or remove components that matches a defined naming pattern from the list 96 | 97 | ```java 98 | List resolveIntentList = mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 99 | IntentIgnore.IgnoreIntentWithName(this, resolveIntentList, new ArrayList(Arrays.asList(new String[]{"Maps"}))); 100 | LaunchIntent.withButtomSheetAsList(this, resolveIntentList, "launch using", new ResolvedIntentListener() { 101 | @Override 102 | public void onIntentSelected(Object resolveIntent) { 103 | 104 | } 105 | }); 106 | ``` 107 | 108 | #### LookingUp Components 109 | to find and return components that matches a defined naming pattern 110 | ```java 111 | List resolveIntentList = mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 112 | IntentLookUp.lookUpAppsByAppName(this, resolveIntentList, "Maps"); 113 | LaunchIntent.withButtomSheetAsList(this, resolveIntentList, "launch using", new ResolvedIntentListener() { 114 | @Override 115 | public void onIntentSelected(Object resolveIntent) { 116 | 117 | } 118 | }); 119 | ``` 120 | 121 | #### Target a Component 122 | to target a particular component from a list of component. 123 | returns `null` if not found or return the first item if more than 1 item is found. 124 | ```java 125 | List resolveIntentList = mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 126 | ResolveIntent resolveIntent = TargetIntent.targetByAppName(this, resolveIntentList, "Photo"); 127 | if (resolveIntent != null) { 128 | startActivity(ManipUtils.getLaunchableIntent(resolveIntent)); 129 | } 130 | ``` 131 | 132 | #### Component Preference 133 | This involves using a `PreferenceType` which could be 134 | 135 | * ASCENDING - to arange components in an alphabetical order by appname 136 | * DECENDING - to arange components in a decending alphabetical order by app name 137 | * CUSTOM_PACKAGENAME - arrange the list, placing components with supplied package names aboved others. 138 | * CUSTOM_APPNAME - arrange the list, placing components with supplied app names aboved others. 139 | * CUSTOM_REGEX_APPNAME - arrange the list, placing components with app names matching regEx supplied aboved others. 140 | * CUSTOM_REGEX_PACKAGE_NAME - arrange the list, placing components with package names matching regEx supplied aboved others. 141 | 142 | Also **note** that CUSTOM Preference type sort the component in same manage the list was provided. 143 | 144 | ```java 145 | List resolveIntentList = mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 146 | PreferenceIntent.preferredIntent(this, PreferenceType.CUSTOM_APPNAME, new ArrayList(Arrays.asList(new String[]{"Maps","Photo"})), resolveIntentList); 147 | LaunchIntent.withButtomSheetAsList(this, resolveIntentList, "launch using", new ResolvedIntentListener() { 148 | @Override 149 | public void onIntentSelected(Object resolveIntent) { 150 | 151 | } 152 | }); 153 | ``` 154 | 155 | The snippet above will sort the merge component of `MediaIntents` and `GeoIntents` but would place Maps and Photo above the entire list. 156 | Maps will be above Photos because if was provided that way. To bring Photos above Maps, it has to be this way: 157 | 158 | ```java 159 | PreferenceIntent.preferredIntent(this, PreferenceType.CUSTOM_APPNAME, new ArrayList(Arrays.asList(new String[]{"Photo","Maps"})), resolveIntentList); 160 | ``` 161 | 162 | ## Contributing 163 | Contributions are welcome. 164 | 165 | 166 | 167 | ## Credits 168 | [android-intents](https://github.com/marvinlabs/android-intents) and 169 | [Bottom Sheet](https://github.com/soarcn/BottomSheet) upon which this library is based. 170 | 171 | ## License 172 | 173 | ``` 174 | Copyright (C) 2016 MarvinLabs 175 | Copyright (C) 2011, 2015 Kai Liao 176 | 177 | Licensed under the Apache License, Version 2.0 (the "License"); 178 | you may not use this file except in compliance with the License. 179 | You may obtain a copy of the License at 180 | 181 | http://www.apache.org/licenses/LICENSE-2.0 182 | 183 | Unless required by applicable law or agreed to in writing, software 184 | distributed under the License is distributed on an "AS IS" BASIS, 185 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 186 | See the License for the specific language governing permissions and 187 | limitations under the License. 188 | ``` 189 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.0" 6 | 7 | defaultConfig { 8 | applicationId "xyz.belvi.intentmanip" 9 | minSdkVersion 15 10 | targetSdkVersion 24 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | repositories { 22 | maven { url "https://jitpack.io" } 23 | } 24 | 25 | 26 | dependencies { 27 | compile fileTree(include: ['*.jar'], dir: 'libs') 28 | compile 'com.android.support:appcompat-v7:24.2.1' 29 | compile 'com.android.support:design:24.2.1' 30 | testCompile 'junit:junit:4.12' 31 | // compile 'com.github.KingsMentor:intentmanip:v1.0' 32 | compile project(':intentmanip') 33 | } 34 | -------------------------------------------------------------------------------- /app/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 /Users/zone2/Library/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 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/xyz/belvi/intentmanipsample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package xyz.belvi.intentmanipsample; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.AdapterView; 8 | import android.widget.ArrayAdapter; 9 | import android.widget.ListView; 10 | 11 | import com.marvinlabs.intents.GeoIntents; 12 | import com.marvinlabs.intents.MediaIntents; 13 | 14 | import java.util.ArrayList; 15 | import java.util.Arrays; 16 | import java.util.List; 17 | 18 | import xyz.belvi.intentmanip.IntentUtils.CategorisedIntent; 19 | import xyz.belvi.intentmanip.IntentUtils.IntentAppend; 20 | import xyz.belvi.intentmanip.IntentUtils.IntentCallBack.ResolvedIntentListener; 21 | import xyz.belvi.intentmanip.IntentUtils.IntentIgnore; 22 | import xyz.belvi.intentmanip.IntentUtils.IntentLookUp; 23 | import xyz.belvi.intentmanip.IntentUtils.ManipUtils; 24 | import xyz.belvi.intentmanip.IntentUtils.MergeIntent; 25 | import xyz.belvi.intentmanip.IntentUtils.Models.PreferenceType; 26 | import xyz.belvi.intentmanip.IntentUtils.Models.PreparedIntent; 27 | import xyz.belvi.intentmanip.IntentUtils.Models.ResolveCategory; 28 | import xyz.belvi.intentmanip.IntentUtils.Models.ResolveIntent; 29 | import xyz.belvi.intentmanip.IntentUtils.PreferenceIntent; 30 | import xyz.belvi.intentmanip.IntentUtils.TargetIntent; 31 | import xyz.belvi.intentmanip.LaunchIntent; 32 | 33 | import static xyz.belvi.intentmanip.IntentUtils.MergeIntent.mergeIntents; 34 | 35 | 36 | public class MainActivity extends AppCompatActivity { 37 | 38 | private final int MERGE = 0; 39 | private final int CATEGORISE = 1; 40 | private final int APPENDING = 2; 41 | private final int IGNORE = 3; 42 | private final int LOOKUP = 4; 43 | private final int PREF = 5; 44 | private final int TARGET = 6; 45 | 46 | @Override 47 | protected void onCreate(Bundle savedInstanceState) { 48 | super.onCreate(savedInstanceState); 49 | setContentView(R.layout.activity_main__intent_manip_sample); 50 | ListView listView = (ListView) findViewById(R.id.intent_list); 51 | listView.setAdapter(ArrayAdapter.createFromResource(this, R.array.manip_list, android.R.layout.simple_list_item_1)); 52 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 53 | @Override 54 | public void onItemClick(AdapterView adapterView, View view, int position, long l) { 55 | switch (position) { 56 | case MERGE: 57 | runMerge(); 58 | break; 59 | case CATEGORISE: 60 | categorised(); 61 | break; 62 | case APPENDING: 63 | appendIntent(); 64 | break; 65 | case IGNORE: 66 | ignore(); 67 | break; 68 | case LOOKUP: 69 | lookUp(); 70 | break; 71 | case PREF: 72 | arrangeInPreference(); 73 | break; 74 | case TARGET: 75 | target(); 76 | 77 | } 78 | } 79 | }); 80 | } 81 | 82 | 83 | private void runMerge() { 84 | List resolveIntentList = mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 85 | LaunchIntent.withButtomSheetAsList(this, resolveIntentList, "launch using", new ResolvedIntentListener() { 86 | @Override 87 | public void onIntentSelected(Object resolveIntent) { 88 | 89 | } 90 | }); 91 | } 92 | 93 | 94 | private void categorised() { 95 | ResolveCategory pixResolveCategory = CategorisedIntent.categorized(this, MediaIntents.newSelectPictureIntent(), "picture", 1); 96 | List merge = new MergeIntent().mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 97 | ResolveCategory mergeResolveCategory = CategorisedIntent.categorized(merge, "Geo and Media", 2); 98 | List resolveCategories = new ArrayList<>(); 99 | resolveCategories.add(pixResolveCategory); 100 | resolveCategories.add(mergeResolveCategory); 101 | LaunchIntent.categorised(this, resolveCategories, "Share With", new ResolvedIntentListener() { 102 | @Override 103 | public void onIntentSelected(ResolveIntent resolveIntent) { 104 | startActivity(ManipUtils.getLaunchableIntent(resolveIntent)); 105 | } 106 | }); 107 | } 108 | 109 | private void appendIntent() { 110 | PreparedIntent preparedIntent = new PreparedIntent(new Intent(this, Sample.class), R.string.sample, R.mipmap.ic_launcher); 111 | List resolveIntentList = IntentAppend.appendCustomIntent(this, MediaIntents.newSelectPictureIntent(), preparedIntent); 112 | LaunchIntent.withButtomSheetAsList(this, resolveIntentList, "launch using", new ResolvedIntentListener() { 113 | @Override 114 | public void onIntentSelected(ResolveIntent resolveIntent) { 115 | startActivity(ManipUtils.getLaunchableIntent(resolveIntent)); 116 | } 117 | }); 118 | } 119 | 120 | 121 | private void ignore() { 122 | List resolveIntentList = mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 123 | IntentIgnore.IgnoreIntentWithName(this, resolveIntentList, new ArrayList(Arrays.asList(new String[]{"Maps"}))); 124 | LaunchIntent.withButtomSheetAsList(this, resolveIntentList, "launch using", new ResolvedIntentListener() { 125 | @Override 126 | public void onIntentSelected(Object resolveIntent) { 127 | 128 | } 129 | }); 130 | } 131 | 132 | 133 | private void lookUp() { 134 | List resolveIntentList = mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 135 | IntentLookUp.lookUpAppsByAppName(this, resolveIntentList, new ArrayList(Arrays.asList("Maps"))); 136 | LaunchIntent.withButtomSheetAsList(this, resolveIntentList, "launch using", new ResolvedIntentListener() { 137 | @Override 138 | public void onIntentSelected(Object resolveIntent) { 139 | 140 | } 141 | }); 142 | } 143 | 144 | 145 | private void arrangeInPreference() { 146 | List resolveIntentList = mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 147 | PreferenceIntent.preferredIntent(this, PreferenceType.CUSTOM_APPNAME, new ArrayList(Arrays.asList(new String[]{"Maps", "Photo"})), resolveIntentList); 148 | LaunchIntent.withButtomSheetAsList(this, resolveIntentList, "launch using", new ResolvedIntentListener() { 149 | @Override 150 | public void onIntentSelected(Object resolveIntent) { 151 | 152 | } 153 | }); 154 | } 155 | 156 | 157 | private void target() { 158 | List resolveIntentList = mergeIntents(this, MediaIntents.newSelectPictureIntent(), GeoIntents.newNavigationIntent("")); 159 | ResolveIntent resolveIntent = TargetIntent.targetByAppName(this, resolveIntentList, "Photo"); 160 | if (resolveIntent != null) { 161 | startActivity(ManipUtils.getLaunchableIntent(resolveIntent)); 162 | } 163 | 164 | } 165 | 166 | 167 | } 168 | -------------------------------------------------------------------------------- /app/src/main/java/xyz/belvi/intentmanipsample/Sample.java: -------------------------------------------------------------------------------- 1 | package xyz.belvi.intentmanipsample; 2 | 3 | import android.animation.Animator; 4 | import android.animation.AnimatorListenerAdapter; 5 | import android.annotation.TargetApi; 6 | import android.app.LoaderManager.LoaderCallbacks; 7 | import android.content.CursorLoader; 8 | import android.content.Loader; 9 | import android.content.pm.PackageManager; 10 | import android.database.Cursor; 11 | import android.net.Uri; 12 | import android.os.AsyncTask; 13 | import android.os.Build; 14 | import android.os.Bundle; 15 | import android.provider.ContactsContract; 16 | import android.support.annotation.NonNull; 17 | import android.support.design.widget.Snackbar; 18 | import android.support.v7.app.AppCompatActivity; 19 | import android.text.TextUtils; 20 | import android.view.KeyEvent; 21 | import android.view.View; 22 | import android.view.View.OnClickListener; 23 | import android.view.inputmethod.EditorInfo; 24 | import android.widget.ArrayAdapter; 25 | import android.widget.AutoCompleteTextView; 26 | import android.widget.Button; 27 | import android.widget.EditText; 28 | import android.widget.TextView; 29 | 30 | import java.util.ArrayList; 31 | import java.util.List; 32 | 33 | import static android.Manifest.permission.READ_CONTACTS; 34 | 35 | /** 36 | * A login screen that offers login via email/password. 37 | */ 38 | public class Sample extends AppCompatActivity implements LoaderCallbacks { 39 | 40 | /** 41 | * Id to identity READ_CONTACTS permission request. 42 | */ 43 | private static final int REQUEST_READ_CONTACTS = 0; 44 | 45 | /** 46 | * A dummy authentication store containing known user names and passwords. 47 | * TODO: remove after connecting to a real authentication system. 48 | */ 49 | private static final String[] DUMMY_CREDENTIALS = new String[]{ 50 | "foo@example.com:hello", "bar@example.com:world" 51 | }; 52 | /** 53 | * Keep track of the login task to ensure we can cancel it if requested. 54 | */ 55 | private UserLoginTask mAuthTask = null; 56 | 57 | // UI references. 58 | private AutoCompleteTextView mEmailView; 59 | private EditText mPasswordView; 60 | private View mProgressView; 61 | private View mLoginFormView; 62 | 63 | @Override 64 | protected void onCreate(Bundle savedInstanceState) { 65 | super.onCreate(savedInstanceState); 66 | setContentView(R.layout.activity_intent_manip_sample); 67 | // Set up the login form. 68 | mEmailView = (AutoCompleteTextView) findViewById(R.id.email); 69 | populateAutoComplete(); 70 | 71 | mPasswordView = (EditText) findViewById(R.id.password); 72 | mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() { 73 | @Override 74 | public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { 75 | if (id == R.id.login || id == EditorInfo.IME_NULL) { 76 | attemptLogin(); 77 | return true; 78 | } 79 | return false; 80 | } 81 | }); 82 | 83 | Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button); 84 | mEmailSignInButton.setOnClickListener(new OnClickListener() { 85 | @Override 86 | public void onClick(View view) { 87 | attemptLogin(); 88 | } 89 | }); 90 | 91 | mLoginFormView = findViewById(R.id.login_form); 92 | mProgressView = findViewById(R.id.login_progress); 93 | } 94 | 95 | private void populateAutoComplete() { 96 | if (!mayRequestContacts()) { 97 | return; 98 | } 99 | 100 | getLoaderManager().initLoader(0, null, this); 101 | } 102 | 103 | private boolean mayRequestContacts() { 104 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 105 | return true; 106 | } 107 | if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) { 108 | return true; 109 | } 110 | if (shouldShowRequestPermissionRationale(READ_CONTACTS)) { 111 | Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE) 112 | .setAction(android.R.string.ok, new View.OnClickListener() { 113 | @Override 114 | @TargetApi(Build.VERSION_CODES.M) 115 | public void onClick(View v) { 116 | requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS); 117 | } 118 | }); 119 | } else { 120 | requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS); 121 | } 122 | return false; 123 | } 124 | 125 | /** 126 | * Callback received when a permissions request has been completed. 127 | */ 128 | @Override 129 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 130 | @NonNull int[] grantResults) { 131 | if (requestCode == REQUEST_READ_CONTACTS) { 132 | if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 133 | populateAutoComplete(); 134 | } 135 | } 136 | } 137 | 138 | 139 | /** 140 | * Attempts to sign in or register the account specified by the login form. 141 | * If there are form errors (invalid email, missing fields, etc.), the 142 | * errors are presented and no actual login attempt is made. 143 | */ 144 | private void attemptLogin() { 145 | if (mAuthTask != null) { 146 | return; 147 | } 148 | 149 | // Reset errors. 150 | mEmailView.setError(null); 151 | mPasswordView.setError(null); 152 | 153 | // Store values at the time of the login attempt. 154 | String email = mEmailView.getText().toString(); 155 | String password = mPasswordView.getText().toString(); 156 | 157 | boolean cancel = false; 158 | View focusView = null; 159 | 160 | // Check for a valid password, if the user entered one. 161 | if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) { 162 | mPasswordView.setError(getString(R.string.error_invalid_password)); 163 | focusView = mPasswordView; 164 | cancel = true; 165 | } 166 | 167 | // Check for a valid email address. 168 | if (TextUtils.isEmpty(email)) { 169 | mEmailView.setError(getString(R.string.error_field_required)); 170 | focusView = mEmailView; 171 | cancel = true; 172 | } else if (!isEmailValid(email)) { 173 | mEmailView.setError(getString(R.string.error_invalid_email)); 174 | focusView = mEmailView; 175 | cancel = true; 176 | } 177 | 178 | if (cancel) { 179 | // There was an error; don't attempt login and focus the first 180 | // form field with an error. 181 | focusView.requestFocus(); 182 | } else { 183 | // Show a progress spinner, and kick off a background task to 184 | // perform the user login attempt. 185 | showProgress(true); 186 | mAuthTask = new UserLoginTask(email, password); 187 | mAuthTask.execute((Void) null); 188 | } 189 | } 190 | 191 | private boolean isEmailValid(String email) { 192 | //TODO: Replace this with your own logic 193 | return email.contains("@"); 194 | } 195 | 196 | private boolean isPasswordValid(String password) { 197 | //TODO: Replace this with your own logic 198 | return password.length() > 4; 199 | } 200 | 201 | /** 202 | * Shows the progress UI and hides the login form. 203 | */ 204 | @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 205 | private void showProgress(final boolean show) { 206 | // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow 207 | // for very easy animations. If available, use these APIs to fade-in 208 | // the progress spinner. 209 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 210 | int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime); 211 | 212 | mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE); 213 | mLoginFormView.animate().setDuration(shortAnimTime).alpha( 214 | show ? 0 : 1).setListener(new AnimatorListenerAdapter() { 215 | @Override 216 | public void onAnimationEnd(Animator animation) { 217 | mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE); 218 | } 219 | }); 220 | 221 | mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); 222 | mProgressView.animate().setDuration(shortAnimTime).alpha( 223 | show ? 1 : 0).setListener(new AnimatorListenerAdapter() { 224 | @Override 225 | public void onAnimationEnd(Animator animation) { 226 | mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); 227 | } 228 | }); 229 | } else { 230 | // The ViewPropertyAnimator APIs are not available, so simply show 231 | // and hide the relevant UI components. 232 | mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); 233 | mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE); 234 | } 235 | } 236 | 237 | @Override 238 | public Loader onCreateLoader(int i, Bundle bundle) { 239 | return new CursorLoader(this, 240 | // Retrieve data rows for the device user's 'profile' contact. 241 | Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, 242 | ContactsContract.Contacts.Data.CONTENT_DIRECTORY), ProfileQuery.PROJECTION, 243 | 244 | // Select only email addresses. 245 | ContactsContract.Contacts.Data.MIMETYPE + 246 | " = ?", new String[]{ContactsContract.CommonDataKinds.Email 247 | .CONTENT_ITEM_TYPE}, 248 | 249 | // Show primary email addresses first. Note that there won't be 250 | // a primary email address if the user hasn't specified one. 251 | ContactsContract.Contacts.Data.IS_PRIMARY + " DESC"); 252 | } 253 | 254 | @Override 255 | public void onLoadFinished(Loader cursorLoader, Cursor cursor) { 256 | List emails = new ArrayList<>(); 257 | cursor.moveToFirst(); 258 | while (!cursor.isAfterLast()) { 259 | emails.add(cursor.getString(ProfileQuery.ADDRESS)); 260 | cursor.moveToNext(); 261 | } 262 | 263 | addEmailsToAutoComplete(emails); 264 | } 265 | 266 | @Override 267 | public void onLoaderReset(Loader cursorLoader) { 268 | 269 | } 270 | 271 | private void addEmailsToAutoComplete(List emailAddressCollection) { 272 | //Create adapter to tell the AutoCompleteTextView what to show in its dropdown list. 273 | ArrayAdapter adapter = 274 | new ArrayAdapter<>(Sample.this, 275 | android.R.layout.simple_dropdown_item_1line, emailAddressCollection); 276 | 277 | mEmailView.setAdapter(adapter); 278 | } 279 | 280 | 281 | private interface ProfileQuery { 282 | String[] PROJECTION = { 283 | ContactsContract.CommonDataKinds.Email.ADDRESS, 284 | ContactsContract.CommonDataKinds.Email.IS_PRIMARY, 285 | }; 286 | 287 | int ADDRESS = 0; 288 | int IS_PRIMARY = 1; 289 | } 290 | 291 | /** 292 | * Represents an asynchronous login/registration task used to authenticate 293 | * the user. 294 | */ 295 | public class UserLoginTask extends AsyncTask { 296 | 297 | private final String mEmail; 298 | private final String mPassword; 299 | 300 | UserLoginTask(String email, String password) { 301 | mEmail = email; 302 | mPassword = password; 303 | } 304 | 305 | @Override 306 | protected Boolean doInBackground(Void... params) { 307 | // TODO: attempt authentication against a network service. 308 | 309 | try { 310 | // Simulate network access. 311 | Thread.sleep(2000); 312 | } catch (InterruptedException e) { 313 | return false; 314 | } 315 | 316 | for (String credential : DUMMY_CREDENTIALS) { 317 | String[] pieces = credential.split(":"); 318 | if (pieces[0].equals(mEmail)) { 319 | // Account exists, return true if the password matches. 320 | return pieces[1].equals(mPassword); 321 | } 322 | } 323 | 324 | // TODO: register the new account here. 325 | return true; 326 | } 327 | 328 | @Override 329 | protected void onPostExecute(final Boolean success) { 330 | mAuthTask = null; 331 | showProgress(false); 332 | 333 | if (success) { 334 | finish(); 335 | } else { 336 | mPasswordView.setError(getString(R.string.error_incorrect_password)); 337 | mPasswordView.requestFocus(); 338 | } 339 | } 340 | 341 | @Override 342 | protected void onCancelled() { 343 | mAuthTask = null; 344 | showProgress(false); 345 | } 346 | } 347 | } 348 | 349 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_intent_manip_sample.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 21 | 22 | 26 | 27 | 32 | 33 | 36 | 37 | 45 | 46 | 47 | 48 | 51 | 52 | 63 | 64 | 65 | 66 |