Home | History | Annotate | Download | only in com.example.android.appshortcuts
      1 /*
      2  * Copyright (C) 2016 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.example.android.appshortcuts;
     17 
     18 import android.app.AlertDialog;
     19 import android.app.ListActivity;
     20 import android.content.Context;
     21 import android.content.pm.ShortcutInfo;
     22 import android.os.AsyncTask;
     23 import android.os.Bundle;
     24 import android.util.Log;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.View.OnClickListener;
     28 import android.view.ViewGroup;
     29 import android.view.inputmethod.EditorInfo;
     30 import android.widget.BaseAdapter;
     31 import android.widget.Button;
     32 import android.widget.EditText;
     33 import android.widget.TextView;
     34 
     35 import java.util.ArrayList;
     36 import java.util.List;
     37 
     38 public class Main extends ListActivity implements OnClickListener {
     39     static final String TAG = "ShortcutSample";
     40 
     41     private static final String ID_ADD_WEBSITE = "add_website";
     42 
     43     private static final String ACTION_ADD_WEBSITE =
     44             "com.example.android.shortcutsample.ADD_WEBSITE";
     45 
     46     private MyAdapter mAdapter;
     47 
     48     private ShortcutHelper mHelper;
     49 
     50     @Override
     51     protected void onCreate(Bundle savedInstanceState) {
     52         super.onCreate(savedInstanceState);
     53 
     54         setContentView(R.layout.main);
     55 
     56         mHelper = new ShortcutHelper(this);
     57 
     58         mHelper.maybeRestoreAllDynamicShortcuts();
     59 
     60         mHelper.refreshShortcuts(/*force=*/ false);
     61 
     62         if (ACTION_ADD_WEBSITE.equals(getIntent().getAction())) {
     63             // Invoked via the manifest shortcut.
     64             addWebSite();
     65         }
     66 
     67         mAdapter = new MyAdapter(this.getApplicationContext());
     68         setListAdapter(mAdapter);
     69     }
     70 
     71     @Override
     72     protected void onResume() {
     73         super.onResume();
     74         refreshList();
     75     }
     76 
     77     /**
     78      * Handle the add button.
     79      */
     80     public void onAddPressed(View v) {
     81         addWebSite();
     82     }
     83 
     84     private void addWebSite() {
     85         Log.i(TAG, "addWebSite");
     86 
     87         // This is important.  This allows the launcher to build a prediction model.
     88         mHelper.reportShortcutUsed(ID_ADD_WEBSITE);
     89 
     90         final EditText editUri = new EditText(this);
     91 
     92         editUri.setHint("http://www.android.com/");
     93         editUri.setInputType(EditorInfo.TYPE_TEXT_VARIATION_URI);
     94 
     95         new AlertDialog.Builder(this)
     96                 .setTitle("Add new website")
     97                 .setMessage("Type URL of a website")
     98                 .setView(editUri)
     99                 .setPositiveButton("Add", (dialog, whichButton) -> {
    100                     final String url = editUri.getText().toString().trim();
    101                     if (url.length() > 0) {
    102                         addUriAsync(url);
    103                     }
    104                 })
    105                 .show();
    106     }
    107 
    108     private void addUriAsync(String uri) {
    109         new AsyncTask<Void, Void, Void>() {
    110             @Override
    111             protected Void doInBackground(Void... params) {
    112                 mHelper.addWebSiteShortcut(uri);
    113                 return null;
    114             }
    115 
    116             @Override
    117             protected void onPostExecute(Void aVoid) {
    118                 refreshList();
    119             }
    120         }.execute();
    121     }
    122 
    123     private void refreshList() {
    124         mAdapter.setShortcuts(mHelper.getShortcuts());
    125     }
    126 
    127     @Override
    128     public void onClick(View v) {
    129         final ShortcutInfo shortcut = (ShortcutInfo) ((View) v.getParent()).getTag();
    130 
    131         switch (v.getId()) {
    132             case R.id.disable:
    133                 if (shortcut.isEnabled()) {
    134                     mHelper.disableShortcut(shortcut);
    135                 } else {
    136                     mHelper.enableShortcut(shortcut);
    137                 }
    138                 refreshList();
    139                 break;
    140             case R.id.remove:
    141                 mHelper.removeShortcut(shortcut);
    142                 refreshList();
    143                 break;
    144         }
    145     }
    146 
    147     private static final List<ShortcutInfo> EMPTY_LIST = new ArrayList<>();
    148 
    149     private String getType(ShortcutInfo shortcut) {
    150         final StringBuilder sb = new StringBuilder();
    151         String sep = "";
    152         if (shortcut.isDynamic()) {
    153             sb.append(sep);
    154             sb.append("Dynamic");
    155             sep = ", ";
    156         }
    157         if (shortcut.isPinned()) {
    158             sb.append(sep);
    159             sb.append("Pinned");
    160             sep = ", ";
    161         }
    162         if (!shortcut.isEnabled()) {
    163             sb.append(sep);
    164             sb.append("Disabled");
    165             sep = ", ";
    166         }
    167         return sb.toString();
    168     }
    169 
    170     private class MyAdapter extends BaseAdapter {
    171         private final Context mContext;
    172         private final LayoutInflater mInflater;
    173         private List<ShortcutInfo> mList = EMPTY_LIST;
    174 
    175         public MyAdapter(Context context) {
    176             mContext = context;
    177             mInflater = mContext.getSystemService(LayoutInflater.class);
    178         }
    179 
    180         @Override
    181         public int getCount() {
    182             return mList.size();
    183         }
    184 
    185         @Override
    186         public Object getItem(int position) {
    187             return mList.get(position);
    188         }
    189 
    190         @Override
    191         public long getItemId(int position) {
    192             return position;
    193         }
    194 
    195         @Override
    196         public boolean hasStableIds() {
    197             return false;
    198         }
    199 
    200         @Override
    201         public boolean areAllItemsEnabled() {
    202             return true;
    203         }
    204 
    205         @Override
    206         public boolean isEnabled(int position) {
    207             return true;
    208         }
    209 
    210         public void setShortcuts(List<ShortcutInfo> list) {
    211             mList = list;
    212             notifyDataSetChanged();
    213         }
    214 
    215         @Override
    216         public View getView(int position, View convertView, ViewGroup parent) {
    217             final View view;
    218             if (convertView != null) {
    219                 view = convertView;
    220             } else {
    221                 view = mInflater.inflate(R.layout.list_item, null);
    222             }
    223 
    224             bindView(view, position, mList.get(position));
    225 
    226             return view;
    227         }
    228 
    229         public void bindView(View view, int position, ShortcutInfo shortcut) {
    230             view.setTag(shortcut);
    231 
    232             final TextView line1 = (TextView) view.findViewById(R.id.line1);
    233             final TextView line2 = (TextView) view.findViewById(R.id.line2);
    234 
    235             line1.setText(shortcut.getLongLabel());
    236 
    237             line2.setText(getType(shortcut));
    238 
    239             final Button remove = (Button) view.findViewById(R.id.remove);
    240             final Button disable = (Button) view.findViewById(R.id.disable);
    241 
    242             disable.setText(
    243                     shortcut.isEnabled() ? R.string.disable_shortcut : R.string.enable_shortcut);
    244 
    245             remove.setOnClickListener(Main.this);
    246             disable.setOnClickListener(Main.this);
    247         }
    248     }
    249 }
    250