4
\$\begingroup\$

This has been an experience. I'm well above "hello world" java is not a language I use often when programming and I don't program too much. I find myself a lot on Google.... basically just about for every task. Which has brought me here. Before I continue on I would like a critique of my work so I don't have to change a great deal if things are off.

The goal is to create a dynamically created login screen, adding the appropriate buttons for the social networks the user has installed on their device. (As I type that I question the reasoning for that... except as an excruciating coding exercise, lol.) Regardless I believe I would find a critique useful and beneficial as I'm sure I'll encounter more situations where the lessons can be applied.

A few things have stuck out at me:

  • Excessive exception handling
  • JSON not as easy in Java as PHP
  • Best approach for global constants

For constants I did the following, and made them all arrays just to make life easier later:

public final class Globals {
    // .. More constants
    public static final class SocialMedia {
        public static final String[] Facebook = {
                    "com.facebook.orca",
                    "com.facebook.katana",
                    "com.example.facebook",
                    "com.facebook.android"
            };
        public static final String[] Twitter = {"com.twitter.android"};
    }
    // .. More constants
}

My onCreate method for the Login activity seems overly cluttered with needless exception handling (at least IDK what to do... and in some instances the "problem" is legal). It has me questioning whether or not JSON is the best way to store the app state across activities. That and accessing nested JSONObjects seems to be a bit more work than PHP (i.e. PHP: if($myJSON["SocialNetworkInstalled"]["Facebook"] == false) )

Lastly, to iterate over constants that could be a growing list, I chose reflection. Not really sure if my approach to global constants and accessing them is the best.

onCreate() Beginning:

public class LoginActivity extends AppCompatActivity implements
    View.OnClickListener {

private JSONObject mAppState;
//... More members and methods

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    JSONObject json = new JSONObject();
    int orientation;
    mAppState = ((MyApp) getApplicationContext()).state;

    if(!mAppState.has(Labels.SocialNetworkInstalled)) {
        String nameNetwork;
        boolean bInstalled;

        PackageManager pm = getPackageManager();


        for (Field field : Globals.SocialMedia.class.getDeclaredFields()) {
            if(!field.getType().isAssignableFrom(String[].class)) continue;

            bInstalled = false;
            nameNetwork = field.getName();

            try {
                for (String apkname : (String[]) field.get(null)) {
                    try {
                        pm.getPackageInfo(apkname, 0);
                        bInstalled = true;
                    } catch (PackageManager.NameNotFoundException e) {}
                }
                json.put(nameNetwork, bInstalled);
            }
            catch (IllegalAccessException e) {/*Legal: field.get(null) for statics*/}
            catch (JSONException e) {}
        }

        try {
            mAppState.put(Labels.SocialNetworkInstalled, json);
        } catch (JSONException e) {}
    }

    try {
        json = mAppState.getJSONObject(Labels.SocialNetworkInstalled);
        Iterator<String> i = json.keys();
        String app;
        while(i.hasNext()) {
            app = i.next();
            if(json.getBoolean(app))
                Log.d("L2SN","FOUND " + app);
            else
                Log.d("L2SN","NOT FOUND " + app);
        }
    } catch (JSONException e) {}
    //.....

Seems quite a bit of code for creating and accessing a structure which at that point would resemble something like:

{
    "SocialNetworkInstalled": {
        "Facebook": true,
        "Twitter": false
    }
}
\$\endgroup\$
  • \$\begingroup\$ Please include the bits you left out (more constants, more members and methods, etc.), this may help reviewers find more improvements that can be made. \$\endgroup\$ – 410_Gone Jan 19 '17 at 7:02
  • \$\begingroup\$ Thanks. For a second I thought I could start sending out applications and resumes. :) Will update later tonight. \$\endgroup\$ – Nolan Robidoux Jan 20 '17 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.