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 41 /** 42 * Launched by system in response to a "More Options" request while tracking a printer. 43 */ 44 public class MoreOptionsActivity extends Activity implements ServiceConnection, Discovery.Listener { 45 private static final String TAG = MoreOptionsActivity.class.getSimpleName(); 46 private static final boolean DEBUG = false; 47 48 private BuiltInPrintService mPrintService; 49 PrinterId mPrinterId; 50 DiscoveredPrinter mPrinter; 51 InetAddress mPrinterAddress; 52 53 public static final String EXTRA_PRINTER_ID = "EXTRA_PRINTER_ID"; 54 55 @Override onCreate(Bundle savedInstanceState)56 protected void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 if (getIntent().hasExtra(PrintService.EXTRA_PRINT_JOB_INFO)) { 59 PrintJobInfo jobInfo = 60 getIntent().getParcelableExtra(PrintService.EXTRA_PRINT_JOB_INFO); 61 mPrinterId = jobInfo.getPrinterId(); 62 } else if (getIntent().hasExtra(EXTRA_PRINTER_ID)) { 63 mPrinterId = getIntent().getParcelableExtra(EXTRA_PRINTER_ID); 64 } else { 65 if (DEBUG) Log.i(TAG, "No job info or printer info to show. Exiting."); 66 finish(); 67 return; 68 } 69 ActionBar actionBar = getActionBar(); 70 if (actionBar != null) { 71 actionBar.setDisplayHomeAsUpEnabled(true); 72 } 73 getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 74 } 75 76 @Override onOptionsItemSelected(MenuItem item)77 public boolean onOptionsItemSelected(MenuItem item) { 78 switch (item.getItemId()) { 79 case android.R.id.home: 80 onBackPressed(); 81 return true; 82 } 83 return super.onOptionsItemSelected(item); 84 } 85 86 @Override onStart()87 protected void onStart() { 88 super.onStart(); 89 bindService(new Intent(this, BuiltInPrintService.class), this, 90 Context.BIND_AUTO_CREATE); 91 } 92 93 @Override onStop()94 protected void onStop() { 95 super.onStop(); 96 if (mPrintService != null) { 97 mPrintService.getDiscovery().stop(this); 98 } 99 unbindService(this); 100 } 101 102 @Override onServiceConnected(ComponentName name, IBinder service)103 public void onServiceConnected(ComponentName name, IBinder service) { 104 mPrintService = BuiltInPrintService.getInstance(); 105 mPrintService.getDiscovery().start(this); 106 } 107 108 @Override onServiceDisconnected(ComponentName name)109 public void onServiceDisconnected(ComponentName name) { 110 mPrintService = null; 111 } 112 113 @Override onPrinterFound(DiscoveredPrinter printer)114 public void onPrinterFound(DiscoveredPrinter printer) { 115 if (printer.getUri().toString().equals(mPrinterId.getLocalId())) { 116 // We discovered a printer matching the job's PrinterId, so show recommendations 117 mPrinter = printer; 118 setTitle(mPrinter.name); 119 try { 120 mPrinterAddress = InetAddress.getByName(mPrinter.path.getHost()); 121 if (getFragmentManager().getFragments().isEmpty()) { 122 MoreOptionsFragment fragment = new MoreOptionsFragment(); 123 getFragmentManager().beginTransaction() 124 .replace(android.R.id.content, fragment) 125 .commit(); 126 } 127 // No need for continued discovery after we find the printer. 128 mPrintService.getDiscovery().stop(this); 129 } catch (UnknownHostException ignored) { } 130 } 131 } 132 133 @Override onPrinterLost(DiscoveredPrinter printer)134 public void onPrinterLost(DiscoveredPrinter printer) { 135 // Ignore 136 } 137 } 138