Ray Zhang 5 лет назад
Родитель
Сommit
10d720f55b

+ 1 - 1
ApexDrivers/RAUtilsLibrary/build.gradle

@@ -59,7 +59,7 @@ dependencies {
     api files('libs/httpmime-4.1.1.jar')
     api files('libs/httpmime-4.1.1.jar')
 
 
     api files('libs/core-2.3.0.jar')
     api files('libs/core-2.3.0.jar')
-    api 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
+//    api 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
     implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
     implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
 }
 }
 
 

+ 1 - 1
ApexDrivers/RAUtilsLibrary/src/main/AndroidManifest.xml

@@ -68,7 +68,7 @@
             android:stateNotNeeded="true"
             android:stateNotNeeded="true"
             android:theme="@style/CaptureTheme"
             android:theme="@style/CaptureTheme"
             android:windowSoftInputMode="stateAlwaysHidden" />
             android:windowSoftInputMode="stateAlwaysHidden" />
-        <activity android:name=".preview.RAPDFPreviewActivity" />
+
         <activity
         <activity
             android:name=".infinitephoto.InfinitePhotoActivity"
             android:name=".infinitephoto.InfinitePhotoActivity"
             android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" />
             android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" />

+ 33 - 0
ApexDrivers/rapdflib/build.gradle

@@ -0,0 +1,33 @@
+apply plugin: 'com.android.library'
+
+android {
+    compileSdkVersion 29
+
+    defaultConfig {
+        minSdkVersion 24
+        targetSdkVersion 29
+        versionCode 1
+        versionName "1.0"
+
+        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
+        consumerProguardFiles "consumer-rules.pro"
+    }
+
+    buildTypes {
+        release {
+            minifyEnabled false
+            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
+        }
+    }
+}
+
+dependencies {
+    implementation fileTree(dir: "libs", include: ["*.jar"])
+    implementation 'androidx.appcompat:appcompat:1.2.0'
+    testImplementation 'junit:junit:4.12'
+    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
+    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
+    api 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
+    api project(path: ':RAUtilsLibrary')
+
+}

+ 0 - 0
ApexDrivers/rapdflib/consumer-rules.pro


+ 21 - 0
ApexDrivers/rapdflib/proguard-rules.pro

@@ -0,0 +1,21 @@
+# Add project specific ProGuard rules here.
+# You can control the set of applied configuration files using the
+# proguardFiles setting in build.gradle.
+#
+# For more details, see
+#   http://developer.android.com/guide/developing/tools/proguard.html
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+#   public *;
+#}
+
+# Uncomment this to preserve the line number information for
+# debugging stack traces.
+#-keepattributes SourceFile,LineNumberTable
+
+# If you keep the line number information, uncomment this to
+# hide the original source file name.
+#-renamesourcefileattribute SourceFile

+ 26 - 0
ApexDrivers/rapdflib/src/androidTest/java/com/redant/usai/rapdflib/ExampleInstrumentedTest.java

@@ -0,0 +1,26 @@
+package com.redant.usai.rapdflib;
+
+import android.content.Context;
+
+import androidx.test.platform.app.InstrumentationRegistry;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.*;
+
+/**
+ * Instrumented test, which will execute on an Android device.
+ *
+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
+ */
+@RunWith(AndroidJUnit4.class)
+public class ExampleInstrumentedTest {
+    @Test
+    public void useAppContext() {
+        // Context of the app under test.
+        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+        assertEquals("com.redant.usai.rapdflib.test", appContext.getPackageName());
+    }
+}

+ 8 - 0
ApexDrivers/rapdflib/src/main/AndroidManifest.xml

@@ -0,0 +1,8 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.redant.usai.rapdflib">
+    <application
+        android:allowBackup="true"
+        android:supportsRtl="true">
+    <activity android:name=".preview.RAPDFPreviewActivity" />
+    </application>
+</manifest>

+ 274 - 0
ApexDrivers/rapdflib/src/main/java/com/redant/usai/rapdflib/preview/RAPDFPreviewActivity.java

