Java tutorial
/********************************************************************** * All Rights Reserved * * MARVELL CONFIDENTIAL * Copyright 2015 Marvell International Ltd All Rights Reserved. * The source code contained or described herein and all documents related to * the source code ("Material") are owned by Marvell International Ltd or its * suppliers or licensors. Title to the Material remains with Marvell International Ltd * or its suppliers and licensors. The Material contains trade secrets and * proprietary and confidential information of Marvell or its suppliers and * licensors. The Material is protected by worldwide copyright and trade secret * laws and treaty provisions. No part of the Material may be used, copied, * reproduced, modified, published, uploaded, posted, transmitted, distributed, * or disclosed in any way without Marvell's prior express written permission. * * No license under any patent, copyright, trade secret or other intellectual * property right is granted to or conferred upon you by disclosure or delivery * of the Materials, either expressly, by implication, inducement, estoppel or * otherwise. Any license under such intellectual property rights must be * express and approved by Marvell in writing. * **********************************************************************/ package com.android.firewall; import java.util.Timer; import java.util.TimerTask; import com.android.firewall.constants.FirewallConstants; import com.android.firewall.data.provider.DatabaseManager; import com.android.firewall.model.BlacklistInfo; import com.android.firewall.model.WhitelistInfo; import com.android.firewall.ui.activity.AddFromContactsActivity; import com.android.firewall.ui.adapter.MainPagerAdapter; import com.android.firewall.ui.dialog.EntryEditDialog; import com.android.firewall.ui.dialog.EntryEditDialog.EditCompletedListener; import com.android.firewall.ui.fragment.BlacklistFragment; import com.android.firewall.ui.fragment.MBRFragment; import com.android.firewall.ui.fragment.PBRFragment; import com.android.firewall.ui.fragment.SettingsFragment; import com.android.firewall.ui.fragment.WhitelistFragment; import com.android.firewall.util.LogUtil; import com.android.firewall.util.Utils; import android.R.integer; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.graphics.pdf.PdfDocument.Page; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.TabHost; import android.widget.TextView; import android.widget.Toast; import android.widget.TabHost.TabSpec; public class FirewallListPage extends Activity implements EditCompletedListener { private final static String TAG = FirewallListPage.class.getSimpleName(); private final static String[] TAB_TITLES = new String[] { "Blacklist", "Blocklog", "Settings" }; private boolean isQuit; private Timer timer = new Timer(); private ViewPager pager; private MainPagerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); FragmentManager manager = getFragmentManager(); // tabhost TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); tabHost.setup(); // viewpager pager = (ViewPager) findViewById(R.id.pager); adapter = new MainPagerAdapter(manager, this, tabHost, pager); // tab size from screen size DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int tabWidth = metrics.widthPixels / 3; int tabHeight = metrics.widthPixels / 7; for (int i = 0; i < 3; i++) { TabSpec spec = tabHost.newTabSpec("tab" + i); spec.setIndicator(getTabView(tabWidth, tabHeight, TAB_TITLES[i])); adapter.addTab(spec, "tab" + i); } this.handleIntent(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (isQuit == false) { isQuit = true; Toast.makeText(getBaseContext(), "click back again to close", Toast.LENGTH_SHORT).show(); TimerTask task = null; task = new TimerTask() { @Override public void run() { isQuit = false; } }; timer.schedule(task, 2000); } else { finish(); } } else { } return false; } private TextView getTabView(int width, int height, String title) { TextView view = new TextView(this); view.setMinimumWidth(width); view.setMinimumHeight(height); view.setGravity(Gravity.CENTER); view.setBackgroundResource(R.drawable.tab_indicator_holo); view.setText(title); return view; } // @Override // public boolean onPrepareOptionsMenu(Menu menu) { // MenuInflater inflater = getMenuInflater(); // menu.clear(); // int curItem = pager.getCurrentItem(); // switch (curItem) { // case 0: // inflater.inflate(R.menu.bw_menu, menu); // break; // case 1: // inflater.inflate(R.menu.pm_menu, menu); // break; // case 2: // inflater.inflate(R.menu.settings_menu, menu); // break; // } // return super.onPrepareOptionsMenu(menu); // } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add_new_item: addNewItem(); break; case R.id.add_from_contacts_item: addFromContacts(); break; case R.id.delete_all_item: case R.id.delete_all_record_item: deleteAllItems(); break; default: break; } return super.onOptionsItemSelected(item); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { int curItem = pager.getCurrentItem(); menu.findItem(R.id.add_new_item).setVisible(curItem == 0); menu.findItem(R.id.add_from_contacts_item).setVisible(curItem == 0); menu.findItem(R.id.delete_all_item).setVisible(curItem == 0); menu.findItem(R.id.delete_all_record_item).setVisible(curItem == 1); menu.findItem(R.id.restore_default).setVisible(curItem == 2); return super.onPrepareOptionsMenu(menu); } /** * Process Intent, For example, add black/white list */ private void handleIntent() { Intent intent = getIntent(); if (intent != null) { if (intent.getAction().equals(Intent.ACTION_INSERT)) { Bundle bundle = intent.getExtras(); if (bundle.getString(FirewallConstants.KEY_MODE).equals("blacklist")) { BlacklistInfo info = new BlacklistInfo(); String num = Utils.getPurePhoneNum(bundle.getString("number")); info.setId(-1); if (num != null) info.setPhone_num(num); String name = bundle.getString("name"); if (name != null) info.setName(name); info.setMsg_mode(FirewallConstants.BLOCK); info.setPhone_mode(FirewallConstants.BLOCK); DatabaseManager.getInstance(getApplicationContext()).insertOrUpdateBlacklist(info); } } } } /** * Add black list * * @param info */ private void addBlacklistInfo(BlacklistInfo info) { LogUtil.log("curr item:", pager.getCurrentItem() + ""); if (pager.getCurrentItem() == 0) { Fragment f = getFragmentManager().findFragmentById(R.id.lyt_main_fragment_container); if (f instanceof BlacklistFragment) { BlacklistFragment bf = (BlacklistFragment) f; bf.updateData(info); bf.updateView(); } } } /** * Add white list * * @param info */ private void addWhitelisttInfo(WhitelistInfo info) { LogUtil.log("curr item:", pager.getCurrentItem() + ""); if (pager.getCurrentItem() == 0) { Fragment f = getFragmentManager().findFragmentById(R.id.lyt_main_fragment_container); if (f instanceof WhitelistFragment) { WhitelistFragment wf = (WhitelistFragment) f; wf.updateData(info); wf.updateView(); } } } private void addNewItem() { if (pager.getCurrentItem() == 0) { Fragment f = getFragmentManager().findFragmentById(R.id.lyt_main_fragment_container); if (f instanceof BlacklistFragment) { EntryEditDialog dialog = new EntryEditDialog(FirewallConstants.ADD_BL_ITEM); dialog.show(getFragmentManager(), "addBlacklistItems"); } else { EntryEditDialog dialog = new EntryEditDialog(FirewallConstants.ADD_WL_ITEM); dialog.show(getFragmentManager(), "addWhitelistItems"); } } } /** * Add blacklist from Contacts */ private void addFromContacts() { if (pager.getCurrentItem() == 0) { Fragment f = getFragmentManager().findFragmentById(R.id.lyt_main_fragment_container); if (f instanceof BlacklistFragment) { Intent intent = new Intent(this, AddFromContactsActivity.class); intent.putExtra("type", FirewallConstants.ADD_BL_ITEM); startActivity(intent); } else { Intent intent = new Intent(this, AddFromContactsActivity.class); intent.putExtra("type", FirewallConstants.ADD_WL_ITEM); startActivity(intent); } } } /** * Delete all items */ private void deleteAllItems() { int curItem = pager.getCurrentItem(); if (curItem == 0) { Fragment f = getFragmentManager().findFragmentById(R.id.lyt_main_fragment_container); if (f instanceof BlacklistFragment) { BlacklistFragment bf = (BlacklistFragment) f; bf.deleteAllData(); bf.updateView(); } else { WhitelistFragment wf = (WhitelistFragment) f; wf.deleteAllData(); wf.updateView(); } } else if (curItem == 1) { Fragment f = getFragmentManager().findFragmentById(R.id.lyt_pms_container); if (f instanceof PBRFragment) { PBRFragment pbrf = (PBRFragment) f; pbrf.deleteAllData(); pbrf.updateView(); } else { MBRFragment mbrf = (MBRFragment) f; mbrf.deleteAllData(); mbrf.updateView(); } } } /** * Call back function for black item edit */ @Override public void onOKBlacklistComplete(BlacklistInfo info) { this.addBlacklistInfo(info); } /** * Call back function for white item edit */ @Override public void onOKWhitelistComplete(WhitelistInfo info) { this.addWhitelisttInfo(info); } }