com.photbit.weatherse.app.ui.settings.AboutFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.photbit.weatherse.app.ui.settings.AboutFragment.java

Source

package com.photbit.weatherse.app.ui.settings;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.photbit.weatherse.app.R;
import com.photbit.weatherse.app.ui.base.BaseActivityInterface;
import com.photbit.weatherse.app.ui.utils.DividerItemDecoration;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import de.psdev.licensesdialog.LicensesDialog;
import de.psdev.licensesdialog.model.Notice;

/**
 * Created by Magnus on 2015-01-21
 * TODO improve library for licences to use material dialog on api < 21 (third part lib for dialogs?)
 */
public class AboutFragment extends Fragment implements LibrariesAdapterRecycler.LibrariesAdapterCallback {
    private LibrariesAdapterRecycler mAdapter;
    private Context mContext;
    @BindView(R.id.recycler_list_chart)
    RecyclerView mList;
    private Unbinder mUnbinder;

    public static AboutFragment newInstance() {
        return new AboutFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
        ((BaseActivityInterface) context).setToolbarTitle(mContext.getString(R.string.about));
        mAdapter = new LibrariesAdapterRecycler(getVersion(), this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_list_recycler, container, false);
        mUnbinder = ButterKnife.bind(this, root);
        mList.setLayoutManager(new LinearLayoutManager(mContext));
        DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext);
        itemDecoration.removeHeaderDivider();
        mList.addItemDecoration(itemDecoration);
        mList.setAdapter(mAdapter);
        addLibrariesToList();
        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((BaseActivityInterface) getActivity()).setToolbarTitle(mContext.getString(R.string.about));
    }

    @Override
    public void onPause() {
        super.onPause();
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }

    private void addLibrariesToList() {
        for (int i = 0; i < LicenseConstants.NAMES.length; i++) {
            mAdapter.addLibrary(new Notice(LicenseConstants.NAMES[i], LicenseConstants.URLS[i],
                    LicenseConstants.COPYRIGHTS[i], LicenseConstants.LICENSES[i]));
        }
    }

    @Override
    public void librarySelected(Notice notice) {
        new LicensesDialog.Builder(mContext).setNotices(notice).setThemeResourceId(R.style.AlertDialogStyle).build()
                .show();
    }

    @Override
    public void contactClicked(String contactString) {
        Intent send = new Intent(Intent.ACTION_SENDTO);
        String uriText = "mailto:" + Uri.encode(contactString);
        Uri uri = Uri.parse(uriText);

        send.setData(uri);
        startActivity(Intent.createChooser(send, ""));
    }

    //Get version and versionCode of app
    private String getVersion() {
        try {
            PackageInfo info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            return ("v" + info.versionName + " (" + info.versionCode + ")");
        } catch (PackageManager.NameNotFoundException e) {
            Log.d(AboutFragment.class.getSimpleName(), e.toString());
            return "";
        }
    }
}