Java tutorial
/** * Copyright (C) 2012 Picon software * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package fr.eoit.activity.fragment.blueprint; import android.content.ContentUris; import android.content.ContentValues; import android.content.res.Resources; import android.database.Cursor; import android.database.MatrixCursor; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v4.widget.SimpleCursorAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Spinner; import fr.eoit.EOITConst; import fr.eoit.R; import fr.eoit.db.bean.Blueprint; import fr.eoit.db.bean.Item; import fr.eoit.formula.util.DecryptorUtil; import fr.eoit.formula.util.DecryptorUtil.DecryptorBonuses; import fr.eoit.service.calculator.PriceCalculatorService; import fr.eoit.util.ValueCache; import fr.piconsoft.activity.fragment.LoaderFragment; import fr.piconsoft.db.util.DbUtil; import java.lang.ref.WeakReference; /** * @author picon.software * */ public class RequiredDecryptorFragment extends LoaderFragment<Cursor> { private int blueprintId, encryptionSkillId, decryptorId, producedItemId; private WeakReference<InventionFragment> fragmentReference; /** * @param encryptionSkillId the encryptionSkillId to set */ public void setEncryptionSkillId(int encryptionSkillId) { this.encryptionSkillId = encryptionSkillId; initOrRestart(); } /** * @param blueprintId the blueprintId to set */ public void setBlueprintId(int blueprintId) { this.blueprintId = blueprintId; } /** * @param decryptorId the decryptorId to set */ public void setDecryptorId(int decryptorId) { this.decryptorId = decryptorId; } /** * @param producedItemId the producedTypeId to set */ public void setProducedItemId(int producedItemId) { this.producedItemId = producedItemId; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.blueprint_required_decryptor, container, false); } @Override public Loader<Cursor> getCursorLoader(int id, Bundle args) { if (id == LOADER_ID) { return new CursorLoader(getActivity(), Item.CONTENT_ID_URI_BASE, new String[] { Item._ID, Item.COLUMN_NAME_NAME }, Item.COLUMN_NAME_GROUP_ID + " = " + DecryptorUtil.getDecryptorGroupId(encryptionSkillId), null, null); } return null; } @Override public void onLoadFinished(Cursor cursor) { if (DbUtil.hasAtLeastOneRow(cursor)) { String[] from = new String[] { Item.COLUMN_NAME_NAME, "decryptorDesc" }; int[] to = new int[] { android.R.id.text1, android.R.id.text2 }; MatrixCursor decryptorCursor = new MatrixCursor( new String[] { Item._ID, Item.COLUMN_NAME_NAME, "decryptorDesc" }); DbUtil.addRowToMatrixCursor(decryptorCursor, 0L, "None", ""); cursor.moveToFirst(); int selectedItem = 0; while (!cursor.isAfterLast()) { long itemId = cursor.getLong(cursor.getColumnIndexOrThrow(Item._ID)); DbUtil.addRowToMatrixCursor(decryptorCursor, itemId, cursor.getString(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_NAME)), getDecryptorDescription(DecryptorUtil.getDecryptorBonuses(itemId), getActivity().getResources())); if (itemId == decryptorId) { selectedItem = cursor.getPosition() + 1; } cursor.moveToNext(); } SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_item, decryptorCursor, from, to, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); Spinner spinner = (Spinner) getActivity().findViewById(R.id.DECRYPTOR_SPINNER); adapter.setDropDownViewResource(R.layout.decryptor_drop_down_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new SpinnerDecryptorOnItemSelectedListener()); spinner.setSelection(selectedItem); } } @Override public void onLoaderReset(Loader<Cursor> loader) { } private class SpinnerDecryptorOnItemSelectedListener implements OnItemSelectedListener { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (ValueCache.hasChangedAndUpdateCache(EOITConst.BEAN_ID_BLUEPRINT, EOITConst.FIELD_ID_BLUEPRINT_DECRYPTOR, blueprintId, id)) { new UpdateDecryptorIdTask().execute(blueprintId, (int) id); } } @Override public void onNothingSelected(AdapterView<?> parent) { } } private class UpdateDecryptorIdTask extends AsyncTask<Integer, Void, Integer> { @Override protected Integer doInBackground(Integer... params) { int blueprintId = params[0]; int decryptorId = params[1]; DecryptorBonuses currentDecryptorBonuses = DecryptorUtil.getDecryptorBonusesOrDefault(decryptorId); Uri updateBlueprintUri = ContentUris.withAppendedId(Blueprint.CONTENT_ID_URI_BASE, blueprintId); ContentValues values = new ContentValues(); values.put(Blueprint.COLUMN_NAME_DECRYPTOR_ID, decryptorId); values.put(Blueprint.COLUMN_NAME_ML, (-4 + currentDecryptorBonuses.meModifier)); values.put(Blueprint.COLUMN_NAME_PL, (-4 + currentDecryptorBonuses.peModifier)); if (getActivity() != null && getActivity().getContentResolver() != null) getActivity().getContentResolver().update(updateBlueprintUri, values, null, null); return decryptorId; } @Override protected void onPostExecute(Integer result) { if (fragmentReference.get() != null) { fragmentReference.get().setDecryptorId(result); } PriceCalculatorService.launchCalculateIntentForItemId(producedItemId, getActivity()); } } public void setParentFragment(InventionFragment fragment) { fragmentReference = new WeakReference<InventionFragment>(fragment); } private static String getDecryptorDescription(DecryptorBonuses bonuses, Resources res) { return res.getString(R.string.decryptor_desc, bonuses.meModifier, bonuses.peModifier, bonuses.maxRunModifier, bonuses.probabilityMultiplier); } }