Java tutorial
/** * MicroEmulator * Copyright (C) 2008 Bartek Teodorczyk <barteo@barteo.net> * * It is licensed under the following two licenses as alternatives: * 1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version * 2. Apache License (the "AL") Version 2.0 * * You may not use this file except in compliance with at least one of * the above two licenses. * * You may obtain a copy of the LGPL at * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt * * You may obtain a copy of the AL 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 LGPL or the AL for the specific language governing permissions and * limitations. * */ package ru.sawimmod.activities; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.Menu; import android.view.MenuItem; import android.view.Window; import protocol.Contact; import protocol.Protocol; import protocol.icq.Icq; import protocol.mrim.Mrim; import protocol.xmpp.Xmpp; import ru.sawimmod.Options; import ru.sawimmod.R; import ru.sawimmod.SawimApplication; import ru.sawimmod.Scheme; import ru.sawimmod.chat.Chat; import ru.sawimmod.chat.ChatHistory; import ru.sawimmod.modules.DebugLog; import ru.sawimmod.roster.RosterHelper; import ru.sawimmod.view.*; import ru.sawimmod.view.preference.MainPreferenceView; public class SawimActivity extends BaseActivity { public static final String LOG_TAG = SawimActivity.class.getSimpleName(); public static final String NOTIFY = "ru.sawimmod.notify"; public static final String NOTIFY_REPLY = "ru.sawimmod.notify.reply"; public static final String NOTIFY_CAPTCHA = "ru.sawimmod.notify.captcha"; private boolean isOpenNewChat = false; @Override public void onCreate(Bundle savedInstanceState) { setTheme(Scheme.isBlack() ? R.style.BaseTheme : R.style.BaseThemeLight); super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_PROGRESS); setContentView(SawimApplication.isManyPane() ? R.layout.main_twopane : R.layout.main); if (savedInstanceState == null && !SawimApplication.isManyPane()) { RosterView rosterView = new RosterView(); rosterView.setMode(RosterView.MODE_DEFAULT); rosterView.setArguments(getIntent().getExtras()); getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, rosterView, RosterView.TAG) .commit(); } } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (getRosterView() != null && (getRosterView().getMode() == RosterView.MODE_SHARE_TEXT || getRosterView().getMode() == RosterView.MODE_SHARE)) return; setIntent(intent); } private void handleIntent() throws InterruptedException { if (getIntent() == null || getIntent().getAction() == null) return; if (getIntent().getAction().startsWith(Intent.ACTION_SEND)) { FragmentManager fm = getSupportFragmentManager(); for (int i = 0; i < fm.getBackStackEntryCount(); ++i) { fm.popBackStack(); } RosterView rosterView = getRosterView(); if (rosterView != null) rosterView.setMode(getIntent().getType().equals("text/plain") ? RosterView.MODE_SHARE_TEXT : RosterView.MODE_SHARE); return; } if (NOTIFY.equals(getIntent().getAction())) { Chat current = ChatHistory.instance.chatAt(ChatHistory.instance.getPreferredItem()); if (current != null) isOpenNewChat = openChat(current.getProtocol(), current.getContact(), true); } if (NOTIFY_REPLY.equals(getIntent().getAction())) { Chat current = ChatHistory.instance.chatAt(ChatHistory.instance.getPreferredItem()); if (current != null) isOpenNewChat = openChat(current.getProtocol(), current.getContact(), true); } if (NOTIFY_CAPTCHA.equals(getIntent().getAction())) { FormView.showWindows(this, getIntent().getStringExtra(NOTIFY_CAPTCHA)); } setIntent(null); } public boolean openChat(Protocol p, Contact c, boolean allowingStateLoss) throws InterruptedException { FragmentManager fragmentManager = getSupportFragmentManager(); ChatView chatView = (ChatView) fragmentManager.findFragmentById(R.id.chat_fragment); if (chatView == null) { Fragment rosterView = fragmentManager.findFragmentByTag(RosterView.TAG); chatView = (ChatView) fragmentManager.findFragmentByTag(ChatView.TAG); if (fragmentManager.getFragments() == null || rosterView == null || chatView == null || rosterView.isVisible()) { if (p == null || c == null) return false; chatView = new ChatView(); chatView.initChat(p, c); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.fragment_container, chatView, ChatView.TAG); transaction.addToBackStack(null); if (allowingStateLoss) transaction.commitAllowingStateLoss(); else transaction.commit(); return true; } if (RosterHelper.getInstance().getCurrentContact() != c && chatView.isLastPosition()) { if (c != null) { chatView.openChat(p, c); return true; } } } else { Protocol protocol = null; Contact contact = null; Chat oldChat = ChatHistory.instance.getChatById(ChatView.getLastChat()); if (p != null && c != null) { protocol = p; contact = c; } else if (oldChat != null) { protocol = oldChat.getProtocol(); contact = oldChat.getContact(); } if (protocol != null && contact != null) { chatView.openChat(protocol, contact); chatView.resume(chatView.getCurrentChat()); return true; } } return false; } @Override public void onResume() { super.onResume(); SawimApplication.maximize(); FragmentManager fragmentManager = getSupportFragmentManager(); StartWindowView startWindowView = (StartWindowView) fragmentManager.findFragmentByTag(StartWindowView.TAG); if (RosterHelper.getInstance().getProtocolCount() == 0) { if (Options.getAccountCount() == 0) { FragmentTransaction transaction = fragmentManager.beginTransaction(); if (SawimApplication.isManyPane()) { setContentView(R.layout.main); if (startWindowView == null) { StartWindowView newFragment = new StartWindowView(); transaction.replace(R.id.fragment_container, newFragment, StartWindowView.TAG); transaction.commit(); supportInvalidateOptionsMenu(); } } } else { startActivity(new Intent(this, AccountsListActivity.class)); } } else { if (startWindowView != null) fragmentManager.popBackStack(); } try { handleIntent(); } catch (InterruptedException e) { e.printStackTrace(); } if (!isOpenNewChat && SawimApplication.isManyPane()) try { openChat(null, null, true); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public void onPause() { super.onPause(); SawimApplication.minimize(); } @Override public void onBackPressed() { FragmentManager fragmentManager = getSupportFragmentManager(); SawimFragment chatView = (SawimFragment) fragmentManager.findFragmentByTag(ChatView.TAG); SawimFragment formView = (SawimFragment) fragmentManager.findFragmentByTag(FormView.TAG); SawimFragment preferenceFormView = (SawimFragment) fragmentManager .findFragmentByTag(MainPreferenceView.TAG); SawimFragment virtualListView = (SawimFragment) fragmentManager.findFragmentByTag(VirtualListView.TAG); if (chatView != null && chatView.isVisible()) { if (chatView.hasBack()) super.onBackPressed(); } else if (virtualListView != null) { if (virtualListView.hasBack()) back(); } else if (formView != null) { if (formView.hasBack()) back(); } else if (preferenceFormView != null) { if (preferenceFormView.hasBack()) { back(); } } else super.onBackPressed(); //if (SawimApplication.isManyPane()) // rosterView.resume(); } private void back() { if (SawimApplication.isManyPane()) recreateActivity(); else super.onBackPressed(); } public void recreateActivity() { finish(); startActivity(new Intent(this, SawimActivity.class)); } public void closeActivity() { finish(); Intent intent = new Intent(this, SawimActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (SawimApplication.isTablet()) recreateActivity(); if (getChatView() != null && getChatView().getDrawerToggle() != null) getChatView().getDrawerToggle().onConfigurationChanged(newConfig); } private RosterView getRosterView() { RosterView rosterView = (RosterView) getSupportFragmentManager().findFragmentByTag(RosterView.TAG); if (rosterView == null) rosterView = (RosterView) getSupportFragmentManager().findFragmentById(R.id.roster_fragment); return rosterView; } private ChatView getChatView() { ChatView chatView = (ChatView) getSupportFragmentManager().findFragmentByTag(ChatView.TAG); if (chatView == null) chatView = (ChatView) getSupportFragmentManager().findFragmentById(R.id.chat_fragment); return chatView; } private static final int MENU_OPTIONS = 7; private static final int MENU_QUIT = 14; private static final int MENU_DEBUG_LOG = 22; @Override public boolean onPrepareOptionsMenu(Menu menu) { menu.clear(); ChatView chatView = getChatView(); RosterView rosterView = getRosterView(); StartWindowView startWindowView = (StartWindowView) getSupportFragmentManager() .findFragmentByTag(StartWindowView.TAG); VirtualListView virtualListView = (VirtualListView) getSupportFragmentManager() .findFragmentByTag(VirtualListView.TAG); if (virtualListView != null && virtualListView.isAdded()) { virtualListView.onPrepareOptionsMenu_(menu); return true; } else if (chatView != null && chatView.isAdded() && !SawimApplication.isManyPane()) { chatView.onPrepareOptionsMenu_(menu); return true; } else if ((rosterView != null && rosterView.isAdded()) || (startWindowView != null && startWindowView.isAdded())) { Protocol p = RosterHelper.getInstance().getProtocol(0); if (RosterHelper.getInstance().getProtocolCount() == 1 && p != null) { menu.add(Menu.NONE, RosterHelper.MENU_CONNECT, Menu.NONE, R.string.connect) .setTitle((p.isConnected() || p.isConnecting()) ? R.string.disconnect : R.string.connect); menu.add(Menu.NONE, RosterHelper.MENU_STATUS, Menu.NONE, R.string.status); if (p.getXStatusInfo() != null) menu.add(Menu.NONE, RosterHelper.MENU_XSTATUS, Menu.NONE, R.string.xstatus); if ((p instanceof Icq) || (p instanceof Mrim)) menu.add(Menu.NONE, RosterHelper.MENU_PRIVATE_STATUS, Menu.NONE, R.string.private_status); if (p instanceof Mrim && p.isConnected()) { menu.add(Menu.NONE, RosterHelper.MENU_SEND_SMS, Menu.NONE, R.string.send_sms); } if (p.isConnected()) { if (p instanceof Xmpp) { if (((Xmpp) p).hasS2S()) { menu.add(Menu.NONE, RosterHelper.MENU_DISCO, Menu.NONE, R.string.service_discovery); } menu.add(Menu.NONE, RosterHelper.MENU_ADHOC, Menu.NONE, R.string.account_settings); } menu.add(Menu.NONE, RosterHelper.MENU_GROUPS, Menu.NONE, R.string.manage_contact_list); if (p instanceof Icq) { menu.add(Menu.NONE, RosterHelper.MENU_MYSELF, Menu.NONE, R.string.myself); } else { if (p instanceof Xmpp) { menu.add(Menu.NONE, RosterHelper.MENU_NOTES, Menu.NONE, R.string.notes); } if (p.hasVCardEditor()) menu.add(Menu.NONE, RosterHelper.MENU_MYSELF, Menu.NONE, R.string.myself); if (p instanceof Mrim) menu.add(Menu.NONE, RosterHelper.MENU_MICROBLOG, Menu.NONE, R.string.microblog); } } } menu.add(Menu.NONE, MENU_OPTIONS, Menu.NONE, R.string.options); menu.add(Menu.NONE, MENU_QUIT, Menu.NONE, R.string.quit); } return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { ChatView chatView = getChatView(); VirtualListView virtualListView = (VirtualListView) getSupportFragmentManager() .findFragmentByTag(VirtualListView.TAG); if (!SawimApplication.isManyPane() && chatView != null && chatView.getDrawerToggle() != null && chatView.getDrawerToggle().onOptionsItemSelected(item)) { return true; } if (item.getItemId() == android.R.id.home) back(); if (virtualListView != null) { virtualListView.onOptionsItemSelected_(item); return true; } else if (!SawimApplication.isManyPane() && chatView != null) { chatView.onOptionsItemSelected_(item); return true; } if (RosterHelper.getInstance().protocolMenuItemSelected(this, RosterHelper.getInstance().getProtocol(0), item.getItemId())) return true; switch (item.getItemId()) { case MENU_OPTIONS: MainPreferenceView.show(this); break; case MENU_DEBUG_LOG: DebugLog.instance.activate(); break; case MENU_QUIT: SawimApplication.getInstance().quit(false); SawimApplication.getInstance().stopService(); finish(); System.exit(0); break; } return super.onOptionsItemSelected(item); } }