1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * Copyright (C) 2016 Mopria Alliance, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.bips.ui; 19 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.ServiceConnection; 24 import android.os.Bundle; 25 import android.os.IBinder; 26 import android.preference.Preference; 27 import android.preference.PreferenceFragment; 28 import android.preference.PreferenceScreen; 29 import android.util.Log; 30 import android.view.ContextMenu; 31 import android.view.Menu; 32 import android.view.MenuItem; 33 import android.view.View; 34 import android.widget.AdapterView; 35 import android.widget.ListView; 36 37 import com.android.bips.BuiltInPrintService; 38 import com.android.bips.R; 39 import com.android.bips.discovery.DiscoveredPrinter; 40 import com.android.bips.discovery.Discovery; 41 import com.android.bips.discovery.ManualDiscovery; 42 43 /** 44 * Presents a list of printers and the ability to add a new one 45 */ 46 public class ManualPrintersFragment extends PreferenceFragment implements ServiceConnection, 47 Discovery.Listener { 48 private static final String TAG = ManualPrintersFragment.class.getSimpleName(); 49 private static final boolean DEBUG = false; 50 51 private static final int ORDER_LAST = Integer.MAX_VALUE - 1; 52 53 private BuiltInPrintService mLocalPrint; 54 private ManualDiscovery mManualDiscovery; 55 private AddManualPrinterDialog mDialog; 56 57 @Override onCreate(Bundle in)58 public void onCreate(Bundle in) { 59 if (DEBUG) Log.d(TAG, "onCreate"); 60 super.onCreate(in); 61 62 getContext().bindService(new Intent(getContext(), BuiltInPrintService.class), this, 63 Context.BIND_AUTO_CREATE); 64 } 65 66 @Override onDestroy()67 public void onDestroy() { 68 if (DEBUG) Log.d(TAG, "onDestroy"); 69 if (mManualDiscovery != null) { 70 mManualDiscovery.stop(this); 71 } 72 getContext().unbindService(this); 73 super.onDestroy(); 74 } 75 76 @Override onServiceConnected(ComponentName componentName, IBinder iBinder)77 public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 78 if (DEBUG) Log.d(TAG, "onServiceConnected"); 79 mLocalPrint = BuiltInPrintService.getInstance(); 80 81 // Set up the UI now that we have a bound service 82 PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(getActivity()); 83 setPreferenceScreen(screen); 84 85 AddPrinterPreference addPrinterPreference = new AddPrinterPreference(); 86 screen.addPreference(addPrinterPreference); 87 registerForContextMenu(getView().findViewById(android.R.id.list)); 88 89 mManualDiscovery = mLocalPrint.getManualDiscovery(); 90 mManualDiscovery.start(this); 91 92 // Simulate a click on add printer since that is likely what the user came here to do. 93 if (mManualDiscovery.getPrinters().isEmpty()) { 94 addPrinterPreference.onPreferenceClick(addPrinterPreference); 95 } 96 } 97 98 @Override onServiceDisconnected(ComponentName componentName)99 public void onServiceDisconnected(ComponentName componentName) { 100 mLocalPrint = null; 101 } 102 103 @Override onPrinterFound(DiscoveredPrinter printer)104 public void onPrinterFound(DiscoveredPrinter printer) { 105 if (DEBUG) Log.d(TAG, "onPrinterFound: " + printer); 106 PreferenceScreen screen = getPreferenceScreen(); 107 108 // Do not add duplicates 109 for (int i = 0; i < screen.getPreferenceCount(); i++) { 110 if (screen.getPreference(i) instanceof ManualPrinterPreference) { 111 ManualPrinterPreference preference = (ManualPrinterPreference) screen 112 .getPreference(i); 113 if (preference.printer.path.equals(printer.path)) { 114 // We have it already, proceed 115 return; 116 } 117 } 118 } 119 screen.addPreference(new ManualPrinterPreference(getContext(), printer)); 120 } 121 122 @Override onPrinterLost(DiscoveredPrinter printer)123 public void onPrinterLost(DiscoveredPrinter printer) { 124 if (DEBUG) Log.d(TAG, "onPrinterLost: " + printer); 125 PreferenceScreen screen = getPreferenceScreen(); 126 for (int i = 0; i < screen.getPreferenceCount(); i++) { 127 Preference preference = screen.getPreference(i); 128 if (preference instanceof ManualPrinterPreference) { 129 if (((ManualPrinterPreference) preference).printer.getUri() 130 .equals(printer.getUri())) { 131 screen.removePreference(preference); 132 break; 133 } 134 } 135 } 136 } 137 138 @Override onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo)139 public void onCreateContextMenu(ContextMenu menu, View view, 140 ContextMenu.ContextMenuInfo menuInfo) { 141 if (view instanceof ListView) { 142 int position = ((AdapterView.AdapterContextMenuInfo) menuInfo).position; 143 Preference preference = getPreferenceScreen().getPreference(position); 144 if (preference instanceof ManualPrinterPreference) { 145 final DiscoveredPrinter printer = ((ManualPrinterPreference) preference).printer; 146 menu.setHeaderTitle(printer.name); 147 MenuItem forgetItem = menu.add(Menu.NONE, R.string.forget_printer, 148 Menu.NONE, R.string.forget_printer); 149 forgetItem.setOnMenuItemClickListener(menuItem -> { 150 mManualDiscovery.removeManualPrinter(printer); 151 return true; 152 }); 153 } 154 } 155 } 156 157 @Override onDestroyView()158 public void onDestroyView() { 159 super.onDestroyView(); 160 if (mDialog != null && mDialog.isShowing()) { 161 mDialog.dismiss(); 162 } 163 } 164 165 private static class ManualPrinterPreference extends Preference { 166 final DiscoveredPrinter printer; 167 ManualPrinterPreference(Context context, DiscoveredPrinter printer)168 ManualPrinterPreference(Context context, DiscoveredPrinter printer) { 169 super(context); 170 this.printer = printer; 171 setLayoutResource(R.layout.printer_item); 172 setTitle(printer.name); 173 setSummary(printer.path.getHost()); 174 setIcon(R.drawable.ic_printer); 175 } 176 } 177 178 private class AddPrinterPreference extends Preference 179 implements Preference.OnPreferenceClickListener { AddPrinterPreference()180 AddPrinterPreference() { 181 super(ManualPrintersFragment.this.getContext()); 182 setTitle(R.string.add_manual_printer); 183 setIcon(R.drawable.ic_menu_add); 184 setOrder(ORDER_LAST); 185 setPersistent(false); 186 setOnPreferenceClickListener(this); 187 } 188 189 @Override onPreferenceClick(Preference preference)190 public boolean onPreferenceClick(Preference preference) { 191 if (DEBUG) Log.d(TAG, "onPreferenceClick for addPrinterPreference"); 192 mDialog = new AddManualPrinterDialog(getContext(), mManualDiscovery); 193 mDialog.show(); 194 return true; 195 } 196 } 197 }