com.gmail.tylerfilla.axon.ui.activity.HelpFOSSViewerActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.gmail.tylerfilla.axon.ui.activity.HelpFOSSViewerActivity.java

Source

package com.gmail.tylerfilla.axon.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.webkit.WebView;
import com.gmail.tylerfilla.axon.R;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;

public class HelpFOSSViewerActivity extends AppCompatActivity {

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

        // Inflate and set content view
        setContentView(R.layout.activity_help_foss_viewer);

        // Find stuff
        Toolbar appBar = (Toolbar) findViewById(R.id.activity_help_foss_viewer_app_bar);
        WebView browser = (WebView) findViewById(R.id.activity_help_foss_viewer_browser);

        // Configure app bar
        setSupportActionBar(appBar);

        // Display up button in app bar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // Apply dark tint to navigation icon
        // noinspection ConstantConditions
        Drawable navigationIcon = DrawableCompat.wrap(appBar.getNavigationIcon());
        DrawableCompat.setTint(navigationIcon, 0x8b000000);
        appBar.setNavigationIcon(navigationIcon);

        // Get component index which was passed in
        int componentIndex = getIntent().getIntExtra("componentIndex", -1);

        if (componentIndex != -1) {
            // Component information
            String componentName = "";
            String licenseName = "";
            String licenseTextURL = "";

            try {
                // Read components file into a buffer
                StringWriter componentFileBuffer = new StringWriter();
                IOUtils.copy(getAssets().open("foss/components.json"), componentFileBuffer,
                        Charset.defaultCharset());

                // Parse component object from file
                JSONObject component = ((JSONArray) new JSONTokener(componentFileBuffer.toString()).nextValue())
                        .getJSONObject(componentIndex);

                // Extract component information from its JSON object
                componentName = component.getString("name");
                licenseName = component.getString("licenseName");
                licenseTextURL = component.getString("licenseTextURL");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            // Set title and subtitle
            setTitle(componentName);
            appBar.setSubtitle(licenseName);

            // Load license URL
            browser.loadUrl(licenseTextURL);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
        }

        return super.onOptionsItemSelected(item);
    }

}