2

I am new here and reason for that I don't know all rules of the site yet, sorry about that!

I am building a Java application to Desktop and Android by using Gluon mobile plugin in Eclipse tool. I have variated code for Desktop and Android as described in an example (http://docs.gluonhq.com/samples/gonative/). So I have Android specific NativeService where I have a method to read files from external storage (that works). I have also studied how to request permissions on run time when using native Android environment (Storage permission error in Marshmallow ). I am able to request the permission, but I can not catch the result.

I am now requesting the permissions same way, but how I can receive the results? I cannot overrrive onRequestPermissionsResult as in the example, becauce I have not directly available android.support.v4.app stuff. Could someone help me and provide an example?

AndroidNativeService.java:

package com.tentaattorix;

import java.io.IOException;

import java.io.File;
import android.os.Environment;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.content.Context;
import javafxports.android.FXActivity;
import android.util.Log;
import android.widget.Toast;
import android.content.pm.PackageManager;
import android.os.Build;
import com.avustajat.LueJaKirjoita;

/**
 * Sanaston luku: Android-natiivi toteutus!
 * @author PT
 * @version 24.10.2016
 *
 */
public class AndroidNativeService implements NativeService {

    private static final String TAG = "Tentaattorix Native Service";
    private Sanastot sanastot = new Sanastot();

    private static final int MY_PERMISSIONS_USE_EXTERNAL_STORAGE = 1;
    public AndroidNativeService() {
        //
    }

    @Override
    public Sanastot haeSanastot(String juuriKansio, String sanastoRegex, char kommentti, char erotin) throws IOException {


        String polku = Environment.getExternalStorageDirectory().toString();
        String readme = "LueMinut.txt";
        String kansioPolku ="";

        //Luodaan kansio, jos sitä ei vielä ole.
        File kansio = new File(polku, juuriKansio);
        kansio.mkdir();

        //Asetetaan oikeudet, jos vaikka auttais skannaukseen.
        kansio.setWritable(true);
        kansio.setReadable(true);
        kansio.setExecutable(true);
        kansioPolku = kansio.getAbsolutePath();


        //Kysy oikeudet, jos niitä ei ole!                                       
        if (isStoragePermissionGranted()) {

            //Luodaan kansioon tiedosto LueMinut.txt.
            try {
                LueJaKirjoita.luoLueMinut(kansioPolku, readme);
            }
            catch (IOException e){
                throw e;
            }

            //Informoidaan uudesta kansiosta ja sinne tulevista tiedostoista järjestelmää!
            scanFile(kansioPolku + File.separator + readme);


            //Luetaan sanastot kansiosta.
            sanastot = LueJaKirjoita.lueTiedostot(kansioPolku, sanastoRegex, kommentti, erotin);

        }

        // Jos sanastot ei sisällä yhtään sanastoa, 
        // niin laitetaan edes yksi :) 
        if (sanastot.annaLkm() < 1) {
            String[] rivix = {"Tyhjä sanasto!", "Empty glossary!"};
            Sanasto san = new Sanasto("sanasto_");
            san.lisaa(rivix);
            sanastot.lisaa(san);
        }
        return sanastot;
    }

    /**
     * //Informoidaan uudesta kansiosta ja sinne tulevista tiedostoista järjestelmää!
     * @param path lisätty polku+tiedosto
     */
    private void scanFile(String path) {

        MediaScannerConnection.scanFile(FXActivity.getInstance().getApplicationContext(),
                new String[] { path }, null,
                new MediaScannerConnection.OnScanCompletedListener() {

            public void onScanCompleted(String path, Uri uri) {
                Log.i("TAG", "Finished scanning " + path);
            }
        });
    }

    private  boolean isStoragePermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (FXActivity.getInstance().checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                Log.v(TAG,"Permission is granted");
                return true;
            } else {

                Log.v(TAG,"Permission is revoked");
                FXActivity.getInstance().requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_USE_EXTERNAL_STORAGE);
                return false;
            }
        }
        else { //permission is automatically granted on sdk<23 upon installation
            Log.v(TAG,"Permission is granted");
            return true;
        }
    }
} 
Community
  • 1
  • 1

1 Answers1

3

For starters, you can add the android-support-v4.jar to your project:

Copy it from its location under ANDROID_HOME/extras/android/support/v4/android-support-v4.jar to a libs folder in your project, and then add the dependency to the build.gradle file:

