コード例 #1
0
  private void createWebPrintJob(WebView webView) {

    PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);

    PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter("MyDocument");

    String jobName = getString(R.string.app_name) + " Print Test";

    printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
  }
 @Override
 public void setListening(boolean isListening) {
   if (mPrintManager != null) {
     if (isListening) {
       mPrintManager.addPrintJobStateChangeListener(this);
       onPrintJobStateChanged(null);
     } else {
       mPrintManager.removePrintJobStateChangeListener(this);
     }
   }
 }
 @Override
 protected void onStartLoading() {
   if (DEBUG) {
     Log.i(LOG_TAG, "onStartLoading()");
   }
   // If we already have a result, deliver it immediately.
   if (!mPrintJobs.isEmpty()) {
     deliverResult(new ArrayList<PrintJobInfo>(mPrintJobs));
   }
   // Start watching for changes.
   if (mPrintJobStateChangeListener == null) {
     mPrintJobStateChangeListener =
         new PrintJobStateChangeListener() {
           @Override
           public void onPrintJobStateChanged(PrintJobId printJobId) {
             onForceLoad();
           }
         };
     mPrintManager.addPrintJobStateChangeListener(mPrintJobStateChangeListener);
   }
   // If the data changed or we have no data - load it now.
   if (mPrintJobs.isEmpty()) {
     onForceLoad();
   }
 }
        @Override
        public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
          List<SearchIndexableRaw> indexables = new ArrayList<SearchIndexableRaw>();

          PackageManager packageManager = context.getPackageManager();
          PrintManager printManager =
              (PrintManager) context.getSystemService(Context.PRINT_SERVICE);

          String screenTitle = context.getResources().getString(R.string.print_settings);
          SearchIndexableRaw data = new SearchIndexableRaw(context);
          data.title = screenTitle;
          data.screenTitle = screenTitle;
          indexables.add(data);

          // Indexing all services, regardless if enabled. Please note that the index will not be
          // updated until this function is called again
          List<PrintServiceInfo> services =
              printManager.getPrintServices(PrintManager.ALL_SERVICES);

          if (services != null) {
            final int serviceCount = services.size();
            for (int i = 0; i < serviceCount; i++) {
              PrintServiceInfo service = services.get(i);

              ComponentName componentName =
                  new ComponentName(
                      service.getResolveInfo().serviceInfo.packageName,
                      service.getResolveInfo().serviceInfo.name);

              data = new SearchIndexableRaw(context);
              data.key = componentName.flattenToString();
              data.title = service.getResolveInfo().loadLabel(packageManager).toString();
              data.summaryOn = context.getString(R.string.print_feature_state_on);
              data.summaryOff = context.getString(R.string.print_feature_state_off);
              data.screenTitle = screenTitle;
              indexables.add(data);
            }
          }

          return indexables;
        }
 @Override
 protected void onReset() {
   if (DEBUG) {
     Log.i(LOG_TAG, "onReset()");
   }
   // Stop loading.
   onStopLoading();
   // Clear the cached result.
   mPrintJobs.clear();
   // Stop watching for changes.
   if (mPrintJobStateChangeListener != null) {
     mPrintManager.removePrintJobStateChangeListener(mPrintJobStateChangeListener);
     mPrintJobStateChangeListener = null;
   }
 }
 @Override
 public List<PrintJobInfo> loadInBackground() {
   List<PrintJobInfo> printJobInfos = null;
   List<PrintJob> printJobs = mPrintManager.getPrintJobs();
   final int printJobCount = printJobs.size();
   for (int i = 0; i < printJobCount; i++) {
     PrintJobInfo printJob = printJobs.get(i).getInfo();
     if (shouldShowToUser(printJob)) {
       if (printJobInfos == null) {
         printJobInfos = new ArrayList<PrintJobInfo>();
       }
       printJobInfos.add(printJob);
     }
   }
   return printJobInfos;
 }
    @Override
    public void onPrintJobStateChanged(PrintJobId printJobId) {
      List<PrintJob> printJobs = mPrintManager.getPrintJobs();

      int numActivePrintJobs = 0;
      final int numPrintJobs = printJobs.size();
      for (int i = 0; i < numPrintJobs; i++) {
        if (shouldShowToUser(printJobs.get(i).getInfo())) {
          numActivePrintJobs++;
        }
      }

      mSummaryLoader.setSummary(
          this,
          mContext
              .getResources()
              .getQuantityString(
                  R.plurals.print_settings_title, numActivePrintJobs, numActivePrintJobs));
    }
コード例 #8
0
 private void print() {
   // Get the print manager.
   PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
   // Pass in the ViewView's document adapter.
   printManager.print("MotoGP stats", mWebView.createPrintDocumentAdapter(), null);
 }