<commit_before>/*******************************************************************************
 * Copyright 2012 <NAME>
 *
 * 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 com.ubergeek42.WeechatAndroid;

import java.security.cert.CertPath;
import java.security.cert.CertPathValidatorException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;

import javax.net.ssl.SSLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.ubergeek42.WeechatAndroid.fragments.BufferFragment;
import com.ubergeek42.WeechatAndroid.fragments.BufferListFragment;
import com.ubergeek42.WeechatAndroid.fragments.BufferListFragment.OnBufferSelectedListener;
import com.ubergeek42.WeechatAndroid.service.RelayService;
import com.ubergeek42.WeechatAndroid.service.RelayServiceBinder;
import com.ubergeek42.weechat.HotlistItem;
import com.ubergeek42.weechat.relay.RelayConnectionHandler;
import com.viewpagerindicator.TitlePageIndicator;

public class WeechatActivity extends SherlockFragmentActivity implements RelayConnectionHandler, OnBufferSelectedListener, OnPageChangeListener {
    
    private static Logger logger = LoggerFactory.getLogger(WeechatActivity.class);
    private boolean mBound = false;
    private RelayServiceBinder rsb;
    
    private SocketToggleConnection taskToggleConnection;
    private HotlistListAdapter hotlistListAdapter;
    private Menu actionBarMenu;
    
    private ViewPager viewPager;
    private MainPagerAdapter mainPagerAdapter;
    private TitlePageIndicator titleIndicator;
    
    private boolean tabletMode = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Start the background service(if necessary)
        startService(new Intent(this, RelayService.class));

        // Load the layout
        setContentView(R.layout.main_screen);
        
        BufferListFragment blf = (BufferListFragment) getSupportFragmentManager().findFragmentById(R.id.bufferlist_fragment);
        if (blf != null) {
            tabletMode = true;
        }
        
        viewPager = (ViewPager) findViewById(R.id.main_viewpager);
        
        // Restore state if we have it
        if (savedInstanceState != null) {
            // Load the previous BufferListFragment, and the Buffers that were open 
            int numpages = savedInstanceState.getInt("numpages");
            
            ArrayList<BufferFragment> frags = new ArrayList<BufferFragment>();
            for(int i=0;i<numpages;i++) {
                
                if (!tabletMode && i==0) {
                    blf = (BufferListFragment)getSupportFragmentManager().getFragment(savedInstanceState, "saved_frag_0");
                    continue;
                }
                
                BufferFragment f = (BufferFragment)getSupportFragmentManager().getFragment(savedInstanceState, "saved_frag_"+i);
                if (f!=null)
                    frags.add(f);
            }
            mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), viewPager);
            mainPagerAdapter.setBuffers(frags);
            
            if (!tabletMode)
                mainPagerAdapter.setBufferList(blf);
        } else {
            mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), viewPager);
            
            if (!tabletMode)
                mainPagerAdapter.setBufferList(new BufferListFragment());
        }
        viewPager.setAdapter(mainPagerAdapter);
        viewPager.setOffscreenPageLimit(10);// TODO: probably a crash if more than 10 buffers, and screen rotates
        // see: http://stackoverflow.com/questions/11296411/fragmentstatepageradapter-illegalstateexception-myfragment-is-not-currently

        titleIndicator = (TitlePageIndicator) findViewById(R.id.main_titleviewpager);
        titleIndicator.setViewPager(viewPager);
        titleIndicator.setOnPageChangeListener(this);
        titleIndicator.setCurrentItem(0);

        // TODO Read preferences from background, its IO, 31ms strict mode!
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        setTitle(getString(R.string.app_version));
        
        // TODO: make notification load the right buffer
        // TODO: add preference to hide the TitlePageIndicator
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("numpages", mainPagerAdapter.getCount());
        for(int i=0;i<mainPagerAdapter.getCount(); i++) {
            if (mainPagerAdapter.getItem(i)!=null) {
                getSupportFragmentManager().putFragment(outState,"saved_frag_"+i, mainPagerAdapter.getItem(i));
            }
        }
    }
    
    @Override
    public void onBufferSelected(String buffer) {
       // Does the buffer actually exist?(If not, return)
        if (mBound && rsb.getBufferByName(buffer) == null) {
            return;
        }
        mainPagerAdapter.openBuffer(buffer);
        titleIndicator.setCurrentItem(viewPager.getCurrentItem());
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        logger.debug("onNewIntent called");
        handleIntent();
    }
    // when we get an intent, do something with it
    private void handleIntent() {
        // Load a buffer if necessary
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            // Load the given buffer
            onBufferSelected(extras.getString("buffer"));
        } else {
            // Load the bufferlist
        }
    }


    @Override
    public void onConnecting() {

    }

    @Override
    public void onConnect() {
        if (rsb != null && rsb.isConnected()) {
            // Create and update the hotlist
            hotlistListAdapter = new HotlistListAdapter(WeechatActivity.this, rsb);
        }

        // Make sure we update action bar menu after a connection change.
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateMenuContext(actionBarMenu);
            }
        });
    }

    @Override
    public void onAuthenticated() {

    }

    @Override
    public void onDisconnect() {
        // Make sure we update action bar menu after a connection change.
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateMenuContext(actionBarMenu);
            }
        });
    }

    @Override
    public void onError(final String errorMsg, Object extraData) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getBaseContext(), "Error: " + errorMsg, Toast.LENGTH_LONG).show();
            }
        });        
        Log.d("WeechatActivity", "onError:" + errorMsg);
        if (extraData instanceof SSLException) {
            Log.d("[WeechatActivity]", "Cause: "+ ((SSLException)extraData).getCause());
            SSLException e1 = (SSLException) extraData;
            if (e1.getCause() instanceof CertificateException) {
                CertificateException e2 = (CertificateException) e1.getCause();
                
                if (e2.getCause() instanceof CertPathValidatorException) {
                    CertPathValidatorException e = (CertPathValidatorException) e2.getCause();
                    CertPath cp = e.getCertPath();                    
                    
                    // Set the cert error on the backend
                    rsb.setCertificateError((X509Certificate) cp.getCertificates().get(0));
                    
                    // Start an activity to attempt establishing trust
                    Intent i = new Intent(this, SSLCertActivity.class);
                    startActivity(i);
                }
            }
        }
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        if (viewPager.getCurrentItem()==0 && !tabletMode) {
            menu.findItem(R.id.menu_nicklist).setVisible(false);
            menu.findItem(R.id.menu_close).setVisible(false);
        } else {
            menu.findItem(R.id.menu_nicklist).setVisible(true);
            menu.findItem(R.id.menu_close).setVisible(true);
        }
        return true;
    }


    @Override
    // Handle the options when the user presses the Menu key
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_connection_state: {
            if (rsb != null) {
                taskToggleConnection = new SocketToggleConnection();
                taskToggleConnection.execute();
            }
            break;<commit_msg>home button navigates to buffer list<commit_after>/*******************************************************************************
 * Copyright 2012 <NAME>
 *
 * 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 com.ubergeek42.WeechatAndroid;

import java.security.cert.CertPath;
import java.security.cert.CertPathValidatorException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;

import javax.net.ssl.SSLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.ubergeek42.WeechatAndroid.fragments.BufferFragment;
import com.ubergeek42.WeechatAndroid.fragments.BufferListFragment;
import com.ubergeek42.WeechatAndroid.fragments.BufferListFragment.OnBufferSelectedListener;
import com.ubergeek42.WeechatAndroid.service.RelayService;
import com.ubergeek42.WeechatAndroid.service.RelayServiceBinder;
import com.ubergeek42.weechat.HotlistItem;
import com.ubergeek42.weechat.relay.RelayConnectionHandler;
import com.viewpagerindicator.TitlePageIndicator;

public class WeechatActivity extends SherlockFragmentActivity implements RelayConnectionHandler, OnBufferSelectedListener, OnPageChangeListener {
    
    private static Logger logger = LoggerFactory.getLogger(WeechatActivity.class);
    private boolean mBound = false;
    private RelayServiceBinder rsb;
    
    private SocketToggleConnection taskToggleConnection;
    private HotlistListAdapter hotlistListAdapter;
    private Menu actionBarMenu;
    
    private ViewPager viewPager;
    private MainPagerAdapter mainPagerAdapter;
    private TitlePageIndicator titleIndicator;
    
    private boolean tabletMode = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Start the background service(if necessary)
        startService(new Intent(this, RelayService.class));

        // Load the layout
        setContentView(R.layout.main_screen);
        
        BufferListFragment blf = (BufferListFragment) getSupportFragmentManager().findFragmentById(R.id.bufferlist_fragment);
        if (blf != null) {
            tabletMode = true;
        }
        
        viewPager = (ViewPager) findViewById(R.id.main_viewpager);
        
        // Restore state if we have it
        if (savedInstanceState != null) {
            // Load the previous BufferListFragment, and the Buffers that were open 
            int numpages = savedInstanceState.getInt("numpages");
            
            ArrayList<BufferFragment> frags = new ArrayList<BufferFragment>();
            for(int i=0;i<numpages;i++) {
                
                if (!tabletMode && i==0) {
                    blf = (BufferListFragment)getSupportFragmentManager().getFragment(savedInstanceState, "saved_frag_0");
                    continue;
                }
                
                BufferFragment f = (BufferFragment)getSupportFragmentManager().getFragment(savedInstanceState, "saved_frag_"+i);
                if (f!=null)
                    frags.add(f);
            }
            mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), viewPager);
            mainPagerAdapter.setBuffers(frags);
            
            if (!tabletMode)
                mainPagerAdapter.setBufferList(blf);
        } else {
            mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), viewPager);
            
            if (!tabletMode)
                mainPagerAdapter.setBufferList(new BufferListFragment());
        }
        viewPager.setAdapter(mainPagerAdapter);
        viewPager.setOffscreenPageLimit(10);// TODO: probably a crash if more than 10 buffers, and screen rotates
        // see: http://stackoverflow.com/questions/11296411/fragmentstatepageradapter-illegalstateexception-myfragment-is-not-currently

        titleIndicator = (TitlePageIndicator) findViewById(R.id.main_titleviewpager);
        titleIndicator.setViewPager(viewPager);
        titleIndicator.setOnPageChangeListener(this);
        titleIndicator.setCurrentItem(0);

        // TODO Read preferences from background, its IO, 31ms strict mode!
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        setTitle(getString(R.string.app_version));
        getSupportActionBar().setHomeButtonEnabled(true);
        
        // TODO: make notification load the right buffer
        // TODO: add preference to hide the TitlePageIndicator
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("numpages", mainPagerAdapter.getCount());
        for(int i=0;i<mainPagerAdapter.getCount(); i++) {
            if (mainPagerAdapter.getItem(i)!=null) {
                getSupportFragmentManager().putFragment(outState,"saved_frag_"+i, mainPagerAdapter.getItem(i));
            }
        }
    }
    
    @Override
    public void onBufferSelected(String buffer) {
       // Does the buffer actually exist?(If not, return)
        if (mBound && rsb.getBufferByName(buffer) == null) {
            return;
        }
        mainPagerAdapter.openBuffer(buffer);
        titleIndicator.setCurrentItem(viewPager.getCurrentItem());
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        logger.debug("onNewIntent called");
        handleIntent();
    }
    // when we get an intent, do something with it
    private void handleIntent() {
        // Load a buffer if necessary
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            // Load the given buffer
            onBufferSelected(extras.getString("buffer"));
        } else {
            // Load the bufferlist
        }
    }


    @Override
    public void onConnecting() {

    }

    @Override
    public void onConnect() {
        if (rsb != null && rsb.isConnected()) {
            // Create and update the hotlist
            hotlistListAdapter = new HotlistListAdapter(WeechatActivity.this, rsb);
        }

        // Make sure we update action bar menu after a connection change.
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateMenuContext(actionBarMenu);
            }
        });
    }

    @Override
    public void onAuthenticated() {

    }

    @Override
    public void onDisconnect() {
        // Make sure we update action bar menu after a connection change.
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateMenuContext(actionBarMenu);
            }
        });
    }

    @Override
    public void onError(final String errorMsg, Object extraData) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getBaseContext(), "Error: " + errorMsg, Toast.LENGTH_LONG).show();
            }
        });        
        Log.d("WeechatActivity", "onError:" + errorMsg);
        if (extraData instanceof SSLException) {
            Log.d("[WeechatActivity]", "Cause: "+ ((SSLException)extraData).getCause());
            SSLException e1 = (SSLException) extraData;
            if (e1.getCause() instanceof CertificateException) {
                CertificateException e2 = (CertificateException) e1.getCause();
                
                if (e2.getCause() instanceof CertPathValidatorException) {
                    CertPathValidatorException e = (CertPathValidatorException) e2.getCause();
                    CertPath cp = e.getCertPath();                    
                    
                    // Set the cert error on the backend
                    rsb.setCertificateError((X509Certificate) cp.getCertificates().get(0));
                    
                    // Start an activity to attempt establishing trust
                    Intent i = new Intent(this, SSLCertActivity.class);
                    startActivity(i);
                }
            }
        }
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        if (viewPager.getCurrentItem()==0 && !tabletMode) {
            menu.findItem(R.id.menu_nicklist).setVisible(false);
            menu.findItem(R.id.menu_close).setVisible(false);
        } else {
            menu.findItem(R.id.menu_nicklist).setVisible(true);
            menu.findItem(R.id.menu_close).setVisible(true);
        }
        return true;
    }


    @Override
    // Handle the options when the user presses the Menu key
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home: {
            if (viewPager != null)
                viewPager.setCurrentItem(0);
            break;
        }
        case R.id.menu_connection_state: {
            if (rsb != null) {
                taskToggleConnection = new SocketToggleConnection();
                taskToggleConnection.execute();
            }
            break;
{'fraction_non_alphanumeric': 0.10213857170505129, 'fraction_numerical': 0.005951228952970776, 'mean_word_length': 2.56608005521049, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '2408208', 'n_tokens_mistral': 5473, 'n_tokens_neox': 5286, 'n_words': 1208}