Sfoglia il codice sorgente

PDFTool(Android) - 移除 MediaPlayService, 适配 Android14

liuxiaolong 1 mese fa
parent
commit
0c7e92c2ff

+ 0 - 13
ComPDFKit_Tools/src/main/AndroidManifest.xml

@@ -26,25 +26,12 @@
             android:exported="false"
             android:theme="@style/ComPDFKit.Theme.Dark" />
 
-        <service
-            android:name=".common.utils.voice.CMediaPlayService"
-            android:enabled="true"
-            android:exported="false" />
-
         <activity
             android:name=".common.pdf.CPDFDocumentActivity"
             android:configChanges="keyboardHidden|orientation|screenSize"
             android:exported="true"
             android:theme="@style/ComPDFKit.Theme.Light"
             android:windowSoftInputMode="adjustPan">
-<!--            <intent-filter tools:ignore="AppLinkUrlError"-->
-<!--                android:scheme="http">-->
-<!--                <action android:name="android.intent.action.VIEW" />-->
-<!--                <action android:name="android.intent.action.SEND" />-->
-
-<!--                <category android:name="android.intent.category.DEFAULT" />-->
-<!--                <data android:mimeType="application/pdf" />-->
-<!--            </intent-filter>-->
         </activity>
 
     </application>

+ 21 - 49
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/annotation/pdfproperties/pdfsound/CPlayVoicePopupWindow.java

@@ -11,13 +11,9 @@ package com.compdfkit.tools.annotation.pdfproperties.pdfsound;
 
 import static java.lang.Thread.sleep;
 
-import android.content.ComponentName;
 import android.content.Context;
-import android.content.Intent;
-import android.content.ServiceConnection;
 import android.media.MediaPlayer;
 import android.os.AsyncTask;
-import android.os.IBinder;
 import android.text.TextUtils;
 import android.view.Gravity;
 import android.view.LayoutInflater;
@@ -31,12 +27,12 @@ import androidx.fragment.app.FragmentActivity;
 import com.compdfkit.tools.R;
 import com.compdfkit.tools.common.utils.viewutils.CDimensUtils;
 import com.compdfkit.tools.common.utils.viewutils.CViewUtils;
-import com.compdfkit.tools.common.utils.voice.CMediaPlayService;
+import com.compdfkit.tools.common.utils.voice.CMediaPlayManager;
 import com.compdfkit.tools.common.utils.window.CBasePopupWindow;
 
 import java.io.File;
 
