Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2016 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.AlertDialog;
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.ServiceConnection;
     24 import android.os.Bundle;
     25 import android.os.IBinder;
     26 import android.preference.Preference;
     27 import android.preference.PreferenceCategory;
     28 import android.preference.PreferenceFragment;
     29 import android.util.Log;
     30 
     31 import com.android.bips.BuiltInPrintService;
     32 import com.android.bips.R;
     33 import com.android.bips.discovery.DiscoveredPrinter;
     34 import com.android.bips.p2p.P2pUtils;
     35 
     36 /**
     37  * Presents a list of printers and the ability to add a new one
     38  */
     39 public class AddPrintersFragment extends PreferenceFragment implements ServiceConnection {
     40     private static final String TAG = AddPrintersFragment.class.getSimpleName();
     41     private static final boolean DEBUG = false;
     42 
     43     private static final String KEY_ADD_BY_IP = "add_by_ip";
     44     private static final String KEY_FIND_WIFI_DIRECT = "find_wifi_direct";
     45     private static final String KEY_SAVED_PRINTERS = "saved_printers";
     46     private static final int ORDER_SAVED = 2;
     47 
     48     private PreferenceCategory mSavedPrintersCategory;
     49     private Preference mAddPrinterByIpPreference;
     50     private BuiltInPrintService mPrintService;
     51 
     52     @Override
     53     public void onCreate(Bundle in) {
     54         if (DEBUG) Log.d(TAG, "onCreate");
     55         super.onCreate(in);
     56         addPreferencesFromResource(R.xml.add_printers_prefs);
     57         mAddPrinterByIpPreference = getPreferenceScreen().findPreference(KEY_ADD_BY_IP);
     58 
     59         Preference findP2pPrintersPreference = getPreferenceScreen().findPreference(
     60                 KEY_FIND_WIFI_DIRECT);
     61         findP2pPrintersPreference.setOnPreferenceClickListener(preference -> {
     62             getFragmentManager().beginTransaction()
     63                     .replace(android.R.id.content, new FindP2pPrintersFragment())
     64                     .addToBackStack(null)
     65                     .commit();
     66             return true;
     67         });
     68 
     69         mSavedPrintersCategory = (PreferenceCategory) getPreferenceScreen()
     70                 .findPreference(KEY_SAVED_PRINTERS);
     71     }
     72 
     73     @Override
     74     public void onStart() {
     75         super.onStart();
     76         if (DEBUG) Log.d(TAG, "onStart");
     77 
     78         getActivity().setTitle(R.string.title_activity_add_printer);
     79         getContext().bindService(new Intent(getContext(), BuiltInPrintService.class), this,
     80                 Context.BIND_AUTO_CREATE);
     81     }
     82 
     83     @Override
     84     public void onStop() {
     85         super.onStop();
     86         if (DEBUG) Log.d(TAG, "onStop");
     87 
     88         getContext().unbindService(this);
     89     }
     90 
     91     @Override
     92     public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
     93         if (DEBUG) Log.d(TAG, "onServiceConnected");
     94         mPrintService = BuiltInPrintService.getInstance();
     95 
     96         mAddPrinterByIpPreference.setOnPreferenceClickListener(preference -> {
     97             AlertDialog dialog = new AddManualPrinterDialog(getActivity(),
     98                     mPrintService.getManualDiscovery());
     99             dialog.setOnDismissListener(d -> updateSavedPrinters());
    100             dialog.show();
    101             return true;
    102         });
    103 
    104         updateSavedPrinters();
    105     }
    106 
    107     @Override
    108     public void onServiceDisconnected(ComponentName componentName) {
    109         mPrintService = null;
    110     }
    111 
    112     private void updateSavedPrinters() {
    113         int savedCount = mPrintService.getDiscovery().getSavedPrinters().size();
    114 
    115         if (savedCount == 0) {
    116             if (getPreferenceScreen().findPreference(mSavedPrintersCategory.getKey()) != null) {
    117                 getPreferenceScreen().removePreference(mSavedPrintersCategory);
    118             }
    119         } else {
    120             if (getPreferenceScreen().findPreference(mSavedPrintersCategory.getKey()) == null) {
    121                 getPreferenceScreen().addPreference(mSavedPrintersCategory);
    122             }
    123 
    124             mSavedPrintersCategory.removeAll();
    125 
    126             // With the service enumerate all saved printers
    127             for (DiscoveredPrinter printer : mPrintService.getDiscovery().getSavedPrinters()) {
    128                 if (DEBUG) Log.d(TAG, "Adding saved printer " + printer);
    129                 PrinterPreference pref = new PrinterPreference(getContext(), mPrintService,
    130                         printer, false);
    131                 pref.setOrder(ORDER_SAVED);
    132                 pref.setOnPreferenceClickListener(preference -> {
    133                     showRemovalDialog(printer);
    134                     return true;
    135                 });
    136                 mSavedPrintersCategory.addPreference(pref);
    137             }
    138         }
    139     }
    140 
    141     private void showRemovalDialog(DiscoveredPrinter printer) {
    142         String message;
    143         if (P2pUtils.isP2p(printer)) {
    144             message = mPrintService.getString(R.string.connects_via_wifi_direct);
    145         } else {
    146             message = mPrintService.getString(R.string.connects_via_network, printer.path);
    147         }
    148         new AlertDialog.Builder(getContext())
    149                 .setTitle(printer.name)
    150                 .setMessage(message)
    151                 .setNegativeButton(android.R.string.cancel, null)
    152                 .setPositiveButton(R.string.forget, (dialog, which) -> {
    153                     mPrintService.getDiscovery().removeSavedPrinter(printer.path);
    154                     updateSavedPrinters();
    155                 }).show();
    156     }
    157 }
    158