@@ -0,0 +1,274 @@
+package com.redant.usai.rapdflib.preview;
+
+import android.app.AlertDialog;
+import android.app.Application;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.graphics.Color;
+import android.net.Uri;
+import android.os.Build;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.Gravity;
+import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.TextView;
+
+import androidx.appcompat.app.ActionBar;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.core.content.FileProvider;
+
+import com.github.barteksc.pdfviewer.PDFView;
+import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
+import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
+import com.github.barteksc.pdfviewer.listener.OnPageErrorListener;
+import com.github.barteksc.pdfviewer.util.FitPolicy;
+import com.redant.usai.rapdflib.R;
+import com.shockwave.pdfium.PdfDocument;
+//import com.usai.redant.rautils.R;
+import com.usai.redant.rautils.utils.FileManager;
+import com.usai.redant.rautils.utils.RAProviderHelper;
+
+import java.io.File;
+import java.util.List;
+
+public class RAPDFPreviewActivity extends AppCompatActivity implements OnPageChangeListener, OnLoadCompleteListener,OnPageErrorListener {
+
+    private static final String FilePathKey = "file";
+
+    public static void test() {
+
+    }
+
+    public static void startPreviewActivity(Context context, String filePath) {
+        if (context == null || filePath == null) {
+            return;
+        }
+        Intent intent = new Intent(context, RAPDFPreviewActivity.class);
+        intent.putExtra(FilePathKey, filePath);
+        context.startActivity(intent);
+    }
+
+    private static final String TAG = RAPDFPreviewActivity.class.getSimpleName();
+
+    private PDFView pdfView;
+    private Integer pageNumber = 0;
+
+    protected void openFile() {
+
+        File file = new File(mFilePath);
+        if (file == null || !file.exists()) {
+            return;
+        }
+
+        Uri uri = null;
+        String type = FileManager.getMimeType(file.getPath());
+
+        // type "application/pdf"
+        Intent intent = new Intent(Intent.ACTION_VIEW);
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+
+            // "com.usai.apex.fileprovider"即是在Manifest文件中配置的authorities
+
+            Application application = getApplication();
+            if (application != null && application instanceof RAProviderHelper.ProviderHelperDelegate) {
+
+                String authority = ((RAProviderHelper.ProviderHelperDelegate) application).getProviderAuthorities();
+
+                uri = FileProvider.getUriForFile(this, authority, file);
+                // 给目标应用一个临时授权
+                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+            }
+
+
+        } else {
+            uri = Uri.fromFile(file);
+        }
+
+        intent.setDataAndType(uri, type);
+
+        if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
+            // someone knows how to handle this mime type with this scheme, don't download.
+            try {
+                startActivity(intent);
+                return;
+            } catch (Exception ex) {
+                Log.d("Open File", "activity not found for " + type + " over " + uri, ex);
+            }
+
+        } else {
+            Log.d("Open File", "openFileAtPath: " + "No App " + uri);
+
+
+            AlertDialog.Builder builder = new AlertDialog.Builder(RAPDFPreviewActivity.this);
+            builder.setMessage("Can not open PDF file, no external App found." );
+
+            builder.setTitle("Open PDF");
+
+            builder.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener()
+            {
+
+                @Override
+                public void onClick(DialogInterface dialog, int which)
+                {
+                    dialog.dismiss();
+
+
+                }
+            });
+
+            builder.create().show();
+        }
+
+    }
+
+    protected void shareFile() {
+
+        File file = new File(mFilePath);
+        if (file == null || !file.exists()) {
+            return;
+        }
+
+        Uri uri = null;
+        String type = FileManager.getMimeType(file.getPath());
+
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+
+            Application application = getApplication();
+            if (application != null && application instanceof RAProviderHelper.ProviderHelperDelegate) {
+
+                String authority = ((RAProviderHelper.ProviderHelperDelegate) application).getProviderAuthorities();
+
+                // "com.usai.apex.fileprovider"即是在Manifest文件中配置的authorities
+                uri = FileProvider.getUriForFile(this, authority, file);
+                // 给目标应用一个临时授权
+            }
+
+        } else {
+            uri = Uri.fromFile(file);
+        }
+
+        Intent share = new Intent();
+        share.setAction(Intent.ACTION_SEND);
+        share.putExtra(Intent.EXTRA_STREAM, uri);
+        share.setType(type);
+
+        startActivity(Intent.createChooser(share, "Share"));
+    }
+
+    private String mFilePath;
+    private void setCustomActionBar() {
+
+        ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
+        View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null);
+
+        String title=new File(mFilePath).getName();
+        TextView mTitleView= mActionBarView.findViewById(R.id.title);
+
+        mTitleView.setText(title);
+        setTitle(title);
+
+        ActionBar actionBar = getSupportActionBar();
+        if (actionBar != null) {
+            actionBar.setCustomView(mActionBarView, lp);
+            actionBar.setDisplayHomeAsUpEnabled(true);
+            actionBar.setDisplayShowTitleEnabled(true);
+        }
+
+    }
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+
+        int id = item.getItemId();
+
+        if (id == android.R.id.home) {
+            finish();
+        } else if (id == R.id.pdf_preview_action_open) {
+            openFile();
+        } else if (id == R.id.pdf_preview_action_send) {
+            shareFile();
+        }
+
+        return super.onOptionsItemSelected(item);
+    }
+
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu)
+    {
+        // Inflate the menu; this adds items to the action bar if it is present.
+        getMenuInflater().inflate(R.menu.pdf_preview_menu, menu);
+        return true;
+    }
+
+    @Override
+    public void onDestroy()
+    {
+        super.onDestroy();
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.pdf_preview_activity);
+
+        mFilePath = getIntent().getStringExtra(FilePathKey);
+
+        setCustomActionBar();
+
+        pdfView = findViewById(R.id.pdfView);
+        pdfView.setBackgroundColor(Color.LTGRAY);
+        pdfView.setPadding(10,10,10,10);
+        pdfView.fromFile(new File(mFilePath))
+                .defaultPage(pageNumber)
+                .onPageChange(this)
+                .enableAnnotationRendering(true)
+                .onLoad(this)
+                .spacing(10) // in dp
+                .onPageError(this)
+                .pageFitPolicy(FitPolicy.BOTH)
+                .load();
+
+    }
+
+    @Override
+    public void loadComplete(int nbPages) {
+        PdfDocument.Meta meta = pdfView.getDocumentMeta();
+        Log.e(TAG, "title = " + meta.getTitle());
+        Log.e(TAG, "author = " + meta.getAuthor());
+        Log.e(TAG, "subject = " + meta.getSubject());
+        Log.e(TAG, "keywords = " + meta.getKeywords());
+        Log.e(TAG, "creator = " + meta.getCreator());
+        Log.e(TAG, "producer = " + meta.getProducer());
+        Log.e(TAG, "creationDate = " + meta.getCreationDate());
+        Log.e(TAG, "modDate = " + meta.getModDate());
+
+        printBookmarksTree(pdfView.getTableOfContents(), "-");
+    }
+
+    @Override
+    public void onPageChanged(int page, int pageCount) {
+
+    }
+
+    @Override
+    public void onPageError(int page, Throwable t) {
+
+    }
+
+    public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
+        for (PdfDocument.Bookmark b : tree) {
+
+            Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));
+
+            if (b.hasChildren()) {
+                printBookmarksTree(b.getChildren(), sep + "-");
+            }
+        }
+    }
+}

