Java tutorial
/* * Copyright (C) 2011 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 org.orange.querysystem; import org.orange.parser.entity.Post; import org.orange.querysystem.content.ListPostsFragment; import org.orange.querysystem.content.TabsAdapter; import org.orange.querysystem.util.PostUpdater; import org.orange.querysystem.util.PostUpdater.OnPostUpdateListener; import android.annotation.TargetApi; import android.app.ActionBar; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TabHost; import android.widget.TextView; import android.widget.Toast; /** * @author Bai Jie */ public class PostsActivity extends FragmentActivity { private TextView currentTime; TabHost mTabHost; ViewPager mViewPager; TabsAdapter mTabsAdapter; PostUpdater mWebUpdaterToDB; MenuItem mRefreshMenuItem; /* (non-Javadoc) * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle) */ @TargetApi(11) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_tabs_pager); currentTime = (TextView) findViewById(R.id.currentTime); currentTime.setText(R.string.post); mWebUpdaterToDB = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? new PostUpdater(this) : new PostUpdater(this) { @Override public boolean updatePosts(boolean mandatorily) { boolean isUpdating = super.updatePosts(mandatorily); if (isUpdating) { startRefreshAnimation(); } return isUpdating; } }; mWebUpdaterToDB.setOnPostExecuteListener(new OnPostUpdateListener() { @Override public void onPostUpdate(long numberOfInsertedPosts, boolean mandatorily) { if (mandatorily || numberOfInsertedPosts > 0) { if (numberOfInsertedPosts > 0) { reloadPosts(); String message = PostsActivity.this.getResources().getString(R.string.has_updated_posts, numberOfInsertedPosts); Toast.makeText(PostsActivity.this, message, Toast.LENGTH_SHORT).show(); } else if (numberOfInsertedPosts == 0) { Toast.makeText(PostsActivity.this, R.string.no_new_post, Toast.LENGTH_SHORT).show(); } else //numberOfInsertedPosts < 0 { Toast.makeText(PostsActivity.this, R.string.fail_to_update_post, Toast.LENGTH_SHORT).show(); } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { stopRefreshAnimation(); } } }); if (mWebUpdaterToDB.updatePostsAutomatically()) { Toast.makeText(this, R.string.start_to_update_post_automatically, Toast.LENGTH_SHORT).show(); } mTabHost = (TabHost) findViewById(android.R.id.tabhost); mTabHost.setup(); mViewPager = (ViewPager) findViewById(R.id.pager); mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager); //3.0ActionBar if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ActionBar mActionBar = getActionBar(); mActionBar.setTitle(R.string.post); currentTime.setLayoutParams(new LinearLayout.LayoutParams(0, 0)); //????ActionBar if (getResources() .getConfiguration().orientation == android.content.res.Configuration.ORIENTATION_LANDSCAPE) { mActionBar.hide(); } } loadPosts(); } @Override protected void onStop() { mWebUpdaterToDB.stop(); super.onStop(); } /* (non-Javadoc) * @see android.app.Activity#onRestoreInstanceState(android.os.Bundle) */ @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); super.onRestoreInstanceState(savedInstanceState); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("tab", mTabHost.getCurrentTabTag()); } /** * ?ID?? * * @param source ???Post.SOURCES.WEBSITE_OF_TEACHING_AFFAIRS * @return ?IDR.string.teaching_affairs * @see Post.SOURCES */ public static int getSourceString(byte source) { switch (source) { case Post.SOURCES.WEBSITE_OF_TEACHING_AFFAIRS: return R.string.teaching_affairs; case Post.SOURCES.WEBSITE_OF_SCCE: return R.string.school_of_computer_and_communication_engineering; case Post.SOURCES.STUDENT_WEBSITE_OF_SCCE: return R.string.student_website_of_SCCE; case Post.SOURCES.UNKNOWN_SOURCE: return R.string.unknown_source; default: throw new IllegalArgumentException("Unknown post source: " + source); } } public void addTab(byte source) { String sourceString = getResources().getText(getSourceString(source)).toString(); mTabsAdapter.addTab(mTabHost.newTabSpec(sourceString).setIndicator(sourceString), ListPostsFragment.class, ListPostsFragment.buildArgument(source)); } public void loadPosts() { mTabsAdapter.clear(); addTab(Post.SOURCES.WEBSITE_OF_TEACHING_AFFAIRS); addTab(Post.SOURCES.STUDENT_WEBSITE_OF_SCCE); addTab(Post.SOURCES.WEBSITE_OF_SCCE); } private void reloadPosts() { for (Fragment fragment : mTabsAdapter.getAddedFragmentPagers()) { if (fragment instanceof ListPostsFragment) { ((ListPostsFragment) fragment).restartLoader(); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { mRefreshMenuItem = menu.add(0, 1, 1, R.string.refresh).setIcon(R.drawable.ic_action_refresh); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mRefreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); if (mWebUpdaterToDB != null && mWebUpdaterToDB.isUpdating()) startRefreshAnimation(); } menu.add(0, 2, 2, R.string.settings); return super.onCreateOptionsMenu(menu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if (item.getItemId() == 1) { if (mWebUpdaterToDB.updatePosts(true)) { Toast.makeText(this, R.string.updating, Toast.LENGTH_SHORT).show(); } } else if (item.getItemId() == 2) { startActivity(new Intent(this, SettingsActivity.class)); } return super.onMenuItemSelected(featureId, item); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void startRefreshAnimation() { if (mRefreshMenuItem == null) return; /* Attach a rotating ImageView to the refresh item as an ActionView */ LayoutInflater inflater = (LayoutInflater) PostsActivity.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); ImageView iv = (ImageView) inflater.inflate(R.layout.action_refresh_view, null); Animation rotation = AnimationUtils.loadAnimation(PostsActivity.this, R.anim.clockwise_refresh); rotation.setRepeatCount(Animation.INFINITE); iv.startAnimation(rotation); mRefreshMenuItem.setActionView(iv); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void stopRefreshAnimation() { if (mRefreshMenuItem != null && mRefreshMenuItem.getActionView() != null) { mRefreshMenuItem.getActionView().clearAnimation(); mRefreshMenuItem.setActionView(null); } } }