Search in sources :

Example 6 with PrintDocumentAdapter

use of android.print.PrintDocumentAdapter in project Notepad by farmerbb.

the class MainActivity method createWebPrintJob.

@TargetApi(Build.VERSION_CODES.KITKAT)
private void createWebPrintJob(WebView webView) {
    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);
    // Get a print adapter instance
    PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
    // Create a print job with name and adapter instance
    String jobName = getString(R.string.document, getString(R.string.app_name));
    printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}
Also used : PrintManager(android.print.PrintManager) PrintDocumentAdapter(android.print.PrintDocumentAdapter) TargetApi(android.annotation.TargetApi)

Example 7 with PrintDocumentAdapter

use of android.print.PrintDocumentAdapter in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class PrintJobSettingsActivityTest method viewPrintJobSettings.

@Test
@LargeTest
public void viewPrintJobSettings() throws Exception {
    UUID uuid = UUID.randomUUID();
    Object isWriteCalled = new Object();
    // Create adapter that is good enough to start a print preview
    PrintDocumentAdapter adapter = new PrintDocumentAdapter() {

        @Override
        public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
            callback.onLayoutFinished(new PrintDocumentInfo.Builder(uuid.toString()).build(), true);
        }

        @Override
        public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
            synchronized (isWriteCalled) {
                isWriteCalled.notify();
            }
            callback.onWriteFailed(null);
        }
    };
    Activity activity = mActivityRule.getActivity();
    PrintManager pm = mActivityRule.getActivity().getSystemService(PrintManager.class);
    // Start printing
    PrintJob printJob = pm.print(uuid.toString(), adapter, null);
    // Wait until print preview is up
    synchronized (isWriteCalled) {
        isWriteCalled.wait();
    }
    // Start print job settings
    Intent intent = new Intent(android.provider.Settings.ACTION_PRINT_SETTINGS);
    intent.putExtra(EXTRA_PRINT_JOB_ID, printJob.getId().flattenToString());
    intent.setData(Uri.fromParts("printjob", printJob.getId().flattenToString(), null));
    activity.startActivity(intent);
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject2 printPrefTitle = uiDevice.wait(Until.findObject(By.text("Configuring " + uuid.toString())), 5000);
    assertNotNull(printPrefTitle);
    Log.i(LOG_TAG, "Found " + printPrefTitle.getText());
}
Also used : PrintJob(android.print.PrintJob) Bundle(android.os.Bundle) UiDevice(android.support.test.uiautomator.UiDevice) Activity(android.app.Activity) Intent(android.content.Intent) PrintAttributes(android.print.PrintAttributes) PrintManager(android.print.PrintManager) ParcelFileDescriptor(android.os.ParcelFileDescriptor) UUID(java.util.UUID) PrintDocumentAdapter(android.print.PrintDocumentAdapter) CancellationSignal(android.os.CancellationSignal) UiObject2(android.support.test.uiautomator.UiObject2) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test) LargeTest(androidx.test.filters.LargeTest)

Example 8 with PrintDocumentAdapter

use of android.print.PrintDocumentAdapter in project Stringlate by LonamiWebs.

the class ShareUtil method print.

/**
 * Print a {@link WebView}'s contents, also allows to create a PDF
 *
 * @param webview WebView
 * @param jobName Name of the job (affects PDF name too)
 * @return {{@link PrintJob}} or null
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@SuppressWarnings("deprecation")
public PrintJob print(WebView webview, String jobName) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        PrintDocumentAdapter printAdapter;
        PrintManager printManager = (PrintManager) webview.getContext().getSystemService(Context.PRINT_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            printAdapter = webview.createPrintDocumentAdapter(jobName);
        } else {
            printAdapter = webview.createPrintDocumentAdapter();
        }
        if (printManager != null) {
            return printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
        }
    } else {
        Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
    }
    return null;
}
Also used : PrintDocumentAdapter(android.print.PrintDocumentAdapter) PrintManager(android.print.PrintManager) RequiresApi(android.support.annotation.RequiresApi)

Example 9 with PrintDocumentAdapter

use of android.print.PrintDocumentAdapter in project diary by billthefarmer.

the class Diary method print.

// print
private void print() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        return;
    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);
    String jobName = getString(R.string.appName) + " Document";
    // Get a print adapter instance
    PrintDocumentAdapter printAdapter = markdownView.createPrintDocumentAdapter(jobName);
    // Create a print job with name and adapter instance
    printManager.print(jobName, printAdapter, new PrintAttributes.Builder().setMediaSize(PrintAttributes.MediaSize.ISO_A4).build());
}
Also used : PrintManager(android.print.PrintManager) PrintDocumentAdapter(android.print.PrintDocumentAdapter) PrintAttributes(android.print.PrintAttributes)

Example 10 with PrintDocumentAdapter

use of android.print.PrintDocumentAdapter in project memetastic by gsantner.

the class ShareUtil method print.

/**
 * Print a {@link WebView}'s contents, also allows to create a PDF
 *
 * @param webview WebView
 * @param jobName Name of the job (affects PDF name too)
 * @return {{@link PrintJob}} or null
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@SuppressWarnings("deprecation")
public PrintJob print(WebView webview, String jobName) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        PrintDocumentAdapter printAdapter;
        PrintManager printManager = (PrintManager) webview.getContext().getSystemService(Context.PRINT_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            printAdapter = webview.createPrintDocumentAdapter(jobName);
        } else {
            printAdapter = webview.createPrintDocumentAdapter();
        }
        if (printManager != null) {
            return printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
        }
    } else {
        Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
    }
    return null;
}
Also used : PrintDocumentAdapter(android.print.PrintDocumentAdapter) PrintManager(android.print.PrintManager) RequiresApi(android.support.annotation.RequiresApi)

Aggregations

PrintDocumentAdapter (android.print.PrintDocumentAdapter)11 PrintManager (android.print.PrintManager)11 PrintAttributes (android.print.PrintAttributes)4 TargetApi (android.annotation.TargetApi)3 Intent (android.content.Intent)3 PrintJob (android.print.PrintJob)3 Activity (android.app.Activity)2 Bundle (android.os.Bundle)2 CancellationSignal (android.os.CancellationSignal)2 ParcelFileDescriptor (android.os.ParcelFileDescriptor)2 RequiresApi (android.support.annotation.RequiresApi)2 UiDevice (android.support.test.uiautomator.UiDevice)2 UiObject2 (android.support.test.uiautomator.UiObject2)2 LargeTest (androidx.test.filters.LargeTest)2 UUID (java.util.UUID)2 Test (org.junit.Test)2 SuppressLint (android.annotation.SuppressLint)1 WebView (android.webkit.WebView)1 WebViewClient (android.webkit.WebViewClient)1 RichDocumentsEditorWebView (com.owncloud.android.ui.activity.RichDocumentsEditorWebView)1