1 /* 2 * Copyright (C) 2019 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.app.ActionBar; 20 import android.app.Activity; 21 import android.app.FragmentManager; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.ServiceConnection; 26 import android.os.Bundle; 27 import android.os.IBinder; 28 import android.print.PrintJobInfo; 29 import android.print.PrinterId; 30 import android.printservice.PrintService; 31 import android.util.Log; 32 import android.view.MenuItem; 33 34 import com.android.bips.BuiltInPrintService; 35 import com.android.bips.discovery.DiscoveredPrinter; 36 import com.android.bips.discovery.Discovery; 37 38 import java.net.InetAddress; 39 import java.net.UnknownHostException; 40 import java.util.concurrent.ExecutorService; 41 import java.util.concurrent.Executors; 42 43 /** 44 * Launched by system in response to a "More Options" request while tracking a printer. 45 */ 46 public class MoreOptionsActivity extends Activity implements ServiceConnection, Discovery.Listener { 47 private static final String TAG = MoreOptionsActivity.class.getSimpleName(); 48 49 private static final boolean DEBUG = false; 50 51 private BuiltInPrintService mPrintService; 52 PrinterId mPrinterId; 53 DiscoveredPrinter mPrinter; 54 InetAddress mPrinterAddress; 55 public static final String EXTRA_PRINTER_ID = "EXTRA_PRINTER_ID"; 56 private final ExecutorService mExecutorService = Executors.newSingleThreadExecutor(); 57 58 @Override onCreate(Bundle savedInstanceState)59 protected void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 if (getIntent().hasExtra(PrintService.EXTRA_PRINT_JOB_INFO)) { 62 PrintJobInfo jobInfo = 63 getIntent().getParcelableExtra(PrintService.EXTRA_PRINT_JOB_INFO); 64 mPrinterId = jobInfo.getPrinterId(); 65 } else if (getIntent().hasExtra(EXTRA_PRINTER_ID)) { 66 mPrinterId = getIntent().getParcelableExtra(EXTRA_PRINTER_ID); 67 } else { 68 if (DEBUG) Log.i(TAG, "No job info or printer info to show. Exiting."); 69 finish(); 70 return; 71 } 72 ActionBar actionBar = getActionBar(); 73 if (actionBar != null) { 74 actionBar.setDisplayHomeAsUpEnabled(true); 75 } 76 getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 77 } 78 79 @Override onOptionsItemSelected(MenuItem item)80 public boolean onOptionsItemSelected(MenuItem item) { 81 switch (item.getItemId()) { 82 case android.R.id.home: 83 onBackPressed(); 84 return true; 85 } 86 return super.onOptionsItemSelected(item); 87 } 88 89 @Override onStart()90 protected void onStart() { 91 super.onStart(); 92 bindService(new Intent(this, BuiltInPrintService.class), this, 93 Context.BIND_AUTO_CREATE); 94 } 95 96 @Override onStop()97 protected void onStop() { 98 super.onStop(); 99 if (mPrintService != null) { 100 mPrintService.getDiscovery().stop(this); 101 } 102 unbindService(this); 103 } 104 105 @Override onDestroy()106 protected void onDestroy() { 107 super.onDestroy(); 108 mExecutorService.shutdownNow(); 109 } 110 111 @Override onServiceConnected(ComponentName name, IBinder service)112 public void onServiceConnected(ComponentName name, IBinder service) { 113 mPrintService = BuiltInPrintService.getInstance(); 114 mPrintService.getDiscovery().start(this); 115 } 116 117 @Override onServiceDisconnected(ComponentName name)118 public void onServiceDisconnected(ComponentName name) { 119 mPrintService = null; 120 } 121 122 @Override onPrinterFound(DiscoveredPrinter printer)123 public void onPrinterFound(DiscoveredPrinter printer) { 124 if (printer.getUri().toString().equals(mPrinterId.getLocalId())) { 125 // We discovered a printer matching the job's PrinterId, so show recommendations 126 mPrinter = printer; 127 setTitle(mPrinter.name); 128 mExecutorService.execute(() -> { 129 try { 130 mPrinterAddress = InetAddress.getByName(mPrinter.path.getHost()); 131 // No need for continued discovery after we find the printer. 132 mPrintService.getDiscovery().stop(this); 133 if (!mExecutorService.isShutdown() && mPrintService != null) { 134 mPrintService.getMainHandler().post(() -> { 135 if (getFragmentManager().findFragmentByTag(TAG) == null) { 136 MoreOptionsFragment fragment = new MoreOptionsFragment(); 137 getFragmentManager().beginTransaction() 138 .replace(android.R.id.content, fragment, TAG) 139 .commit(); 140 } 141 }); 142 } 143 } catch (UnknownHostException ignored) { } 144 }); 145 } 146 } 147 148 @Override onPrinterLost(DiscoveredPrinter printer)149 public void onPrinterLost(DiscoveredPrinter printer) { 150 // Ignore 151 } 152 } 153