-public class CPlayVoicePopupWindow extends CBasePopupWindow implements CMediaPlayService.IMediaPlayConstants {
+public class CPlayVoicePopupWindow extends CBasePopupWindow implements CMediaPlayManager.IMediaPlayConstants {
 
     private Context context;
 
@@ -50,9 +46,7 @@ public class CPlayVoicePopupWindow extends CBasePopupWindow implements CMediaPla
 
     AsyncTask<Void, Void, Boolean> updateTask = null;
 
-    CMediaPlayService mediaPlayService = null;
-
-    boolean boundService = false;
+    private CMediaPlayManager mediaPlayManager;
 
     boolean isPlay = false;
 
@@ -61,41 +55,18 @@ public class CPlayVoicePopupWindow extends CBasePopupWindow implements CMediaPla
     private void setPlayState(boolean play) {
         isPlay = play;
         if (isPlay == true) {
-            if (mediaPlayService != null) {
-                mediaPlayService.mediaStart();
+            if (mediaPlayManager != null) {
+                mediaPlayManager.mediaStart();
                 imageState.setImageResource(R.drawable.tools_ic_record_stop);
             }
         } else {
-            if (mediaPlayService != null) {
-                mediaPlayService.mediaPause();
+            if (mediaPlayManager != null) {
+                mediaPlayManager.mediaPause();
                 imageState.setImageResource(R.drawable.tools_ic_play_arrow);
             }
         }
     }
 
-    private ServiceConnection serviceConnection = new ServiceConnection() {
-        @Override
-        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
-            boundService = true;
-            mediaPlayService = ((CMediaPlayService.MyBinder) iBinder).get();
-            if (mediaPlayService != null) {
-                mediaPlayService.setMediaPlayConstants(CPlayVoicePopupWindow.this);
-                mediaPlayService.getMediaPlayer().setOnErrorListener((mediaPlayer, i, i1) -> true);
-                mediaPlayService.setPlayFile(voicePath);
-                mediaPlayService.mediaStart();
-            }
-        }
-
-        @Override
-        public void onServiceDisconnected(ComponentName componentName) {
-            boundService = false;
-            if (mediaPlayService != null) {
-                mediaPlayService.stopSelf();
-            }
-            mediaPlayService = null;
-        }
-    };
-
     public CPlayVoicePopupWindow(Context context, View rootView) {
         super(context);
         setAnimationStyle(R.style.tools_popwindow_anim_style);
@@ -131,13 +102,13 @@ public class CPlayVoicePopupWindow extends CBasePopupWindow implements CMediaPla
     protected void onClickListener(View view) {
         int id = view.getId();
         if (id == R.id.id_voice_recording_iv_state) {
-            isPlay = !(mediaPlayService == null ? false : mediaPlayService.isPlaying());
-            if (mediaPlayService != null) {
+            isPlay = !(mediaPlayManager == null ? false : mediaPlayManager.isPlaying());
+            if (mediaPlayManager != null) {
                 if (isPlay) {
-                    mediaPlayService.mediaStart();
+                    mediaPlayManager.mediaStart();
                     imageState.setImageResource(R.drawable.tools_ic_record_stop);
                 } else {
-                    mediaPlayService.mediaPause();
+                    mediaPlayManager.mediaPause();
                     imageState.setImageResource(R.drawable.tools_ic_play_arrow);
                 }
             } else {
@@ -169,8 +140,8 @@ public class CPlayVoicePopupWindow extends CBasePopupWindow implements CMediaPla
                 while (true) {
                     if (mContext instanceof FragmentActivity) {
                         ((FragmentActivity) mContext).runOnUiThread(() -> {
-                            if (mediaPlayService != null && mediaPlayService.getMediaPlayer() != null) {
-                                tvTimeStart.setText(convert(mediaPlayService.getMediaPlayer().getCurrentPosition()) + "");
+                            if (mediaPlayManager != null && mediaPlayManager.getMediaPlayer() != null) {
+                                tvTimeStart.setText(convert(mediaPlayManager.getMediaPlayer().getCurrentPosition()) + "");
                             }
                         });
                     }
@@ -186,11 +157,12 @@ public class CPlayVoicePopupWindow extends CBasePopupWindow implements CMediaPla
     }
 
     public void show() {
-        if (!boundService) {
-            Intent intent = new Intent(mContext, CMediaPlayService.class);
-            mContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
-            startUpdateTask();
-        }
+        mediaPlayManager = new CMediaPlayManager();
+        mediaPlayManager.setMediaPlayConstants(this);
+        mediaPlayManager.getMediaPlayer().setOnErrorListener((mediaPlayer, i, i1) -> true);
+        mediaPlayManager.setPlayFile(voicePath);
+        mediaPlayManager.mediaStart();
+        startUpdateTask();
         if (mContext instanceof FragmentActivity) {
             ((FragmentActivity) mContext).runOnUiThread(() -> {
                 showAtLocation(rootView, Gravity.BOTTOM, 0, CViewUtils.getActionBarSize(rootView.getContext()) + CDimensUtils.dp2px(rootView.getContext(), 32));
@@ -209,8 +181,8 @@ public class CPlayVoicePopupWindow extends CBasePopupWindow implements CMediaPla
         if (!TextUtils.isEmpty(voicePath)) {
             new File(voicePath).delete();
         }
-        if (mediaPlayService != null) {
-            mediaPlayService.onDestroy();
+        if (mediaPlayManager != null) {
+            mediaPlayManager.onDestroy();
         }
         if (updateTask != null) {
             updateTask.cancel(true);

+ 39 - 11
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/basic/fragment/CBasicPDFFragment.java

@@ -11,7 +11,10 @@ package com.compdfkit.tools.common.basic.fragment;
 
 
 import android.net.Uri;
+import android.os.Environment;
+import android.text.TextUtils;
 
+import androidx.documentfile.provider.DocumentFile;
 import androidx.fragment.app.FragmentActivity;
 
 import com.compdfkit.core.annotation.CPDFLinkAnnotation;
@@ -19,6 +22,7 @@ import com.compdfkit.core.annotation.CPDFTextAnnotation;
 import com.compdfkit.core.annotation.form.CPDFComboboxWidget;
 import com.compdfkit.core.annotation.form.CPDFListboxWidget;
 import com.compdfkit.core.annotation.form.CPDFSignatureWidget;
+import com.compdfkit.core.document.CPDFDocument;
 import com.compdfkit.core.edit.CPDFEditPage;
 import com.compdfkit.tools.R;
 import com.compdfkit.tools.annotation.pdfproperties.pdflink.CLinkAnnotAttachHelper;
@@ -32,6 +36,8 @@ import com.compdfkit.tools.common.contextmenu.impl.CSearchReplaceContextMenuView
 import com.compdfkit.tools.common.contextmenu.impl.CSignatureContextMenuView;
 import com.compdfkit.tools.common.pdf.config.CPDFConfiguration;
 import com.compdfkit.tools.common.utils.CFileUtils;
+import com.compdfkit.tools.common.utils.CLog;
+import com.compdfkit.tools.common.utils.CUriUtil;
 import com.compdfkit.tools.common.utils.dialog.CLoadingDialog;
 import com.compdfkit.tools.common.views.pdfview.CPDFViewCtrl;
 import com.compdfkit.tools.common.views.pdfview.CPreviewMode;
@@ -51,9 +57,6 @@ public class CBasicPDFFragment extends CPermissionFragment {
 
     public int curEditMode = CPDFEditPage.LoadNone;
 
-    protected Uri documentUri;
-
-    protected String documentPath;
 
     protected void resetContextMenu(CPDFViewCtrl pdfView, CPreviewMode mode) {
         switch (mode) {
@@ -128,16 +131,41 @@ public class CBasicPDFFragment extends CPermissionFragment {
 
     protected void sharePDF(CPDFViewCtrl pdfView) {
         pdfView.savePDF((filePath, pdfUri) -> {
-            if (null != documentUri) {
-                CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", documentUri);
-            } else {
-                CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", new File(documentPath));
+            if (pdfUri != null && pdfUri.toString().startsWith("content://")){
+                CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", pdfUri);
+                return;
+            }
+            String externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
+            if (!TextUtils.isEmpty(filePath)) {
+                if (filePath.startsWith(getContext().getCacheDir().getAbsolutePath()) ||
+                filePath.startsWith(getContext().getFilesDir().getAbsolutePath())){
+                    CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", new File(filePath));
+                } else if (filePath.startsWith(externalStoragePath)) {
+                    Uri uri = CFileUtils.getUriBySystem(getContext(), new File(filePath));
+                    CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", uri);
+                }
             }
         }, e -> {
-            if (null != documentUri) {
-                CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", documentUri);
-            } else {
-                CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", new File(documentPath));
+
+            CPDFDocument document = pdfView.getCPdfReaderView().getPDFDocument();
+            if (document == null){
+                return;
+            }
+            Uri pdfUri = document.getUri();
+            String filePath = document.getAbsolutePath();
+            if (pdfUri != null && pdfUri.toString().startsWith("content://")){
+                CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", pdfUri);
+                return;
+            }
+            String externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
+            if (!TextUtils.isEmpty(filePath)) {
+                if (filePath.startsWith(getContext().getCacheDir().getAbsolutePath()) ||
+                        filePath.startsWith(getContext().getFilesDir().getAbsolutePath())){
+                    CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", new File(filePath));
+                }else if (filePath.startsWith(externalStoragePath)) {
+                    Uri uri = CFileUtils.getUriBySystem(getContext(), new File(filePath));
+                    CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", uri);
+                }
             }
         });
     }

+ 0 - 5
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/pdf/CPDFApplyConfigUtil.java

@@ -362,20 +362,15 @@ public class CPDFApplyConfigUtil {
         switch (configuration.globalConfig.themeMode) {
             case Light:
                 themeId = R.style.ComPDFKit_Theme_Light;
-                CLog.e("ComPDFKit_Tools", "Theme-getThemeId(Context context, CPDFConfiguration configuration) - Use Light Theme");
                 break;
             case Dark:
                 themeId = R.style.ComPDFKit_Theme_Dark;
-                CLog.e("ComPDFKit_Tools", "Theme-getThemeId(Context context, CPDFConfiguration configuration) - Use Dark Theme");
                 break;
             default:
                 if (CViewUtils.isDarkMode(context)) {
                     themeId = R.style.ComPDFKit_Theme_Dark;
-                    CLog.e("ComPDFKit_Tools", "Theme-getThemeId(Context context, CPDFConfiguration configuration) - Follow the system and use the Dark theme");
-
                 } else {
                     themeId = R.style.ComPDFKit_Theme_Light;
-                    CLog.e("ComPDFKit_Tools", "Theme-getThemeId(Context context, CPDFConfiguration configuration) - Follow the system and use the Light theme");
                 }
                 break;
         }

+ 0 - 3
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/pdf/CPDFDocumentFragment.java

@@ -172,7 +172,6 @@ public class CPDFDocumentFragment extends CBasicPDFFragment {
             if (readerView != null && readerView.getContextMenuShowListener() != null) {
                 readerView.getContextMenuShowListener().dismissContextMenu();
             }
-            this.documentUri = uri;
             CFileUtils.takeUriPermission(getContext(), uri);
             pdfView.resetAnnotationType();
             formToolBar.reset();
@@ -270,12 +269,10 @@ public class CPDFDocumentFragment extends CBasicPDFFragment {
             String password = getArguments().getString(EXTRA_FILE_PASSWORD);
             if (!TextUtils.isEmpty(getArguments().getString(EXTRA_FILE_PATH))) {
                 String path = getArguments().getString(EXTRA_FILE_PATH);
-                documentPath = path;
                 pdfView.openPDF(path, password, callback);
             } else if (getArguments().getParcelable(EXTRA_FILE_URI) != null) {
                 Uri uri = getArguments().getParcelable(EXTRA_FILE_URI);
                 CFileUtils.takeUriPermission(getContext(), uri);
-                documentUri = uri;
                 pdfView.openPDF(uri, password, callback);
             }
         }

+ 35 - 57
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/utils/voice/CMediaPlayService.java

@@ -6,40 +6,18 @@
  * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  * This notice may not be removed from this file.
  */
-
 package com.compdfkit.tools.common.utils.voice;
 
-import android.app.Service;
-import android.content.Intent;
+
 import android.media.MediaPlayer;
-import android.os.Binder;
-import android.os.IBinder;
 import android.text.TextUtils;
 
-import androidx.annotation.Nullable;
-
-public class CMediaPlayService extends Service implements MediaPlayer.OnCompletionListener {
+public class CMediaPlayManager implements MediaPlayer.OnCompletionListener {
     private IMediaPlayConstants iMediaPlayConstants = null;
-    MyBinder mybinder = new MyBinder();
-
-    static MediaPlayer mMediaPlayer = null;
-
-     public class MyBinder extends Binder {
-        public CMediaPlayService service;
-        public CMediaPlayService get() {
-            return CMediaPlayService.this;
-        }
-    }
 
-    @Nullable
-    @Override
-    public IBinder onBind(Intent intent) {
-        return mybinder;
-    }
+    MediaPlayer mMediaPlayer = null;
 
-    @Override
-    public void onCreate() {
-        super.onCreate();
+    public CMediaPlayManager(){
         createPlayer();
     }
 
@@ -50,25 +28,26 @@ public class CMediaPlayService extends Service implements MediaPlayer.OnCompleti
         return mMediaPlayer;
     }
 
-    private void createPlayer() {
-         try {
-             if (mMediaPlayer != null) {
-                 mMediaPlayer.reset();
-                 mMediaPlayer.release();
-                 mMediaPlayer = null;
-             }
-             mMediaPlayer = new MediaPlayer();
-             mMediaPlayer.setOnCompletionListener(this);
-         } catch (Exception e) {
+    private void createPlayer(){
+        try {
+            if (mMediaPlayer != null) {
+                mMediaPlayer.reset();
+                mMediaPlayer.release();
+                mMediaPlayer = null;
+            }
+            mMediaPlayer = new MediaPlayer();
+            mMediaPlayer.setOnCompletionListener(this);
+        } catch (Exception e) {
             e.printStackTrace();
-         }
+        }
     }
 
+
     public boolean isPlaying() {
-         if (mMediaPlayer == null) {
-             return false;
-         }
-         return mMediaPlayer.isPlaying();
+        if (mMediaPlayer == null) {
+            return false;
+        }
+        return mMediaPlayer.isPlaying();
     }
 
     public void setMediaPlayConstants(IMediaPlayConstants iMediaPlayConstants) {
@@ -76,21 +55,21 @@ public class CMediaPlayService extends Service implements MediaPlayer.OnCompleti
     }
 
     public void setPlayFile(String path) {
-         try {
-             if(mMediaPlayer != null && !TextUtils.isEmpty(path)) {
-                 mMediaPlayer.reset();
-                 mMediaPlayer.setDataSource(path);
-                 mMediaPlayer.prepare();
-             }
+        try {
+            if(mMediaPlayer != null && !TextUtils.isEmpty(path)) {
+                mMediaPlayer.reset();
+                mMediaPlayer.setDataSource(path);
+                mMediaPlayer.prepare();
+            }
         } catch (Exception e) {
 
-         }
+        }
     }
 
     public void mediaStart() {
-         if (mMediaPlayer != null && !isPlaying()) {
-             mMediaPlayer.start();
-         }
+        if (mMediaPlayer != null && !isPlaying()) {
+            mMediaPlayer.start();
+        }
     }
 
     public void mediaPause() {
@@ -107,14 +86,12 @@ public class CMediaPlayService extends Service implements MediaPlayer.OnCompleti
 
     @Override
     public void onCompletion(MediaPlayer mediaPlayer) {
-         if (iMediaPlayConstants != null) {
-             iMediaPlayConstants.onCompletion(mediaPlayer);
-         }
+        if (iMediaPlayConstants != null) {
+            iMediaPlayConstants.onCompletion(mediaPlayer);
+        }
     }
 
-    @Override
-    public void onDestroy() {
-        super.onDestroy();
+    public void onDestroy(){
         if (mMediaPlayer != null) {
             mMediaPlayer.stop();
             mMediaPlayer.release();
@@ -123,6 +100,7 @@ public class CMediaPlayService extends Service implements MediaPlayer.OnCompleti
         iMediaPlayConstants = null;
     }
 
+
     public interface IMediaPlayConstants {
         void onCompletion(MediaPlayer mp);
     }

+ 5 - 5
config.gradle

@@ -1,12 +1,12 @@
 ext {
     android = [
-            COMPILESDK: 33,
+            COMPILESDK: 34,
             MINSDK: 21,
-            TARGETSDK: 33,
-            VERSIONCODE: 1015
+            TARGETSDK: 34,
+            VERSIONCODE: 1016
     ]
     sdk = [
-            COMPDFKIT_SDK_VERSION : "2.1.3",
-            COMPDFKIT_SDK_BUILD_TAG : "build_dev_2.1.2_32064baa5_202408091526"
+            COMPDFKIT_SDK_VERSION : "2.2.0",
+            COMPDFKIT_SDK_BUILD_TAG : "build_dev_2.2.0_32064baa5_202408091526"
     ]
 }