Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2017 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 package com.android.documentsui.inspector;
     17 
     18 import static com.android.internal.util.Preconditions.checkArgument;
     19 
     20 import android.text.Spannable;
     21 import android.view.ActionMode;
     22 import android.view.Menu;
     23 import android.view.MenuItem;
     24 import android.widget.TextView;
     25 
     26 /**
     27  * Selects all or part of a textview.
     28  */
     29  public final class HeaderTextSelector implements ActionMode.Callback {
     30 
     31     private final TextView mText;
     32     private final Selector mSelector;
     33 
     34     public HeaderTextSelector(TextView text, Selector selector) {
     35         checkArgument(text != null);
     36         checkArgument(selector != null);
     37         mText = text;
     38         mSelector = selector;
     39     }
     40 
     41     // An action mode is created when the user selects text. This method is called where
     42     // we force it to select only the filename in the header. Meaning the name of the file would
     43     // be selected but the extension would not.
     44     @Override
     45     public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
     46         CharSequence value = mText.getText();
     47         if (value instanceof Spannable) {
     48             mSelector.select((Spannable) value, 0, getLengthOfFilename(value));
     49         }
     50         return true;
     51     }
     52 
     53     @Override
     54     public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
     55         return true;
     56     }
     57 
     58     @Override
     59     public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
     60         return false;
     61     }
     62 
     63     @Override
     64     public void onDestroyActionMode(ActionMode actionMode) {}
     65 
     66     /**
     67      * Gets the length of the inspector header for selection.
     68      *
     69      * Example:
     70      * filename.txt returns the end index needed to select "filename".
     71      *
     72      * @return the index of the last character in the filename
     73      */
     74     private static int getLengthOfFilename(CharSequence text) {
     75         String title = text.toString();
     76         if (title != null) {
     77             int index = title.indexOf('.');
     78             if (index > 0) {
     79                 return index;
     80             }
     81         }
     82 
     83         return text.length();
     84     }
     85 
     86     public interface Selector {
     87         void select(Spannable text, int start, int stop);
     88     }
     89 }
     90