dependencies {
    androidCompile files('libs/android-support-v4.jar')
}

Assuming you are targeting Android SDK 23+:

    android {
        minSdkVersion '23'
        compileSdkVersion '23'
        targetSdkVersion '23'
        manifest = 'src/android/AndroidManifest.xml'
    }

then you know that by default all the permissions included in the manifest will be disabled.

If you want to check for permissions on runtime, you can define a new activity that takes care of requesting permissions with a built-in dialog (using ActivityCompat.requestPermissions), register this activity in the manifest, and call it from the FXActivity within a new intent that passes a list with the required permissions.

You just need to call FXActivity.getInstance().setOnActivityResultHandler() to listen to the end of that activity and resume the call if permissions were granted.

The following code is partly based in the PermissionHelper class.

I'll use the simple case of the Dialer service from the new Charm Down 3.0.0 library, that requires Manifest.permission.CALL_PHONE.

AndroidDialerService.java, under Android package

public class AndroidDialerAskService implements DialerAskService {

    public static final String KEY_PERMISSIONS = "permissions";
    public static final String KEY_GRANT_RESULTS = "grantResults";
    public static final String KEY_REQUEST_CODE = "requestCode";

    @Override
    public void call(String number) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(FXActivity.getInstance(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                FXActivity.getInstance().setOnActivityResultHandler((requestCode, resultCode, data) -> {
                    if (requestCode == 11112) {
                        // if now we have permission, resume call
                        if (ContextCompat.checkSelfPermission(FXActivity.getInstance(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                            call(number);
                        }
                    }
                });

                Intent permIntent = new Intent(FXActivity.getInstance(), PermissionRequestActivity.class);
                permIntent.putExtra(KEY_PERMISSIONS, new String[]{Manifest.permission.CALL_PHONE});
                permIntent.putExtra(KEY_REQUEST_CODE, 11111);
                FXActivity.getInstance().startActivityForResult(permIntent, 11112);
                return;
            }
        }

        if (number != null && !number.isEmpty()) {
            Uri uriNumber = Uri.parse("tel:" + number);
            Intent dial = new Intent(Intent.ACTION_CALL, uriNumber);
            FXActivity.getInstance().startActivity(dial);
        }
    }

    public static class PermissionRequestActivity extends Activity {
        private String[] permissions;
        private int requestCode;

        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
            FXActivity.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
            finish();
        }

        @Override
        protected void onStart() {
            super.onStart();
            permissions = this.getIntent().getStringArrayExtra(KEY_PERMISSIONS);
            requestCode = this.getIntent().getIntExtra(KEY_REQUEST_CODE, 0);

            ActivityCompat.requestPermissions(this, permissions, requestCode);
        }
    }
}

AndroidManifest.xml

. . .
<uses-permission android:name="android.permission.CALL_PHONE"/>
. . .
<activity android:name="javafxports.android.FXActivity" .../>
<activity android:name="com.gluonhq.charm.down.plugins.android.AndroidDialerService$PermissionRequestActivity" />
. . .   

José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • what does 11112 mean in **AndroidDialerService.java** ? @JosePereda – guru_007 Jul 11 '17 at 14:40
  • 1
    @guru_007 That's just a random `requestCode`, that is passed to `startActivityForResult(Intent intent, int requestCode)`, to verify that `setOnActivityResultHandler` is executed only for an Intent with that code. – José Pereda Jul 11 '17 at 16:23
  • Got it ! @ José Pereda! – guru_007 Jul 11 '17 at 17:21
  • `FXActivity.getInstance().setOnActivityResultHandler((requestCode, resultCode, data) -> { //statement });` is unavalible @Jose Pereda – guru_007 Aug 19 '17 at 17:39
  • @guru_007 What do you mean unavailable? Are you using a recent jfxmobile plugin? – José Pereda Aug 20 '17 at 11:43
  • `classpath 'org.javafxports:jfxmobile-plugin:1.3.5` is included ,when am using this `FXActivity.getInstance().setOnActivityResultHandler((request‌​Code, resultCode, data) -> { //statement });` my IDE prompts the error that this method unavailable. – guru_007 Aug 23 '17 at 03:37
  • Then it's an IDE issue. If you run from command line `./gradlew android` it will work. On your IDE (Eclipse?) make sure you have added jfxdvk.jar as a dependency. – José Pereda Aug 23 '17 at 16:28