gt:                 if (uri != null)
context: /*
   * Kontalk Android client
   * Copyright (C) 2014 Kontalk Devteam <devteam@kontalk.org>
  
   * 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 3 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, see <http://www.gnu.org/licenses/>.
   */
  
  package org.kontalk.ui;
  
  import org.kontalk.R;
  import org.kontalk.authenticator.Authenticator;
  import org.kontalk.authenticator.LegacyAuthentication;
  import org.kontalk.client.EndpointServer;
  import org.kontalk.client.ServerList;
  import org.kontalk.data.Contact;
  import org.kontalk.data.Conversation;
  import org.kontalk.provider.MessagesProvider;
  import org.kontalk.provider.MyMessages.Threads;
  import org.kontalk.service.ServerListUpdater;
  import org.kontalk.util.Preferences;
  
  import android.accounts.Account;
  import android.accounts.AccountManager;
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Bundle;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v7.app.ActionBarActivity;
  import android.text.Html;
  import android.text.InputType;
  import android.text.TextUtils;
  import android.view.View;
  import android.widget.ListAdapter;
  import android.widget.Toast;
  
  
  /**
   * The conversations list activity.
   * @author Daniele Ricci
   * @version 1.0
   */
  public class ConversationList extends ActionBarActivity
          implements ContactsSyncActivity, ContactPickerListener {
      static final String TAG = ConversationList.class.getSimpleName();
  
      private ConversationListFragment mFragment;
  
      private static final int REQUEST_CONTACT_PICKER = 7720;
  
      private static final int DIALOG_AUTH_ERROR_WARNING = 1;
  
      private static final String ACTION_AUTH_ERROR_WARNING = "org.kontalk.AUTH_ERROR_WARN";
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.conversation_list_screen);
  
          mFragment = (ConversationListFragment) getSupportFragmentManager()
                  .findFragmentById(R.id.fragment_conversation_list);
  
          if (!xmppUpgrade())
              handleIntent(getIntent());
      }
  
      public void titleComposeMessage(View view) {
          getListFragment().chooseContact();
      }
  
      public void titleSearch(View view) {
          onSearchRequested();
      }
  
      /** Big upgrade: asymmetric key encryption (for XMPP). */
      private boolean xmppUpgrade() {
          AccountManager am = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
          Account account = Authenticator.getDefaultAccount(am);
          if (account != null) {
              // adjust manual server address if any
              String manualServer = Preferences.getServerURI(this);
              if (!TextUtils.isEmpty(manualServer) && manualServer.indexOf('|') < 0) {
                  ServerList list = ServerListUpdater.getCurrentList(this);
                  if (list != null) {
                      EndpointServer server = list.random();
                      String newServer = server.getNetwork() + "|" + manualServer;
  
                      Preferences.setServerURI(this, newServer);
                  }
              }
  
              if (!Authenticator.hasPersonalKey(am, account)) {
                  // first of all, disable offline mode
                  Preferences.setOfflineMode(this, false);
  
                  // ask for user name
                  askForPersonalName();
  
                  return true;
              }
          }
  
          return false;
      }
  
      private void askForPersonalName() {
          DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  // no key pair found, generate a new one
                  Toast.makeText(ConversationList.this,
                      R.string.msg_generating_keypair, Toast.LENGTH_LONG).show();
  
                  String name = InputDialog
                          .getInputText((Dialog) dialog)
                          .toString();
  
                  // upgrade account
                  LegacyAuthentication.doUpgrade(getApplicationContext(), name);
  
              }
          };
  
          DialogInterface.OnCancelListener cancelListener = new DialogInterface.OnCancelListener() {
              public void onCancel(DialogInterface dialog) {
                  new AlertDialog.Builder(ConversationList.this)
                      .setTitle(R.string.title_no_personal_key)
                      .setMessage(R.string.msg_no_personal_key)
                      .setPositiveButton(android.R.string.ok, null)
                      .show();
              }
          };
  
          new InputDialog.Builder(this,
                  InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME)
              .setTitle(R.string.title_no_name)
              .setMessage(R.string.msg_no_name)
              .setPositiveButton(android.R.string.ok, okListener)
              .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                      dialog.cancel();
                  }
              })
              .setOnCancelListener(cancelListener)
              .show();
      }
  
      /** Called when a new intent is sent to the activity (if already started). */
      @Override
      protected void onNewIntent(Intent intent) {
          handleIntent(intent);
  
          ConversationListFragment fragment = getListFragment();
          fragment.startQuery();
      }
  
      @Override
      protected Dialog onCreateDialog(int id, Bundle args) {
          if (id == DIALOG_AUTH_ERROR_WARNING) {
  
              return new AlertDialog.Builder(this)
                  .setTitle(R.string.title_auth_error)
                  .setMessage(Html.fromHtml(getString(R.string.msg_auth_error)))
                  .setPositiveButton(android.R.string.ok, null)
                  .create();
  
          }
  
          return super.onCreateDialog(id, args);
      }
  
      private void handleIntent(Intent intent) {
          if (intent != null && ACTION_AUTH_ERROR_WARNING.equals(intent.getAction())) {
              showDialog(DIALOG_AUTH_ERROR_WARNING);
          }
      }
  
      @Override
      public boolean onSearchRequested() {
          ConversationListFragment fragment = getListFragment();
  
          ListAdapter list = fragment.getListAdapter();
          // no data found
          if (list == null || list.getCount() == 0)
              return false;
  
          startSearch(null, false, null, false);
          return true;
      }
  
      @Override
      public void onResume() {
          super.onResume();
  
          final Context context = getApplicationContext();
          new Thread(new Runnable() {
              @Override
              public void run() {
                  // mark all messages as old
                  MessagesProvider.markAllThreadsAsOld(context);
                  // update notification
                  MessagingNotification.updateMessagesNotification(context, false);
              }
          }).start();
  
          if (Authenticator.getDefaultAccount(this) == null) {
              NumberValidation.startValidation(this);
              finish();
          }
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          // contact chooser
          if (requestCode == REQUEST_CONTACT_PICKER) {
              if (resultCode == Activity.RESULT_OK) {
                  Uri uri = data.getData();
  