Java tutorial
/* Copyright (c) 2013 Richard G. Todd. * Licensed under the terms of the GNU General Public License (GPL) Version 3.0. */ package com.richtodd.android.quiltdesign.app; import org.json.JSONObject; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import com.richtodd.android.quiltdesign.block.JSONRepository; import com.richtodd.android.quiltdesign.block.QuiltDesignThumbnailProvider; import com.richtodd.android.quiltdesign.block.Repository; import com.richtodd.android.quiltdesign.block.Repository.MergeModes; import com.richtodd.android.repository.JSONUtility; import com.richtodd.android.repository.RepositoryException; public class OpenRepositoryActivity extends Activity { private Repository m_repository; private TextView m_text_theme_count; private TextView m_text_block_count; private TextView m_text_quilt_count; private RadioButton m_radio_import_new; private RadioButton m_radio_import_replace; private RadioButton m_radio_import_merge; private Button m_button_import; private Button m_button_cancel_import; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_open_repository); Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras.containsKey(Intent.EXTRA_STREAM)) { Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM); try { JSONObject jsonObject = JSONUtility.loadJSONObject(getContentResolver(), uri); m_repository = new JSONRepository(jsonObject, new QuiltDesignThumbnailProvider()); } catch (RepositoryException e) { Handle.asRuntimeError(e); } } m_text_theme_count = (TextView) findViewById(R.id.text_theme_count); m_text_block_count = (TextView) findViewById(R.id.text_block_count); m_text_quilt_count = (TextView) findViewById(R.id.text_quilt_count); m_radio_import_new = (RadioButton) findViewById(R.id.radio_import_new); m_radio_import_replace = (RadioButton) findViewById(R.id.radio_import_replace); m_radio_import_merge = (RadioButton) findViewById(R.id.radio_import_merge); m_button_import = (Button) findViewById(R.id.button_import); m_button_import.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { Repository repository = Repository.getDefaultRepository(OpenRepositoryActivity.this); repository.merge(m_repository, getMergeMode()); Toast.makeText(OpenRepositoryActivity.this, getString(R.string.toast_openRepository_success), Toast.LENGTH_SHORT).show(); finish(); } catch (RepositoryException e) { Handle.asRuntimeError(e); } } }); m_button_cancel_import = (Button) findViewById(R.id.button_cancel_import); m_button_cancel_import.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); if (m_repository != null) { try { m_text_theme_count.setText(Integer.toString(m_repository.getThemes().getCount())); m_text_block_count.setText(Integer.toString(m_repository.getBlocks().getCount())); m_text_quilt_count.setText(Integer.toString(m_repository.getQuilts().getCount())); } catch (RepositoryException e) { Handle.asRuntimeError(e); } } else { m_text_theme_count.setText("0"); m_text_block_count.setText("0"); m_text_quilt_count.setText("0"); } } private MergeModes getMergeMode() { if (m_radio_import_merge.isChecked()) return MergeModes.Merge; if (m_radio_import_new.isChecked()) return MergeModes.New; if (m_radio_import_replace.isChecked()) return MergeModes.Replace; return MergeModes.New; } }