Java tutorial
/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ru.freshartapp.automarka.utils; import ru.freshartapp.automarka.R; import ru.freshartapp.automarka.activities.ImageDetailActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; /** * This fragment will populate the children of the ViewPager from * {@link ImageDetailActivity}. */ public class ImageDetailFragment extends Fragment { // Add and remove favorites public class FavotitesTask extends AsyncTask<Integer, Void, Void> { private int mode; public FavotitesTask(int mode) { this.mode = mode; } @Override protected Void doInBackground(Integer... params) { ExternalDbOpenHelper dbFavoritesOpenHelper = new ExternalDbOpenHelper( ImageDetailFragment.this.getActivity().getApplicationContext(), ImageDetailFragment.this.DB_FAVORITES_NAME); SQLiteDatabase database_favorites = dbFavoritesOpenHelper.openDataBase(); if (database_favorites != null) { switch (this.mode) { case FAVORITES_ADD: database_favorites .execSQL("INSERT INTO model_favorites (parent, status) VALUES (" + params[0] + ",1)"); break; case FAVORITES_REMOVE: database_favorites.execSQL("DELETE FROM model_favorites WHERE parent=" + params[0]); break; } database_favorites.close(); } return null; } } private static final String IMAGE_DATA_EXTRA = "resId"; private static final String ID_DATA_EXTRA = "modelId"; private static final String TITLE_DATA_EXTRA = "title"; /** * Factory method to generate a new instance of the fragment given an image * number. * * @param imageNum * The image number within the parent adapter to load * @return A new instance of ImageDetailFragment with imageNum extras */ public static ImageDetailFragment newInstance(int imageNum, int modelId, String title) { Log.d("ImageDetailFragment newInstance", "start"); final ImageDetailFragment f = new ImageDetailFragment(); final Bundle args = new Bundle(); args.putInt(IMAGE_DATA_EXTRA, imageNum); args.putInt(ID_DATA_EXTRA, modelId); args.putString(TITLE_DATA_EXTRA, title); f.setArguments(args); return f; } private int mImageNum; private int mModelId; private String mTitle; private ImageView mImageView = null; private TextView mTextView; private String DB_FAVORITES_NAME = "favorites.sqlite"; private Button favorites_button = null; private Boolean favorites_button_status = false; private ImageWorker mImageWorker = null; final private int FAVORITES_ADD = 0; final private int FAVORITES_REMOVE = 1; /** * Empty constructor as per the Fragment documentation */ public ImageDetailFragment() { } private void addToFavorites() { Log.d("addToFavorites", "start"); final FavotitesTask task = new FavotitesTask(this.FAVORITES_ADD); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this.mModelId); } else { task.execute(this.mModelId); } } /** * Cancels the asynchronous work taking place on the ImageView, called by the * adapter backing the ViewPager when the child is destroyed. */ public void cancelWork() { if (this.mImageView != null) { ImageWorker.cancelWork(this.mImageView); this.mImageView.setImageDrawable(null); this.mImageView = null; } if (this.favorites_button != null) { this.favorites_button.setOnClickListener(null); this.favorites_button.setBackgroundDrawable(null); this.favorites_button = null; } } private void changeFavorites(Boolean status) { this.favorites_button_status = status; if (status) { this.favorites_button.setBackgroundResource(R.drawable.favorites_on); } else { this.favorites_button.setBackgroundResource(R.drawable.favorites_off); } } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // Use the parent activity to load the image asynchronously into the // ImageView (so a single // cache can be used over all pages in the ViewPager if (ImageDetailActivity.class.isInstance(this.getActivity())) { if (this.mModelId != -1) { // Load information about favorites ExternalDbOpenHelper dbFavoritesOpenHelper = new ExternalDbOpenHelper( this.getActivity().getApplicationContext(), this.DB_FAVORITES_NAME); SQLiteDatabase database_favorites = dbFavoritesOpenHelper.openDataBase(); Cursor cur = database_favorites .rawQuery("SELECT status FROM model_favorites WHERE parent=" + this.mModelId, null); cur.moveToFirst(); if (!cur.isAfterLast()) { this.favorites_button_status = (cur.getInt(0) == 1); } cur.close(); database_favorites.close(); database_favorites = null; // Set favorites status this.changeFavorites(this.favorites_button_status); this.favorites_button.setVisibility(View.VISIBLE); this.favorites_button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (ImageDetailFragment.this.favorites_button_status) { ImageDetailFragment.this.changeFavorites(false); ImageDetailFragment.this.removeFromFavorites(); Toast.makeText(ImageDetailFragment.this.getActivity().getApplicationContext(), R.string.imagedetailactivity_favorites_deleted_message, Toast.LENGTH_SHORT) .show(); } else { ImageDetailFragment.this.changeFavorites(true); ImageDetailFragment.this.addToFavorites(); Toast.makeText(ImageDetailFragment.this.getActivity().getApplicationContext(), R.string.imagedetailactivity_favorites_added_message, Toast.LENGTH_SHORT) .show(); } } }); } this.mImageWorker = ((ImageDetailActivity) this.getActivity()).getImageWorker(); this.mImageWorker.loadImage(this.mImageNum, this.mImageView); this.mTextView.setText(this.mTitle); } } /** * Populate image number from extra, use the convenience factory method * {@link ImageDetailFragment#newInstance(int)} to create this fragment. */ @Override public void onCreate(Bundle savedInstanceState) { Log.d("ImageDetailFragment onCreate", "start"); super.onCreate(savedInstanceState); this.mImageNum = this.getArguments() != null ? this.getArguments().getInt(IMAGE_DATA_EXTRA) : -1; this.mModelId = this.getArguments() != null ? this.getArguments().getInt(ID_DATA_EXTRA) : -1; this.mTitle = this.getArguments() != null ? this.getArguments().getString(TITLE_DATA_EXTRA) : ""; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("ImageDetailFragment onCreateView", "start"); // Inflate and locate the main ImageView final View v = inflater.inflate(R.layout.image_detail_fragment, container, false); this.favorites_button = (Button) v.findViewById(R.id.favorites_button); this.mImageView = (ImageView) v.findViewById(R.id.imageView); this.mTextView = (TextView) v.findViewById(R.id.textView); return v; } private void removeFromFavorites() { Log.d("removeFromFavorites", "start"); final FavotitesTask task = new FavotitesTask(this.FAVORITES_REMOVE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this.mModelId); } else { task.execute(this.mModelId); } } }