Java Question How to use code with lambda?

Discussion in 'Libraries developers questions' started by DonManfred, May 11, 2019.

  1. DonManfred

    DonManfred Expert Licensed User

    I was asked to help creating a wrapper for TapKey Securitykeys.

    They provide sample Androidapplication in which they are using lambda.

    https://developers.tapkey.io/mobile/android/getting_started/

    The Example app can be found here: https://github.com/tapkey/android-s...src/main/java/net/tpky/demoapp/authentication

    In the mainactivity it starts with a check.
    https://github.com/tapkey/android-s.../main/java/net/tpky/demoapp/MainActivity.java
    Code:
    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {

        
    private static final String TAG = MainActivity.class.getSimpleName();

        
    private UserManager userManager;
        
    private Auth0PasswordIdentityProvider passwordIdentityProvider;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            /*
             * Retrieve the TapkeyServiceFactory singleton.
             */
            App app = (App) getApplication();
            TapkeyServiceFactory tapkeyServiceFactory = app.getTapkeyServiceFactory();

            ConfigManager configManager = tapkeyServiceFactory.getConfigManager();
            userManager = tapkeyServiceFactory.getUserManager();
            passwordIdentityProvider = app.getPasswordIdentityProvider();

            /*
             * When no user 
    is signed in, redirect to login LoginActivity
             */
            
    if(!userManager.hasUsers()){
                
    Intent intent = new Intent(this, LoginActivity.class);
                
    startActivity(intent);
                finish();
                
    return;
            
    }

            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);

            View headerView = navigationView.getHeaderView(0);
            TextView usernameTextView = (TextView) headerView.findViewById(R.id.nav_header__username);

            /*
             * Get the username of first user and display it in side menu. The first user is used here,
             * because we don't support multi-user yet anyways.
             */
            User firstUser = userManager.getFirstUser();
            if(firstUser != null){
                usernameTextView.setText(firstUser.getIpUserName());
            }

            /*
             * Verify, that app is compatible
             */
            configManager.getAppState(CancellationTokens.None)

                    // asynchronously receive the operation result on the UI thread
                    .continueOnUi((Func1<ConfigManager.AppState, Void, Exception>) appState -> {

                        // some time might have passed and maybe the activity is already finishing.
                        if (isFinishing())
                            return null;


                        switch (appState) {

                            // SDK Version is not supported anymore
                            case CLIENT_TOO_OLD:

                                new AlertDialog.Builder(MainActivity.this)
                                        .setMessage(R.string.main_app_too_old)
                                        .setPositiveButton(R.string.app_too_old_button, new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                finish();
                                            }
                                        })
                                        .setOnDismissListener(dialogInterface -> {

                                            // TODO: Redirect to the app store, to let the user
                                            // download the latest app version.

                                            finish();
                                        })
                                        .create()
                                        .show();

                                break;

                            // This version of the SDK is still supported but is short before expiring and should be updated.
                            case CLIENT_UPDATE_SUGGESTED:

                                new AlertDialog.Builder(MainActivity.this)
                                        .setMessage(R.string.newer_app_available)
                                        .setPositiveButton(R.string.newer_app_available_button, null)
                                        .create()
                                        .show();

                                break;

                            case CLIENT_VERSION_OK:
                            default:
                        }

                        return null;
                    })

                    // Handle exceptions raised in async code.
                    .catchOnUi(e -> {

                        // Handle error
                        Log.e(TAG, "failed to fetch app state: ", e);
                        return null;
                    })

                    // Make sure, no exceptions get lost.
                    .conclude();
        }
    How can i use such code in my wrapper?

    Do i need to develop the library with Android Studio? As of now i am using Eclipse for syntaxhelp and SLC to compile.

    I can into an error in SLC for sure

    Any suggestions/hints?

    PD: The Device used is using Android 7+
     
    Last edited: May 11, 2019
  2. moster67

    moster67 Expert Licensed User

    I am not at home now but you should be able to replace the Lamda with an Anonymous class. After that you can compile with SLC.
     
  3. moster67

    moster67 Expert Licensed User

    Try the modified MainActivity.java class I have attached.

    As a general note, I think it is much easier to work and wrap libs with Android Studio.
    In this case, in Android Studio, you can simply highlight the "->" sign and then type ALT + ENTER and you get an option to automatically convert Lamda into an Anonymous class. Now you can compile with SLC.
     

    Attached Files:

    DonManfred likes this.
  4. DonManfred

    DonManfred Expert Licensed User

    I tried to follow the Tutorial about Android Studio and i failed. I was using a recent AS though.
    Can you upload a AS-Project with a small example wrapper? Or help me configuring AS or the build scripts using a Remotedesktopconnection (would like to donate for a bit help to get started)?
    in the beginning i was not sure that i understand what a anonymous class is. But after using this "trick" i know better. Thank you for this tip! At least i could use AS now to convert the code to not use lamda :)
    The code in the example main was just an example. It was more important to adapt the Authproviderclass.

    I now converted the java files in AS to use anonymous classes and then i copied over the files to my Eclipse project. And, hopefully, i do come futher now. I´ll inform you...

    Many thanks for this - really useful - hint :)
     
  5. DonManfred

    DonManfred Expert Licensed User

    After i now learned that i need to replace the Lamda starting from the most inner methods i now was able to convert all of the Lamda from the authentificationclasses.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice