Index: demos/src/main/java/org/chromium/customtabsdemos/CustomUIActivity.java |
diff --git a/demos/src/main/java/org/chromium/customtabsdemos/CustomUIActivity.java b/demos/src/main/java/org/chromium/customtabsdemos/CustomUIActivity.java |
index 5ce437c407c8c7f7cc1dccbd8e92ccbbb986b2dc..b3142ed064498fdbfea47298b8b2e66b4322c9ce 100644 |
--- a/demos/src/main/java/org/chromium/customtabsdemos/CustomUIActivity.java |
+++ b/demos/src/main/java/org/chromium/customtabsdemos/CustomUIActivity.java |
@@ -17,16 +17,20 @@ import android.app.PendingIntent; |
import android.content.Intent; |
import android.graphics.Bitmap; |
import android.graphics.BitmapFactory; |
-import android.graphics.Color; |
import android.net.Uri; |
import android.os.Bundle; |
+import android.provider.CalendarContract; |
+import android.provider.ContactsContract; |
import android.support.customtabs.CustomTabsIntent; |
import android.support.v7.app.AppCompatActivity; |
-import android.util.Log; |
import android.view.View; |
import android.widget.CheckBox; |
import android.widget.EditText; |
+import org.chromium.customtabsclient.shared.CustomTabsHelper; |
+ |
+import java.util.ArrayList; |
+ |
/** |
* Opens Chrome Custom Tabs with a customized UI. |
*/ |
@@ -34,29 +38,37 @@ public class CustomUIActivity extends AppCompatActivity implements View.OnClickL |
private static final String TAG = "CustChromeTabActivity"; |
private EditText mUrlEditText; |
- private EditText mCustomTabColorEditText; |
- private CheckBox mShowActionButtonCheckbox; |
+ private CheckBox mAddActionButtonCheckbox; |
private CheckBox mAddMenusCheckbox; |
private CheckBox mShowTitleCheckBox; |
private CheckBox mCustomBackButtonCheckBox; |
private CheckBox mAutoHideAppBarCheckbox; |
private CustomTabActivityHelper mCustomTabActivityHelper; |
+ private String toolbarColor; |
+ private String packageName; |
+ private ArrayList<String> itemsDescriptions; |
+ private ArrayList<Uri> itemsImage; |
+ private ArrayList<String> allSupportingPackageNames; |
+ private ArrayList<String> itemsIntent; |
+ |
+ private int advancedUISettintRequestCode; |
@Override |
protected void onCreate(Bundle savedInstanceState) { |
super.onCreate(savedInstanceState); |
setContentView(R.layout.activity_custom_ui); |
- |
mCustomTabActivityHelper = new CustomTabActivityHelper(); |
findViewById(R.id.start_custom_tab).setOnClickListener(this); |
- |
+ findViewById(R.id.advanced_ui_setting).setOnClickListener(this); |
mUrlEditText = (EditText) findViewById(R.id.url); |
- mCustomTabColorEditText = (EditText) findViewById(R.id.custom_toolbar_color); |
- mShowActionButtonCheckbox = (CheckBox) findViewById(R.id.custom_show_action_button); |
+ mAddActionButtonCheckbox = (CheckBox) findViewById(R.id.custom_show_action_button); |
mAddMenusCheckbox = (CheckBox) findViewById(R.id.custom_add_menus); |
mShowTitleCheckBox = (CheckBox) findViewById(R.id.show_title); |
mCustomBackButtonCheckBox = (CheckBox) findViewById(R.id.custom_back_button); |
mAutoHideAppBarCheckbox = (CheckBox) findViewById(R.id.auto_hide_checkbox); |
+ advancedUISettintRequestCode = 999; |
+ |
+ makeDefaultSetting(); |
} |
@Override |
@@ -78,25 +90,96 @@ public class CustomUIActivity extends AppCompatActivity implements View.OnClickL |
case R.id.start_custom_tab: |
openCustomTab(); |
break; |
+ case R.id.advanced_ui_setting: |
+ openUISetting(); |
+ break; |
default: |
//Unknown View Clicked |
} |
} |
- private void openCustomTab() { |
- String url = mUrlEditText.getText().toString(); |
+ @Override |
+ public void onActivityResult(int requestCode, int resultCode, Intent data) { |
+ super.onActivityResult(requestCode, resultCode, data); |
+ if (requestCode == advancedUISettintRequestCode && data != null) { |
+ packageName = data.getStringExtra(AdvancedUISettingActivity.KEY_PACKAGE); |
+ toolbarColor = data.getStringExtra(AdvancedUISettingActivity.KEY_COLOR); |
+ itemsDescriptions = (ArrayList<String>) data.getSerializableExtra( |
+ AdvancedUISettingActivity.KEY_DESCRIPTIONS); |
+ itemsImage = (ArrayList<Uri>) data.getSerializableExtra( |
+ AdvancedUISettingActivity.KEY_IMAGES); |
+ itemsIntent = (ArrayList<String>) data.getSerializableExtra( |
+ AdvancedUISettingActivity.KEY_INTENTS); |
+ } else { |
- int color = Color.BLUE; |
- try { |
- color = Color.parseColor(mCustomTabColorEditText.getText().toString()); |
- } catch (NumberFormatException ex) { |
- Log.i(TAG, "Unable to parse Color: " + mCustomTabColorEditText.getText()); |
} |
+ } |
+ |
+ private void makeDefaultSetting() { |
+ toolbarColor = "Red"; |
+ allSupportingPackageNames = CustomTabsHelper.getAllPackagesSupportingCustomTabs(this); |
+ packageName = CustomTabsHelper.getPackageNameToUse(this, allSupportingPackageNames); |
+ itemsIntent = new ArrayList<String>(); |
+ itemsDescriptions = new ArrayList<String>(); |
+ itemsImage = new ArrayList<Uri>(); |
+ } |
+ |
+ private void openUISetting() { |
+ Intent intent = new Intent(this, AdvancedUISettingActivity.class); |
+ intent.putExtra(AdvancedUISettingActivity.KEY_PACKAGES_LIST, allSupportingPackageNames); |
Ian Wen
2016/02/10 02:36:06
you don't need to pass the list to the advanced ac
BigBossZhiling
2016/02/17 05:18:09
Done.
|
+ intent.putExtra(AdvancedUISettingActivity.KEY_PACKAGE, packageName); |
+ intent.putExtra(AdvancedUISettingActivity.KEY_COLOR, toolbarColor); |
+ intent.putExtra(AdvancedUISettingActivity.KEY_DESCRIPTIONS, itemsDescriptions); |
+ intent.putExtra(AdvancedUISettingActivity.KEY_IMAGES, itemsImage); |
+ intent.putExtra(AdvancedUISettingActivity.KEY_INTENTS, itemsIntent); |
+ startActivityForResult(intent, advancedUISettintRequestCode); |
+ } |
+ |
+ private void addActionButtons(CustomTabsIntent.Builder intentBuilder) { |
+ for (int i = 0; i < itemsDescriptions.size(); i++) { |
+ intentBuilder.addActionBarItem(i, |
+ AdvancedUISettingActivity.uriToBitMap(this, itemsImage.get(i)), |
+ itemsDescriptions.get(i), |
+ createPendingIntent(itemsIntent.get(i))); |
+ } |
+ } |
+ |
+ private PendingIntent createPendingIntent(String intent) { |
+ switch (intent) { |
+ case "Send": |
+ Intent sendIntent = new Intent(); |
+ sendIntent.setAction(Intent.ACTION_SEND); |
+ sendIntent.setType("text/plain"); |
+ return PendingIntent.getActivity(this, 0, sendIntent, 0); |
+ case "Select Contact": |
+ Intent contact = new Intent(Intent.ACTION_PICK); |
+ contact.setType(ContactsContract.Contacts.CONTENT_TYPE); |
+ return PendingIntent.getActivity(this, 0, contact, 0); |
+ case "Add Event": |
+ Intent event = new Intent(Intent.ACTION_INSERT) |
+ .setData(CalendarContract.Events.CONTENT_URI) |
+ .putExtra(CalendarContract.Events.TITLE, "Great Event") |
+ .putExtra(CalendarContract.Events.EVENT_LOCATION, "Googleplex 43") |
+ .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, "today") |
+ .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, "tomorrow"); |
+ return PendingIntent.getActivity(this, 0, event, 0); |
+ case "Camera": |
+ return PendingIntent.getActivity(this, 0, new Intent(this, Camera.class), |
+ PendingIntent.FLAG_UPDATE_CURRENT); |
+ default: |
+ return null; |
+ } |
+ } |
+ |
+ private void openCustomTab() { |
+ String url = mUrlEditText.getText().toString(); |
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); |
- intentBuilder.setToolbarColor(color); |
- if (mShowActionButtonCheckbox.isChecked()) { |
+ intentBuilder.setToolbarColor(CustomTabsHelper.stringToColor(toolbarColor)); |
+ intentBuilder.addDefaultShareMenuItem(); |
+ |
+ if (mAddActionButtonCheckbox.isChecked()) { |
//Generally you do not want to decode bitmaps in the UI thread. |
String shareLabel = getString(R.string.label_action_share); |
Bitmap icon = BitmapFactory.decodeResource(getResources(), |
@@ -113,6 +196,8 @@ public class CustomUIActivity extends AppCompatActivity implements View.OnClickL |
intentBuilder.setShowTitle(mShowTitleCheckBox.isChecked()); |
+ addActionButtons(intentBuilder); |
+ |
if (mAutoHideAppBarCheckbox.isChecked()) { |
intentBuilder.enableUrlBarHiding(); |
} |
@@ -127,7 +212,8 @@ public class CustomUIActivity extends AppCompatActivity implements View.OnClickL |
android.R.anim.slide_out_right); |
CustomTabActivityHelper.openCustomTab( |
- this, intentBuilder.build(), Uri.parse(url), new WebviewFallback()); |
+ this, intentBuilder.build(), Uri.parse(url), new WebviewFallback(), |
+ packageName); |
} |
private PendingIntent createPendingIntent() { |