1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bidi;
18 
19 import android.app.AlertDialog;
20 import android.app.Fragment;
21 import android.os.Bundle;
22 import android.text.Editable;
23 import android.text.Spannable;
24 import android.text.style.ForegroundColorSpan;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.Button;
29 import android.widget.EditText;
30 
31 public class BiDiTestBasic extends Fragment {
32 
33     private View currentView;
34     private Button alertDialogButton;
35     private String[] items = {"This is a very very very very very very very very very very very long Item1", "Item2"};
36 
37     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)38     public View onCreateView(LayoutInflater inflater, ViewGroup container,
39             Bundle savedInstanceState) {
40         currentView = inflater.inflate(R.layout.basic, container, false);
41         return currentView;
42     }
43 
44     @Override
onViewCreated(View view, Bundle savedInstanceState)45     public void onViewCreated(View view, Bundle savedInstanceState) {
46         super.onViewCreated(view, savedInstanceState);
47 
48         alertDialogButton = (Button) currentView.findViewById(R.id.button_alert_dialog);
49         alertDialogButton.setOnClickListener(new View.OnClickListener() {
50             public void onClick(View v) {
51                 showDialog();
52             }
53         });
54 
55         useSpans();
56     }
57 
showDialog()58     private void showDialog() {
59         AlertDialog.Builder builder = new AlertDialog.Builder(currentView.getContext());
60         builder.setSingleChoiceItems(items, 0, null);
61         builder.show();
62     }
63 
useSpans()64     private void useSpans() {
65         EditText urlEdit = (EditText) currentView.findViewById(R.id.edittext_url);
66         Editable url = urlEdit.getText();
67         if (url.length() < 1) {
68           return;
69         }
70 
71         String urlString = url.toString();
72         int urlLength = urlString.length();
73         String domainAndRegistry = "amazon.co.uk";
74 
75         int startSchemeIndex = urlString.startsWith("https") ? 5 : 0;
76         int startDomainIndex = urlString.indexOf(domainAndRegistry);
77         if (startDomainIndex == -1) {
78           assert false;
79           return;
80         }
81         int stopIndex = startDomainIndex + domainAndRegistry.length();
82 
83         if (startDomainIndex != 0) {
84           url.setSpan(new ForegroundColorSpan(0xfff00fff),
85                   startSchemeIndex,
86                   startDomainIndex,
87                   Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
88         }
89 
90         url.setSpan(new ForegroundColorSpan(0xff548aff),
91                 startDomainIndex,
92                 stopIndex,
93                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
94 
95         if (stopIndex < urlString.length()) {
96           url.setSpan(new ForegroundColorSpan(0xfff00fff),
97                   stopIndex,
98                   urlLength,
99                   Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
100         }
101     }
102 }
103