File indexing completed on 2024-05-26 04:37:33

0001 package org.kde.something;
0002 
0003 import android.content.ContentResolver;
0004 import android.content.Intent;
0005 import android.util.Log;
0006 import android.os.Bundle;
0007 import android.os.ParcelFileDescriptor;
0008 import android.net.Uri;
0009 import android.app.Activity;
0010 
0011 import java.io.FileNotFoundException;
0012 
0013 import org.qtproject.qt5.android.bindings.QtActivity;
0014 
0015 class FileClass
0016 {
0017     public static native void openUri(String uri);
0018 }
0019 
0020 public class OpenFileActivity extends QtActivity
0021 {
0022 
0023     public String contentUrlToFd(String url)
0024     {
0025         try {
0026             ContentResolver resolver = getBaseContext().getContentResolver();
0027             ParcelFileDescriptor fdObject = resolver.openFileDescriptor(Uri.parse(url), "r");
0028             return "fd:///" + fdObject.detachFd();
0029         } catch (FileNotFoundException e) {
0030             Log.e("Okular", "Cannot find file", e);
0031         }
0032         return "";
0033     }
0034 
0035 
0036     private void displayUri(Uri uri)
0037     {
0038         if (uri == null)
0039             return;
0040 
0041         if (!uri.getScheme().equals("file")) {
0042             try {
0043                 ContentResolver resolver = getBaseContext().getContentResolver();
0044                 ParcelFileDescriptor fdObject = resolver.openFileDescriptor(uri, "r");
0045                 uri = Uri.parse("fd:///" + fdObject.detachFd());
0046             } catch (Exception e) {
0047                 e.printStackTrace();
0048 
0049                 //TODO: emit warning that couldn't be opened
0050                 Log.e("Okular", "failed to open");
0051                 return;
0052             }
0053         }
0054 
0055         Log.e("Okular", "opening url: " + uri.toString());
0056         FileClass.openUri(uri.toString());
0057     }
0058 
0059     public void handleViewIntent() {
0060         final Intent bundleIntent = getIntent();
0061         if (bundleIntent == null)
0062             return;
0063 
0064         final String action = bundleIntent.getAction();
0065         Log.v("Okular", "Starting action: " + action);
0066         if (action == "android.intent.action.VIEW") {
0067             displayUri(bundleIntent.getData());
0068         }
0069     }
0070 }