Home | History | Annotate | Download | only in ui
      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.annotation.SuppressLint;
     20 import android.app.AlertDialog;
     21 import android.app.Fragment;
     22 import android.net.wifi.p2p.WifiP2pDevice;
     23 import android.os.Bundle;
     24 import android.view.View;
     25 
     26 import com.android.bips.BuiltInPrintService;
     27 import com.android.bips.R;
     28 import com.android.bips.discovery.ConnectionListener;
     29 import com.android.bips.discovery.DiscoveredPrinter;
     30 import com.android.bips.p2p.P2pPrinterConnection;
     31 
     32 /**
     33  * Keep users aware of the P2P connection progress and allow them to cancel.
     34  */
     35 public class AddP2pPrinterDialog extends AlertDialog implements ConnectionListener {
     36     private final WifiP2pDevice mPeer;
     37     private final BuiltInPrintService mPrintService;
     38     private final Fragment mFragment;
     39 
     40     private P2pPrinterConnection mValidating;
     41 
     42     AddP2pPrinterDialog(Fragment fragment, BuiltInPrintService printService, WifiP2pDevice peer) {
     43         super(fragment.getContext());
     44         mFragment = fragment;
     45         mPrintService = printService;
     46         mPeer = peer;
     47     }
     48 
     49     @SuppressLint("InflateParams")
     50     @Override
     51     protected void onCreate(Bundle savedInstanceState) {
     52         // Prepare the dialog for display
     53         setView(getLayoutInflater().inflate(R.layout.manual_printer_add, null));
     54         setTitle(getContext().getString(R.string.connecting_to, mPeer.deviceName));
     55         setButton(AlertDialog.BUTTON_NEGATIVE, getContext().getString(android.R.string.cancel),
     56                 (OnClickListener) null);
     57         super.onCreate(savedInstanceState);
     58 
     59         findViewById(R.id.labelHostname).setVisibility(View.GONE);
     60         findViewById(R.id.hostname).setVisibility(View.GONE);
     61         findViewById(R.id.progress).setVisibility(View.VISIBLE);
     62 
     63         setOnDismissListener(d -> mValidating.close());
     64 
     65         // Attempt to add the discovered device as a P2P printer
     66         mValidating = new P2pPrinterConnection(mPrintService, mPeer, this);
     67     }
     68 
     69     @Override
     70     public void onConnectionComplete(DiscoveredPrinter printer) {
     71         if (printer != null) {
     72             // Callback could arrive quickly if we are already connected, so run later
     73             mPrintService.getMainHandler().post(() -> {
     74                 mValidating.close();
     75                 mPrintService.getP2pDiscovery().addValidPrinter(printer);
     76                 mFragment.getActivity().finish();
     77             });
     78             dismiss();
     79         } else {
     80             fail();
     81         }
     82     }
     83 
     84     @Override
     85     public void onConnectionDelayed(boolean delayed) {
     86         findViewById(R.id.connect_hint).setVisibility(delayed ? View.VISIBLE :
     87                 View.GONE);
     88     }
     89 
     90     private void fail() {
     91         cancel();
     92         new AlertDialog.Builder(getContext())
     93                 .setTitle(getContext().getString(R.string.failed_connection,
     94                         mPeer.deviceName))
     95                 .setPositiveButton(android.R.string.ok, null)
     96                 .show();
     97     }
     98 }
     99