Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.awt.Desktop class

I am using a Google API for android. Since Google API/G Suite Quickstart for android refers to their java examples, I am trying to implement this:

GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    ReadMail.HTTP_TRANSPORT, ReadMail.JSON_FACTORY, clientSecrets, ReadMail.SCOPES)
                    .setDataStoreFactory(ReadMail.DATA_STORE_FACTORY)
                    .setAccessType("offline")
                    .build();


    AuthorizationCodeInstalledApp authCodeInstalledApp = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver());
    Credential credential = authCodeInstalledApp.authorize("user");

The problem, I am encountering now is that Android just support a subset of JDK classes. Therefore java.awt.Desktop isn't supported. But I really need this class, since AuthorizationCodeInstalledApp's authorize() will soon or later call its intern function browse(). This function needs the Desktop class.

Is there a way to get that class for android? Or is there another workaround to authenticate with Google?

like image 442
dnsiv Avatar asked Oct 17 '18 08:10

dnsiv


People also ask

What is import Java AWT?

import java. awt. Graphics means that the Graphics class in the java. awt package is made known to the current class. import java.

Which API is used to launch desktop application?

The Desktop API The Desktop class allows Java applications to launch the native desktop applications that handle URIs or files.


1 Answers

I solved it by myself now. Instead of trying to get the Desktop class from java.awt.Desktop, I just overwrote the on Authorization method:

AuthorizationCodeInstalledApp ab = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()){
        protected void onAuthorization(AuthorizationCodeRequestUrl authorizationUrl) throws IOException {
                String url = (authorizationUrl.build());
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                main_Activity.startActivity(browserIntent);
            }
        };

The reason I did this is because authorize() will call onAuthorization() which will call browse(), which checks if Desktop is supported or not. So by rewriting the onAuthorization() method, I won't need that class anymore. My rewritten class will just start a new Browserwindow with that authorization-URL in your android device.

I hope, I was able to help anyone, who encounteres this problem.

like image 190
dnsiv Avatar answered Oct 17 '22 23:10

dnsiv