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