protected AN_Intent GenerateChooserIntent(string title, params string[] filters) { if (filters.Length == 0) { return(AN_Intent.CreateChooser(ShareIntent, title)); } //Simple intent to get list of the apps that can support the send action var testIntent = new AN_Intent(); testIntent.SetAction(AN_Intent.ACTION_SEND); testIntent.SetType("text/plain"); var pm = AN_MainActivity.Instance.GetPackageManager(); var resolveInfoList = pm.QueryIntentActivities(testIntent); var intentShareList = new List <AN_Intent>(); foreach (var resInfo in resolveInfoList) { var packageName = resInfo.ActivityInfo.PackageName; foreach (var filterPattern in filters) { if (resInfo.ActivityInfo.PackageName.ToLower().Contains(filterPattern) || resInfo.ActivityInfo.Name.ToLower().Contains(filterPattern)) { //TODO do we need full data or only package name var intent = new AN_Intent(ShareIntent); intent.SetPackage(packageName); intentShareList.Add(intent); break; } } } if (intentShareList.Count == 0) { //we can't find packages for a provided filters, so we will use standard chooser string filterList = ""; foreach (var f in filters) { filterList += f + ";"; } AN_Logger.Log("Wasn't able to find packages for filters: " + filterList); return(AN_Intent.CreateChooser(ShareIntent, title)); } else { AN_Logger.Log("Chooser created with options count: " + intentShareList.Count); //if we have only 1 option there is no point to create hole chooser UI. Let's just use this option if (intentShareList.Count == 1) { return(intentShareList[0]); } return(AN_Intent.CreateChooser(ShareIntent, title, intentShareList.ToArray())); } }
void ShowSharingMenuAndroid(Texture2D imageToShare) { var Title = "Test Title"; var AppLink = "Test AppLink"; var PrioritizedApps = new List <string> { "whatsapp", "facebook" }; AN_Intent shareIntent = new AN_Intent(); shareIntent.SetAction(AN_Intent.ACTION_SEND); shareIntent.AddFlags(AN_Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.SetType(AN_MIMEDataType.Image); string sharingMessageText = "Sharing message text."; sharingMessageText = sharingMessageText + "\n\n" + AppLink; shareIntent.PutExtra(AN_Intent.EXTRA_TEXT, sharingMessageText); shareIntent.PutExtra(AN_Intent.EXTRA_STREAM, imageToShare); var pm = AN_MainActivity.Instance.GetPackageManager(); List <AN_ResolveInfo> resolveInfoList = pm.QueryIntentActivities(shareIntent, 0); if (resolveInfoList.Count == 0) { AN_Logger.Log("Could not find any app for sharing."); return; } List <AN_Intent> intentShareList = new List <AN_Intent>(); // Try to find apps from priority list to put them on the first positions foreach (string appName in PrioritizedApps) { foreach (AN_ResolveInfo resInfo in resolveInfoList) { string packageName = resInfo.ActivityInfo.PackageName; string name = resInfo.ActivityInfo.Name; if (resInfo.ActivityInfo.PackageName.ToLower().Contains(appName) || resInfo.ActivityInfo.Name.ToLower().Contains(appName)) { AN_Intent intent = new AN_Intent(shareIntent); intent.SetPackage(packageName); intentShareList.Add(intent); resolveInfoList.Remove(resInfo); break; } } } // Put all the others apps after prioritized ones for (int resInfoIndex = 0; resInfoIndex < resolveInfoList.Count; resInfoIndex++) { AN_ResolveInfo resInfo = resolveInfoList[resInfoIndex]; string packageName = resInfo.ActivityInfo.PackageName; string name = resInfo.ActivityInfo.Name; if (resInfoIndex != resolveInfoList.Count - 1) { AN_Intent intent = new AN_Intent(shareIntent); intent.SetPackage(packageName); intentShareList.Add(intent); } else { shareIntent.SetPackage(packageName); } } if (intentShareList.Count == 0) { AN_Logger.Log("Could not find any app for sharing."); } else { AN_Logger.Log("proxy.StartActivityForResult"); AN_Intent chooserIntent = AN_Intent.CreateChooser(shareIntent, Title, intentShareList.ToArray()); /* AN_ProxyActivity proxy = new AN_ProxyActivity(); * proxy.StartActivityForResult(chooserIntent, (result) => { * Debug.Log("Unity: Activity Result: " + result.ResultCode.ToString()); * proxy.Finish(); * });*/ AN_MainActivity.Instance.StartActivity(chooserIntent); } }
public static void StartFilteredChooserActivity(Texture2D screenshot) { AN_Intent shareIntent = new AN_Intent(); shareIntent.SetAction(AN_Intent.ACTION_SEND_MULTIPLE); shareIntent.SetType(AN_MIMEDataType.Image); shareIntent.PutExtra(AN_Intent.EXTRA_TEXT, "This is my text to send."); shareIntent.PutExtra(AN_Intent.EXTRA_EMAIL, "*****@*****.**", "*****@*****.**"); shareIntent.PutExtra(AN_Intent.EXTRA_TEXT, "EXTRA_TEXT2"); shareIntent.PutExtra(AN_Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT2s"); var list = new List <Texture2D>() { screenshot, screenshot }; shareIntent.PutExtra(AN_Intent.EXTRA_STREAM, list.ToArray()); List <string> filters = new List <string>(); filters.Add("mail"); var pm = AN_MainActivity.Instance.GetPackageManager(); List <AN_ResolveInfo> resolveInfoList = pm.QueryIntentActivities(shareIntent, 0); if (resolveInfoList.Count == 0) { Debug.Log("No apps found"); return; } List <AN_Intent> intentShareList = new List <AN_Intent>(); foreach (AN_ResolveInfo resInfo in resolveInfoList) { string packageName = resInfo.ActivityInfo.PackageName; string name = resInfo.ActivityInfo.Name; foreach (string filterPattern in filters) { AN_Logger.Log(resInfo.ActivityInfo.PackageName); if (resInfo.ActivityInfo.PackageName.ToLower().Contains(filterPattern) || resInfo.ActivityInfo.Name.ToLower().Contains(filterPattern)) { AN_Intent intent = new AN_Intent(shareIntent); intent.SetPackage(packageName); // intent.setComponent(new ComponentName(packageName, name)); intentShareList.Add(intent); AN_Logger.Log("packageName: " + packageName); shareIntent.SetPackage(packageName); } } } if (intentShareList.Count == 0) { AN_Logger.Log("CAN'T FIND PACKAGES FOR FILTER: " + filters); } else { AN_Logger.Log("SHARE WITH FILTER: " + filters); AN_Intent chooserIntent = AN_Intent.CreateChooser(shareIntent, "Hello title", intentShareList.ToArray()); AN_MainActivity.Instance.StartActivity(chooserIntent); } }