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

Java tutorial

Introduction

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

Source

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

import android.content.Intent;
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.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
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;
import java.util.ArrayList;
import java.util.List;

public class HelpFOSSActivity extends AppCompatActivity {

    private List<FOSSComponent> fossComponents;

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

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

        // Find stuff
        Toolbar appBar = (Toolbar) findViewById(R.id.activity_help_foss_app_bar);
        RecyclerView list = (RecyclerView) findViewById(R.id.activity_help_foss_list);

        // 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);

        // Configure list
        list.setAdapter(new ListAdapter());
        list.setLayoutManager(new LinearLayoutManager(this));

        // Create list for licensed FOSS components
        fossComponents = new ArrayList<>();

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

            // Parse component array
            JSONArray componentArray = (JSONArray) new JSONTokener(componentFileBuffer.toString()).nextValue();

            // Iterate over components
            for (int i = 0; i < componentArray.length(); i++) {
                JSONObject component = componentArray.getJSONObject(i);

                // Add component to list
                fossComponents.add(new FOSSComponent(component.getString("name"),
                        component.getString("licenseName"), component.getString("licenseTextURL")));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

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

        return super.onOptionsItemSelected(item);
    }

    private class ListViewHolder extends RecyclerView.ViewHolder {

        private View itemView;

        private ListViewHolder(View itemView) {
            super(itemView);

            this.itemView = itemView;
        }

        private View getItemView() {
            return itemView;
        }

    }

    private class ListAdapter extends RecyclerView.Adapter<ListViewHolder> {

        @Override
        public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ListViewHolder(LayoutInflater.from(HelpFOSSActivity.this)
                    .inflate(R.layout.activity_help_foss_list_item, parent, false));
        }

        @Override
        public void onBindViewHolder(final ListViewHolder holder, int position) {
            // Get component for this position
            final FOSSComponent component = fossComponents.get(position);

            // Find stuff
            TextView componentName = (TextView) holder.getItemView()
                    .findViewById(R.id.activity_help_foss_list_item_component_name);
            TextView licenseName = (TextView) holder.getItemView()
                    .findViewById(R.id.activity_help_foss_list_item_license_name);

            // Set item information
            componentName.setText(component.getName());
            licenseName.setText(component.getLicenseName());

            // Listen for clicks
            holder.getItemView().setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // Create intent to help FOSS viewer activity
                    Intent intent = new Intent(HelpFOSSActivity.this, HelpFOSSViewerActivity.class);

                    // Pass FOSS component index to viewer
                    intent.putExtra("componentIndex", holder.getAdapterPosition());

                    // Start activity
                    startActivity(intent);
                }

            });
        }

        @Override
        public int getItemCount() {
            return fossComponents.size();
        }

    }

    private class FOSSComponent {

        private String name;
        private String licenseName;
        private String licenseTextURL;

        private FOSSComponent(String name, String licenseName, String licenseTextURL) {
            this.name = name;
            this.licenseName = licenseName;
            this.licenseTextURL = licenseTextURL;
        }

        private String getName() {
            return name;
        }

        private void setName(String name) {
            this.name = name;
        }

        private String getLicenseName() {
            return licenseName;
        }

        private void setLicenseName(String licenseName) {
            this.licenseName = licenseName;
        }

        private String getLicenseTextURL() {
            return licenseTextURL;
        }

        private void setLicenseTextURL(String licenseTextURL) {
            this.licenseTextURL = licenseTextURL;
        }

    }

}