<commit_before>/*
 * Copyright (C) 2008 Esmertec AG. Copyright (C) 2008 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 info.guardianproject.otr.app.im.app;

import info.guardianproject.otr.OtrDataHandler;
import info.guardianproject.otr.app.im.IChatSession;
import info.guardianproject.otr.app.im.IChatSessionManager;
import info.guardianproject.otr.app.im.IImConnection;
import info.guardianproject.otr.app.im.R;
import info.guardianproject.otr.app.im.engine.ImConnection;
import info.guardianproject.otr.app.im.provider.Imps;
import info.guardianproject.otr.app.im.service.ImServiceConstants;
import info.guardianproject.util.LogCleaner;
import info.guardianproject.util.SystemServices;
import info.guardianproject.util.SystemServices.FileInfo;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import net.java.otr4j.session.SessionStatus;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class ImUrlActivity extends Activity {
    
    private static final int REQUEST_PICK_CONTACTS = RESULT_FIRST_USER + 1;
    private static final int REQUEST_CREATE_ACCOUNT = RESULT_FIRST_USER + 2;
    private static final int REQUEST_SIGNIN_ACCOUNT = RESULT_FIRST_USER + 3;
    private static final int REQUEST_START_MUC =  RESULT_FIRST_USER + 4;
    
    private String mProviderName;
    private String mToAddress;
    private String mFromAddress;
    private String mHost;
    
    private IImConnection mConn;
    private IChatSessionManager mChatSessionManager;
    
    private String mSendUrl;
    private String mSendType;
    private String mSendText;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        doOnCreate();
    }

    
    
    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        
        setIntent(intent);
    }

    public void onDBLocked() {
        
        Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    void handleIntent() {
        ContentResolver cr = getContentResolver();
        
        long providerId = -1;
        long accountId = -1;
        
        Collection<IImConnection> listConns = ((ImApp)getApplication()).getActiveConnections();
        
        //look for active connections that match the host we need
        for (IImConnection conn : listConns)
        {            
   
            
            try {
                long connProviderId = conn.getProviderId();

                Cursor cursor = cr.query(Imps.ProviderSettings.CONTENT_URI,new String[] {Imps.ProviderSettings.NAME, Imps.ProviderSettings.VALUE},Imps.ProviderSettings.PROVIDER + "=?",new String[] { Long.toString(connProviderId)},null);

                Imps.ProviderSettings.QueryMap settings = new Imps.ProviderSettings.QueryMap(
                        cursor, cr, connProviderId, false /* don't keep updated */, null /* no handler */);
                
                try {
                    String domainToCheck = settings.getDomain();
                    
                    if (domainToCheck != null && domainToCheck.length() > 0 && mHost.contains(domainToCheck))
                    {
                        mConn = conn;
                        providerId = connProviderId;
                        accountId = conn.getAccountId();
                        
                        break;
                    }
                } finally {
                    settings.close();
                }
                
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            
            
        }

        //nothing active, let's see if non-active connections match
        if (mConn == null) {
            
            Cursor cursorProvider = initProviderCursor();
            
            if (cursorProvider == null || cursorProvider.getCount() == 0) {                                

                createNewAccount();
                return;
            } else {
                
                
                while (cursorProvider.moveToNext())
                {                
                    //make sure there is a stored password
                    if (!cursorProvider.isNull(ACTIVE_ACCOUNT_PW_COLUMN)) {
                            
                        long cProviderId = cursorProvider.getLong(PROVIDER_ID_COLUMN);
                        Cursor cursor = cr.query(Imps.ProviderSettings.CONTENT_URI,new String[] {Imps.ProviderSettings.NAME, Imps.ProviderSettings.VALUE},Imps.ProviderSettings.PROVIDER + "=?",new String[] { Long.toString(cProviderId)},null);

                        Imps.ProviderSettings.QueryMap settings = new Imps.ProviderSettings.QueryMap(
                                cursor, cr, cProviderId, false /* don't keep updated */, null /* no handler */);
                        
                        //does the conference host we need, match the settings domain for a logged in account
                        String domainToCheck = settings.getDomain(); 
                        
                        if (domainToCheck != null && domainToCheck.length() > 0 && mHost.contains(domainToCheck))
                        {
                            providerId = cProviderId;
                            accountId = cursorProvider.getLong(ACTIVE_ACCOUNT_ID_COLUMN);
                            mConn = ((ImApp)getApplication()).getConnection(providerId);
                            
                            //now sign in
                            signInAccount(accountId, providerId, cursorProvider.getString(ACTIVE_ACCOUNT_PW_COLUMN));
                            
                            break; //do sign in, the rest of the process will happen later
                           
                        }
                        
                        settings.close();
                        cursorProvider.close();
                        
                    }
                }
                
                
                
                
            }
            
        }
            
        if (mConn != null)
        {
            try {
                int state = mConn.getState();
                accountId = mConn.getAccountId();
                providerId = mConn.getProviderId();
                
                if (state < ImConnection.LOGGED_IN) {                
                    
                    Cursor cursorProvider = initProviderCursor();
                    
                    while(cursorProvider.moveToNext())
                    {<commit_msg>fix cursor closed issue with intent API<commit_after>/*
 * Copyright (C) 2008 Esmertec AG. Copyright (C) 2008 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 info.guardianproject.otr.app.im.app;

import info.guardianproject.otr.OtrDataHandler;
import info.guardianproject.otr.app.im.IChatSession;
import info.guardianproject.otr.app.im.IChatSessionManager;
import info.guardianproject.otr.app.im.IImConnection;
import info.guardianproject.otr.app.im.R;
import info.guardianproject.otr.app.im.engine.ImConnection;
import info.guardianproject.otr.app.im.provider.Imps;
import info.guardianproject.otr.app.im.service.ImServiceConstants;
import info.guardianproject.util.LogCleaner;
import info.guardianproject.util.SystemServices;
import info.guardianproject.util.SystemServices.FileInfo;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import net.java.otr4j.session.SessionStatus;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class ImUrlActivity extends Activity {
    
    private static final int REQUEST_PICK_CONTACTS = RESULT_FIRST_USER + 1;
    private static final int REQUEST_CREATE_ACCOUNT = RESULT_FIRST_USER + 2;
    private static final int REQUEST_SIGNIN_ACCOUNT = RESULT_FIRST_USER + 3;
    private static final int REQUEST_START_MUC =  RESULT_FIRST_USER + 4;
    
    private String mProviderName;
    private String mToAddress;
    private String mFromAddress;
    private String mHost;
    
    private IImConnection mConn;
    private IChatSessionManager mChatSessionManager;
    
    private String mSendUrl;
    private String mSendType;
    private String mSendText;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        doOnCreate();
    }

    
    
    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        
        setIntent(intent);
    }

    public void onDBLocked() {
        
        Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    void handleIntent() {
        
        ContentResolver cr = getContentResolver();
        
        long providerId = -1;
        long accountId = -1;
        
        Collection<IImConnection> listConns = ((ImApp)getApplication()).getActiveConnections();
        
        //look for active connections that match the host we need
        for (IImConnection conn : listConns)
        {            
   
            
            try {
                long connProviderId = conn.getProviderId();

                Cursor cursor = cr.query(Imps.ProviderSettings.CONTENT_URI,new String[] {Imps.ProviderSettings.NAME, Imps.ProviderSettings.VALUE},Imps.ProviderSettings.PROVIDER + "=?",new String[] { Long.toString(connProviderId)},null);

                Imps.ProviderSettings.QueryMap settings = new Imps.ProviderSettings.QueryMap(
                        cursor, cr, connProviderId, false /* don't keep updated */, null /* no handler */);
                
                try {
                    String domainToCheck = settings.getDomain();
                    
                    if (domainToCheck != null && domainToCheck.length() > 0 && mHost.contains(domainToCheck))
                    {
                        mConn = conn;
                        providerId = connProviderId;
                        accountId = conn.getAccountId();
                        
                        break;
                    }
                } finally {
                    settings.close();
                }
                
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            
            
        }

        //nothing active, let's see if non-active connections match
        if (mConn == null) {
            
            Cursor cursorProvider = initProviderCursor();
            
            if (cursorProvider == null || cursorProvider.isClosed() || cursorProvider.getCount() == 0) {                                

                createNewAccount();
                return;
            } else {
                
                
                while (cursorProvider.moveToNext())
                {                
                    //make sure there is a stored password
                    if (!cursorProvider.isNull(ACTIVE_ACCOUNT_PW_COLUMN)) {
                            
                        long cProviderId = cursorProvider.getLong(PROVIDER_ID_COLUMN);
                        Cursor cursor = cr.query(Imps.ProviderSettings.CONTENT_URI,new String[] {Imps.ProviderSettings.NAME, Imps.ProviderSettings.VALUE},Imps.ProviderSettings.PROVIDER + "=?",new String[] { Long.toString(cProviderId)},null);

                        Imps.ProviderSettings.QueryMap settings = new Imps.ProviderSettings.QueryMap(
                                cursor, cr, cProviderId, false /* don't keep updated */, null /* no handler */);
                        
                        //does the conference host we need, match the settings domain for a logged in account
                        String domainToCheck = settings.getDomain(); 
                        
                        if (domainToCheck != null && domainToCheck.length() > 0 && mHost.contains(domainToCheck))
                        {
                            providerId = cProviderId;
                            accountId = cursorProvider.getLong(ACTIVE_ACCOUNT_ID_COLUMN);
                            mConn = ((ImApp)getApplication()).getConnection(providerId);
                            
                            //now sign in
                            signInAccount(accountId, providerId, cursorProvider.getString(ACTIVE_ACCOUNT_PW_COLUMN));
                            
                            break; //do sign in, the rest of the process will happen later
                           
                        }
                        
                        settings.close();
                        
                    }    
                    
                }
                
                cursorProvider.close();
                
                
                
                
                
            }
            
        }
            
        if (mConn != null)
        {
            try {
                int state = mConn.getState();
                accountId = mConn.getAccountId();
                providerId = mConn.getProviderId();
                
                if (state < ImConnection.LOGGED_IN) {                
                    
                    Cursor cursorProvider = initProviderCursor();
                    
                    while(cursorProvider.moveToNext())
                    {
{'fraction_non_alphanumeric': 0.0782674294335827, 'fraction_numerical': 0.0027660778273715975, 'mean_word_length': 1.6889790398918187, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4261968', 'n_tokens_mistral': 3997, 'n_tokens_neox': 3802, 'n_words': 919}