com.example.nitish.welcomapp.activitypt.ElementDetailsActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.example.nitish.welcomapp.activitypt.ElementDetailsActivity.java

Source

/*
 * The MIT License (MIT)
 * Copyright  2016 Steve Guidetti
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the Software?), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED AS IS?, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.example.nitish.welcomapp.activitypt;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.example.nitish.welcomapp.R;
import com.example.nitish.welcomapp.utilitypt.ActionBarCompat;
import com.example.nitish.welcomapp.utilitypt.CommonMenuHandler;
import com.example.nitish.welcomapp.utilitypt.PreferenceUtils;

/**
 * This Activity holds the ElementDetailsFragment. It can be launched by an Intent with an extra
 * identifying the element by atomic number.
 *
 * @author Steve Guidetti
 */
public class ElementDetailsActivity extends FragmentActivity {
    /**
     * The tag to identify the Activity
     */
    private static final String TAG = "ElementDetailsActivity";

    /**
     * Intent extras
     */
    public static final String EXTRA_ATOMIC_NUMBER = "atomic_number";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final boolean darkTheme = PreferenceUtils.getPrefDarkTheme();
        setTheme(darkTheme ? R.style.DarkTheme : R.style.LightTheme);

        super.onCreate(savedInstanceState);

        ActionBarCompat.setDisplayHomeAsUpEnabled(this, true);

        if (savedInstanceState == null) {
            final Intent intent = getIntent();
            Fragment fragment = null;
            if (intent.hasExtra(EXTRA_ATOMIC_NUMBER)) {
                fragment = ElementDetailsFragment.getInstance(intent.getIntExtra(EXTRA_ATOMIC_NUMBER, 0));
            } else if (getIntent().getData() != null) {
                final Uri uri = getIntent().getData();
                if (uri.getHost().equals("element")) {
                    final String path = uri.getPathSegments().get(0);
                    if (TextUtils.isDigitsOnly(path)) {
                        try {
                            fragment = ElementDetailsFragment
                                    .getInstance(Integer.parseInt(uri.getPathSegments().get(0)));
                        } catch (NumberFormatException e) {
                            Log.w(TAG, "Invalid atomic number");
                        }
                    } else {
                        fragment = ElementDetailsFragment.getInstance(path);
                    }
                }
            }
            if (fragment != null) {
                getSupportFragmentManager().beginTransaction().add(android.R.id.content, fragment).commit();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        final MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.common, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        final int id = item.getItemId();
        switch (id) {
        case android.R.id.home:
            finish();
            return true;
        }
        return CommonMenuHandler.handleSelect(this, id) || super.onOptionsItemSelected(item);
    }
}