+ 14 - 0
ApexDrivers/rapdflib/src/main/res/layout/pdf_preview_activity.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context="com.usai.redant.rautils.preview.RAPDFPreviewActivity">
+
+    <com.github.barteksc.pdfviewer.PDFView
+        android:id="@+id/pdfView"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"/>
+
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 17 - 0
ApexDrivers/rapdflib/src/test/java/com/redant/usai/rapdflib/ExampleUnitTest.java

@@ -0,0 +1,17 @@
+package com.redant.usai.rapdflib;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Example local unit test, which will execute on the development machine (host).
+ *
+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
+ */
+public class ExampleUnitTest {
+    @Test
+    public void addition_isCorrect() {
+        assertEquals(4, 2 + 2);
+    }
+}

+ 1 - 0
ApexDrivers/settings.gradle

@@ -1,3 +1,4 @@
+include ':rapdflib'
 include ':RALocationLib'
 include ':RALocationLib'
 include ':FunctionTest'
 include ':FunctionTest'
 include ':apexdriverslib', ':apexdriverscn', ':apexdriversi', ':apexcrm', ':RAUtilsLibrary', ':raimage', ':apexmobile', ':apexmap', ':racameralib'
 include ':apexdriverslib', ':apexdriverscn', ':apexdriversi', ':apexcrm', ':RAUtilsLibrary', ':raimage', ':apexmobile', ':apexmap', ':racameralib'