|
34 | 34 | import android.support.customtabs.CustomTabsService;
|
35 | 35 | import android.util.DisplayMetrics;
|
36 | 36 |
|
37 |
| - |
38 |
| -@Kroll.module(name="TitaniumWebDialog", id="ti.webdialog") |
| 37 | +@Kroll.module(name = "TitaniumWebDialog", id = "ti.webdialog") |
39 | 38 | public class TitaniumWebDialogModule extends KrollModule
|
40 | 39 | {
|
41 |
| - // Standard Debugging variables |
42 |
| - private static final String LCAT = "TiWebDialog"; |
43 |
| - |
44 |
| - private List<String> getCustomTabBrowsers(Context context, List<ResolveInfo> browsersList) { |
45 |
| - List<String> customTabBrowsers = new ArrayList<String>(); |
46 |
| - |
47 |
| - for (ResolveInfo info : browsersList) { |
48 |
| - String packageName = info.activityInfo.packageName; |
49 |
| - |
50 |
| - Intent intent = new Intent(); |
51 |
| - intent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION); |
52 |
| - intent.setPackage(packageName); |
53 |
| - |
54 |
| - if (context.getPackageManager().resolveService(intent, 0) != null) { |
55 |
| - customTabBrowsers.add(packageName); |
56 |
| - } |
57 |
| - } |
58 |
| - |
59 |
| - return customTabBrowsers; |
60 |
| - } |
61 |
| - |
62 |
| - private void openCustomTab(Context context, List<String> customTabBrowsers, KrollDict options) { |
63 |
| - String URL = options.getString(Params.URL); |
64 |
| - CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); |
65 |
| - builder.setShowTitle(Utils.getBool(options, Params.SHOW_TITLE)); |
66 |
| - |
67 |
| - int barColor = Utils.getColor(options, Params.BAR_COLOR); |
68 |
| - if (barColor != -1) { |
69 |
| - builder.setToolbarColor(barColor); |
70 |
| - } |
71 |
| - |
72 |
| - // set start and exit animations |
73 |
| - if (Utils.getBool(options, Params.FADE_TRANSITION)) { |
74 |
| - builder.setStartAnimations(context, android.R.anim.fade_in, android.R.anim.fade_out); |
75 |
| - builder.setExitAnimations(context, android.R.anim.fade_in, android.R.anim.fade_out); |
76 |
| - } |
77 |
| - |
78 |
| - //enable Share link option |
79 |
| - if (Utils.getBool(options, Params.ENABLE_SHARING)) { |
80 |
| - builder.addDefaultShareMenuItem(); |
81 |
| - } |
82 |
| - |
83 |
| - String closeIcon = Utils.getString(options, Params.CLOSE_ICON); |
84 |
| - if (!closeIcon.isEmpty()) { |
85 |
| - builder.setCloseButtonIcon( getIcon(closeIcon) ); |
86 |
| - } |
87 |
| - |
88 |
| - CustomTabsIntent tabIntent = builder.build(); |
89 |
| - |
90 |
| - for(String s:customTabBrowsers) { |
91 |
| - tabIntent.intent.setPackage(s); |
92 |
| - } |
93 |
| - |
94 |
| - tabIntent.launchUrl(context, Uri.parse(URL)); |
95 |
| - } |
96 |
| - |
97 |
| - |
98 |
| - private Bitmap getIcon(String path) { |
99 |
| - Bitmap resultBitmap = null; |
100 |
| - |
101 |
| - if (path != null && !path.trim().equalsIgnoreCase("")) { |
102 |
| - String resourceIcon = path.replaceAll(".png", ""); |
103 |
| - resourceIcon = "drawable." + resourceIcon; |
104 |
| - |
105 |
| - int resource = Utils.getR(resourceIcon); |
106 |
| - |
107 |
| - if (resource == 0) { |
108 |
| - TiUrl imageUrl = new TiUrl(path); |
109 |
| - TiFileHelper tfh = new TiFileHelper(TiApplication.getInstance()); |
110 |
| - Drawable d = tfh.loadDrawable(imageUrl.resolve(), false); |
111 |
| - |
112 |
| - resultBitmap = ((BitmapDrawable) d).getBitmap(); |
113 |
| - |
114 |
| - } else { |
115 |
| - resultBitmap = BitmapFactory.decodeResource(TiApplication.getAppRootOrCurrentActivity().getResources(), resource); |
116 |
| - } |
117 |
| - } |
118 |
| - |
119 |
| - // important step to show close icon |
120 |
| - // rescale bitmap to 24dp(Height) x 48dp(Width) as mentioned here, otherwise it won't work |
121 |
| - // https://developer.chrome.com/multidevice/android/customtabs#choosing-an icon for the action button |
122 |
| - if (resultBitmap != null) { |
123 |
| - resultBitmap = Utils.rescaleBitmap(TiApplication.getAppRootOrCurrentActivity(), resultBitmap, 24, 48); |
124 |
| - } |
125 |
| - |
126 |
| - return resultBitmap; |
127 |
| - } |
128 |
| - |
129 |
| - |
130 |
| - @Kroll.method |
131 |
| - public void open(KrollDict options) { |
132 |
| - if ((options != null) && options.containsKeyAndNotNull(Params.URL)) { |
133 |
| - Context context = TiApplication.getAppCurrentActivity(); |
134 |
| - List<ResolveInfo> browsersList = Utils.allBrowsers(context); |
135 |
| - |
136 |
| - if (!browsersList.isEmpty()) { |
137 |
| - List<String> customTabBrowsers = getCustomTabBrowsers(context, browsersList); |
138 |
| - |
139 |
| - // show supported browsers list or open directly if only 1 supported browser is available |
140 |
| - openCustomTab(context, customTabBrowsers, options); |
141 |
| - } else { |
142 |
| - Log.i(Params.LCAT, "No browsers available in this device."); |
143 |
| - } |
144 |
| - } |
145 |
| - } |
146 |
| - |
147 |
| - @Kroll.method |
148 |
| - public boolean isSupported() { |
149 |
| - Context context = TiApplication.getAppCurrentActivity(); |
150 |
| - List<ResolveInfo> browsersList = Utils.allBrowsers(context); |
151 |
| - |
152 |
| - return !browsersList.isEmpty(); |
153 |
| - } |
154 |
| - |
155 |
| - @Kroll.method |
156 |
| - public void close(KrollDict options) { |
157 |
| - Log.e(Params.LCAT, "The \"close\" method is not implemented, yet!"); |
158 |
| - } |
159 |
| - |
160 |
| - @Kroll.method |
161 |
| - public boolean isOpen(KrollDict options) { |
162 |
| - Log.e(Params.LCAT, "The \"isOpen\" method is not implemented, yet!"); |
163 |
| - return false; |
164 |
| - } |
| 40 | + // Standard Debugging variables |
| 41 | + private static final String LCAT = "TiWebDialog"; |
| 42 | + |
| 43 | + private List<String> getCustomTabBrowsers(Context context, List<ResolveInfo> browsersList) |
| 44 | + { |
| 45 | + List<String> customTabBrowsers = new ArrayList<String>(); |
| 46 | + |
| 47 | + for (ResolveInfo info : browsersList) { |
| 48 | + String packageName = info.activityInfo.packageName; |
| 49 | + |
| 50 | + Intent intent = new Intent(); |
| 51 | + intent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION); |
| 52 | + intent.setPackage(packageName); |
| 53 | + |
| 54 | + if (context.getPackageManager().resolveService(intent, 0) != null) { |
| 55 | + customTabBrowsers.add(packageName); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return customTabBrowsers; |
| 60 | + } |
| 61 | + |
| 62 | + private void openCustomTab(Context context, List<String> customTabBrowsers, KrollDict options) |
| 63 | + { |
| 64 | + String URL = options.getString(Params.URL); |
| 65 | + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); |
| 66 | + builder.setShowTitle(Utils.getBool(options, Params.SHOW_TITLE)); |
| 67 | + |
| 68 | + int barColor = Utils.getColor(options, Params.BAR_COLOR); |
| 69 | + if (barColor != -1) { |
| 70 | + builder.setToolbarColor(barColor); |
| 71 | + } |
| 72 | + |
| 73 | + // set start and exit animations |
| 74 | + if (Utils.getBool(options, Params.FADE_TRANSITION)) { |
| 75 | + builder.setStartAnimations(context, android.R.anim.fade_in, android.R.anim.fade_out); |
| 76 | + builder.setExitAnimations(context, android.R.anim.fade_in, android.R.anim.fade_out); |
| 77 | + } |
| 78 | + |
| 79 | + // hide navigation bar on scroll |
| 80 | + if (Utils.getBool(options, Params.BAR_COLLAPSING_ENABLED)) { |
| 81 | + builder.enableUrlBarHiding(); |
| 82 | + } |
| 83 | + |
| 84 | + //enable Share link option |
| 85 | + if (Utils.getBool(options, Params.ENABLE_SHARING)) { |
| 86 | + builder.addDefaultShareMenuItem(); |
| 87 | + } |
| 88 | + |
| 89 | + String closeIcon = Utils.getString(options, Params.CLOSE_ICON); |
| 90 | + if (!closeIcon.isEmpty()) { |
| 91 | + builder.setCloseButtonIcon(getIcon(closeIcon)); |
| 92 | + } |
| 93 | + |
| 94 | + CustomTabsIntent tabIntent = builder.build(); |
| 95 | + |
| 96 | + for (String s : customTabBrowsers) { |
| 97 | + tabIntent.intent.setPackage(s); |
| 98 | + } |
| 99 | + |
| 100 | + tabIntent.launchUrl(context, Uri.parse(URL)); |
| 101 | + } |
| 102 | + |
| 103 | + private Bitmap getIcon(String path) |
| 104 | + { |
| 105 | + Bitmap resultBitmap = null; |
| 106 | + |
| 107 | + if (path != null && !path.trim().equalsIgnoreCase("")) { |
| 108 | + String resourceIcon = path.replaceAll(".png", ""); |
| 109 | + resourceIcon = "drawable." + resourceIcon; |
| 110 | + |
| 111 | + int resource = Utils.getR(resourceIcon); |
| 112 | + |
| 113 | + if (resource == 0) { |
| 114 | + TiUrl imageUrl = new TiUrl(path); |
| 115 | + TiFileHelper tfh = new TiFileHelper(TiApplication.getInstance()); |
| 116 | + Drawable d = tfh.loadDrawable(imageUrl.resolve(), false); |
| 117 | + |
| 118 | + resultBitmap = ((BitmapDrawable) d).getBitmap(); |
| 119 | + |
| 120 | + } else { |
| 121 | + resultBitmap = |
| 122 | + BitmapFactory.decodeResource(TiApplication.getAppRootOrCurrentActivity().getResources(), resource); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // important step to show close icon |
| 127 | + // rescale bitmap to 24dp(Height) x 48dp(Width) as mentioned here, otherwise it won't work |
| 128 | + // https://developer.chrome.com/multidevice/android/customtabs#choosing-an icon for the action button |
| 129 | + if (resultBitmap != null) { |
| 130 | + resultBitmap = Utils.rescaleBitmap(TiApplication.getAppRootOrCurrentActivity(), resultBitmap, 24, 48); |
| 131 | + } |
| 132 | + |
| 133 | + return resultBitmap; |
| 134 | + } |
| 135 | + |
| 136 | + @Kroll.method |
| 137 | + public void open(KrollDict options) |
| 138 | + { |
| 139 | + if ((options != null) && options.containsKeyAndNotNull(Params.URL)) { |
| 140 | + Context context = TiApplication.getAppCurrentActivity(); |
| 141 | + List<ResolveInfo> browsersList = Utils.allBrowsers(context); |
| 142 | + |
| 143 | + KrollDict event = new KrollDict(); |
| 144 | + |
| 145 | + event.put("success", !browsersList.isEmpty()); |
| 146 | + event.put("url", options.getString(Params.URL)); |
| 147 | + |
| 148 | + if (!browsersList.isEmpty()) { |
| 149 | + List<String> customTabBrowsers = getCustomTabBrowsers(context, browsersList); |
| 150 | + |
| 151 | + // show supported browsers list or open directly if only 1 supported browser is available |
| 152 | + openCustomTab(context, customTabBrowsers, options); |
| 153 | + |
| 154 | + } else { |
| 155 | + Log.i(Params.LCAT, "No browsers available in this device."); |
| 156 | + } |
| 157 | + |
| 158 | + fireEvent("open", event); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Kroll.method |
| 163 | + public boolean isSupported() |
| 164 | + { |
| 165 | + Context context = TiApplication.getAppCurrentActivity(); |
| 166 | + List<ResolveInfo> browsersList = Utils.allBrowsers(context); |
| 167 | + |
| 168 | + return !browsersList.isEmpty(); |
| 169 | + } |
| 170 | + |
| 171 | + @Kroll.method |
| 172 | + public void close(KrollDict options) |
| 173 | + { |
| 174 | + Log.e(Params.LCAT, "The \"close\" method is not implemented on Android, yet!"); |
| 175 | + } |
| 176 | + |
| 177 | + @Kroll.method |
| 178 | + public boolean isOpen(KrollDict options) |
| 179 | + { |
| 180 | + Log.e(Params.LCAT, "The \"isOpen\" method is not implemented on Android, yet!"); |
| 181 | + return false; |
| 182 | + } |
165 | 183 | }
|
0 commit comments