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 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 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 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 addPrinterPreference.onPreferenceClick(addPrinterPreference); 94 } 95 96 @Override 97 public void onServiceDisconnected(ComponentName componentName) { 98 mLocalPrint = null; 99 } 100 101 @Override 102 public void onPrinterFound(DiscoveredPrinter printer) { 103 if (DEBUG) Log.d(TAG, "onPrinterFound: " + printer); 104 PreferenceScreen screen = getPreferenceScreen(); 105 106 // Do not add duplicates 107 for (int i = 0; i < screen.getPreferenceCount(); i++) { 108 if (screen.getPreference(i) instanceof ManualPrinterPreference) { 109 ManualPrinterPreference preference = (ManualPrinterPreference) screen 110 .getPreference(i); 111 if (preference.printer.path.equals(printer.path)) { 112 // We have it already, proceed 113 return; 114 } 115 } 116 } 117 screen.addPreference(new ManualPrinterPreference(getContext(), printer)); 118 } 119 120 @Override 121 public void onPrinterLost(DiscoveredPrinter printer) { 122 if (DEBUG) Log.d(TAG, "onPrinterLost: " + printer); 123 PreferenceScreen screen = getPreferenceScreen(); 124 for (int i = 0; i < screen.getPreferenceCount(); i++) { 125 Preference preference = screen.getPreference(i); 126 if (preference instanceof ManualPrinterPreference) { 127 if (((ManualPrinterPreference) preference).printer.getUri() 128 .equals(printer.getUri())) { 129 screen.removePreference(preference); 130 break; 131 } 132 } 133 } 134 } 135 136 @Override 137 public void onCreateContextMenu(ContextMenu menu, View view, 138 ContextMenu.ContextMenuInfo menuInfo) { 139 if (view instanceof ListView) { 140 int position = ((AdapterView.AdapterContextMenuInfo) menuInfo).position; 141 Preference preference = getPreferenceScreen().getPreference(position); 142 if (preference instanceof ManualPrinterPreference) { 143 final DiscoveredPrinter printer = ((ManualPrinterPreference) preference).printer; 144 menu.setHeaderTitle(printer.name); 145 MenuItem forgetItem = menu.add(Menu.NONE, R.string.forget_printer, 146 Menu.NONE, R.string.forget_printer); 147 forgetItem.setOnMenuItemClickListener(menuItem -> { 148 mManualDiscovery.removeManualPrinter(printer); 149 return true; 150 }); 151 } 152 } 153 } 154 155 @Override 156 public void onDestroyView() { 157 super.onDestroyView(); 158 if (mDialog != null && mDialog.isShowing()) { 159 mDialog.dismiss(); 160 } 161 } 162 163 private static class ManualPrinterPreference extends Preference { 164 final DiscoveredPrinter printer; 165 166 ManualPrinterPreference(Context context, DiscoveredPrinter printer) { 167 super(context); 168 this.printer = printer; 169 setLayoutResource(R.layout.printer_item); 170 setTitle(printer.name); 171 setSummary(printer.path.getHost()); 172 setIcon(R.drawable.ic_printer); 173 } 174 } 175 176 private class AddPrinterPreference extends Preference 177 implements Preference.OnPreferenceClickListener { 178 AddPrinterPreference() { 179 super(ManualPrintersFragment.this.getContext()); 180 setTitle(R.string.add_manual_printer); 181 setIcon(R.drawable.ic_menu_add); 182 setOrder(ORDER_LAST); 183 setPersistent(false); 184 setOnPreferenceClickListener(this); 185 } 186 187 @Override 188 public boolean onPreferenceClick(Preference preference) { 189 if (DEBUG) Log.d(TAG, "onPreferenceClick for addPrinterPreference"); 190 mDialog = new AddManualPrinterDialog(getContext(), mManualDiscovery); 191 mDialog.show(); 192 return true; 193 } 194 } 195 }