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 
17 package com.android.bips.ui;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.ServiceConnection;
23 import android.net.Uri;
24 import android.net.wifi.p2p.WifiP2pDevice;
25 import android.os.Bundle;
26 import android.os.IBinder;
27 import android.preference.Preference;
28 import android.preference.PreferenceCategory;
29 import android.preference.PreferenceFragment;
30 import android.util.Log;
31 
32 import com.android.bips.BuiltInPrintService;
33 import com.android.bips.R;
34 import com.android.bips.discovery.DiscoveredPrinter;
35 import com.android.bips.discovery.P2pDiscovery;
36 import com.android.bips.p2p.P2pMonitor;
37 import com.android.bips.p2p.P2pPeerListener;
38 
39 /**
40  * Present a list of previously-saved printers, and allow them to be removed
41  */
42 public class FindP2pPrintersFragment extends PreferenceFragment implements ServiceConnection {
43     private static final String TAG = FindP2pPrintersFragment.class.getSimpleName();
44     private static final boolean DEBUG = false;
45 
46     private static final String KEY_AVAILABLE = "available";
47 
48     private BuiltInPrintService mPrintService;
49     private P2pListener mPeerDiscoveryListener;
50     private PreferenceCategory mAvailableCategory;
51 
52     @Override
onCreate(Bundle savedInstanceState)53     public void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55         addPreferencesFromResource(R.xml.find_p2p_prefs);
56         mAvailableCategory = (PreferenceCategory) getPreferenceScreen()
57                 .findPreference(KEY_AVAILABLE);
58     }
59 
60     @Override
onStart()61     public void onStart() {
62         super.onStart();
63         if (DEBUG) Log.d(TAG, "onStart");
64         getActivity().setTitle(R.string.wifi_direct_printers);
65         getContext().bindService(new Intent(getContext(), BuiltInPrintService.class), this,
66                 Context.BIND_AUTO_CREATE);
67     }
68 
69     @Override
onStop()70     public void onStop() {
71         super.onStop();
72         if (DEBUG) Log.d(TAG, "onStop");
73         if (mPeerDiscoveryListener != null) {
74             mPrintService.getP2pMonitor().stopDiscover(mPeerDiscoveryListener);
75             mPeerDiscoveryListener = null;
76         }
77         getContext().unbindService(this);
78         mPrintService = null;
79     }
80 
81     @Override
onServiceConnected(ComponentName componentName, IBinder iBinder)82     public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
83         if (DEBUG) Log.d(TAG, "onServiceConnected");
84         mPrintService = BuiltInPrintService.getInstance();
85         if (mPrintService == null) {
86             return;
87         }
88 
89         if (mPeerDiscoveryListener == null) {
90             mPeerDiscoveryListener = new P2pListener();
91             mPrintService.getP2pMonitor().discover(mPeerDiscoveryListener);
92         }
93     }
94 
95     @Override
onServiceDisconnected(ComponentName componentName)96     public void onServiceDisconnected(ComponentName componentName) {
97         mPrintService = null;
98     }
99 
100     /** Handle discovered P2P printers */
101     private class P2pListener implements P2pPeerListener {
102         @Override
onPeerFound(WifiP2pDevice peer)103         public void onPeerFound(WifiP2pDevice peer) {
104             if (DEBUG) Log.d(TAG, "onPeerFound: " + P2pMonitor.toString(peer));
105             if (mPrintService == null) {
106                 return;
107             }
108 
109             DiscoveredPrinter printer = P2pDiscovery.toPrinter(peer);
110 
111             // Ignore printers we have already added
112             for (DiscoveredPrinter prior : mPrintService.getP2pDiscovery().getSavedPrinters()) {
113                 if (prior.path.equals(printer.path)) {
114                     return;
115                 }
116             }
117 
118             // Install a preference so the user can add this printer
119             PrinterPreference pref = getPrinterPreference(printer.getUri());
120             if (pref != null) {
121                 pref.updatePrinter(printer);
122             } else {
123                 pref = new PrinterPreference(getContext(), mPrintService, printer, true);
124                 pref.setOnPreferenceClickListener(preference -> {
125                     if (DEBUG) Log.d(TAG, "add " + P2pDiscovery.toPrinter(peer));
126                     new AddP2pPrinterDialog(FindP2pPrintersFragment.this, mPrintService, peer)
127                             .show();
128                     return true;
129                 });
130                 mAvailableCategory.addPreference(pref);
131             }
132         }
133 
134         @Override
onPeerLost(WifiP2pDevice peer)135         public void onPeerLost(WifiP2pDevice peer) {
136             if (DEBUG) Log.d(TAG, "onPeerLost: " + P2pMonitor.toString(peer));
137             if (mPrintService == null) {
138                 return;
139             }
140 
141             DiscoveredPrinter printer = P2pDiscovery.toPrinter(peer);
142 
143             // Remove this preference because the printer is no longer available
144             PrinterPreference pref = getPrinterPreference(printer.path);
145             if (pref != null) {
146                 mAvailableCategory.removePreference(pref);
147             }
148         }
149 
getPrinterPreference(Uri printerPath)150         private PrinterPreference getPrinterPreference(Uri printerPath) {
151             for (int i = 0; i < mAvailableCategory.getPreferenceCount(); i++) {
152                 Preference pref = mAvailableCategory.getPreference(i);
153                 if (pref instanceof PrinterPreference
154                         && ((PrinterPreference) pref).getPrinter().path.equals(printerPath)) {
155                     return (PrinterPreference) pref;
156                 }
157             }
158             return null;
159         }
160     }
161 }
162