<commit_before>package org.sorz.lab.smallcloudemoji;


import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.sony.smallapp.SmallAppWindow;
import com.sony.smallapp.SmallApplication;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Small application for Sony.
 */
public class MainApplication extends SmallApplication {
    private SharedPreferences sharedPreferences;
    private HistoryDataSource historyDataSource;
    private List<EmojiGroup> emojiGroups;

    @Override
    public void onCreate() {
        super.onCreate();
        setContentView(R.layout.application_main);
        setTitle(R.string.app_name);

        SmallAppWindow.Attributes attr = getWindow().getAttributes();
        attr.minHeight = 350;
        attr.minWidth = 350;
        attr.width = 500;
        attr.height = 650;
        getWindow().setAttributes(attr);

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        historyDataSource = new HistoryDataSource(this);

        loadAllGroupsOrDownload();
        final ExpandableListView listView =
                (ExpandableListView) findViewById(R.id.expandableListView);
        final MainExpandableAdapter adapter = new MainExpandableAdapter(this, emojiGroups);
        listView.setAdapter(adapter);
        listView.expandGroup(0, false);
        listView.expandGroup(adapter.getGroupCount() - 1, false);

        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                        int childPosition, long id) {
                Object emoji = adapter.getChild(groupPosition, childPosition);
                if (emoji != null) {
                    ClipboardManager clipboard =
                            (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                    ClipData clipData = ClipData.newPlainText("emoji", emoji.toString());
                    clipboard.setPrimaryClip(clipData);
                    Toast.makeText(MainApplication.this, R.string.toast_copied,
                            Toast.LENGTH_SHORT).show();
                    historyDataSource.updateHistory((Emoji) emoji);

                    String action = sharedPreferences.getString("action_after_copied", "MINIMIZE");
                    if (action.equals("MINIMIZE"))
                        getWindow().setWindowState(SmallAppWindow.WindowState.MINIMIZED);
                    else if (action.equals("CLOSE"))
                        finish();
                    if (! action.equals("CLOSE")) {
                        updateFavoriteGroup();
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    Intent intent = new Intent(MainApplication.this, SettingsActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                }
                return true;
            }
        });

    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private void loadAllGroupsOrDownload() {
        if (emojiGroups == null)
            emojiGroups = new ArrayList<EmojiGroup>();

        updateFavoriteGroup();
        updateCategoryGroups();

        // TODO: Download if it's empty.
    }

    /**
     * Read favorite group into emojiGroups.
     * Create or update.
     */
    private void updateFavoriteGroup() {
        int count = Integer.parseInt(sharedPreferences.getString("favorites_count", "8"));
        EmojiGroup favoriteGroup = new EmojiGroup(
                getResources().getString(R.string.list_title_favorite),
                Arrays.asList(historyDataSource.getFavorites(count)));
        if (emojiGroups.size() == 0)
            emojiGroups.add(favoriteGroup);
        else
            emojiGroups.set(0, favoriteGroup);
    }

    /**
     * Read category groups into emojiGroups.
     * Create or update.
     */
    private void updateCategoryGroups() {
        List<EmojiGroup> categoryGroups;
        try {
            categoryGroups = XmlSourceParser.parserAll(this, "emojis.xml");
        } catch (IOException e) {<commit_msg>Auto sync source on first boot<commit_after>package org.sorz.lab.smallcloudemoji;


import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.sony.smallapp.SmallAppWindow;
import com.sony.smallapp.SmallApplication;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Small application for Sony.
 */
public class MainApplication extends SmallApplication {
    private SharedPreferences sharedPreferences;
    private HistoryDataSource historyDataSource;
    private List<EmojiGroup> emojiGroups;
    private MainExpandableAdapter adapter;

    @Override
    public void onCreate() {
        super.onCreate();
        setContentView(R.layout.application_main);
        setTitle(R.string.app_name);

        SmallAppWindow.Attributes attr = getWindow().getAttributes();
        attr.minHeight = 350;
        attr.minWidth = 350;
        attr.width = 500;
        attr.height = 650;
        getWindow().setAttributes(attr);

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        historyDataSource = new HistoryDataSource(this);

        loadAllGroupsOrDownload();
        final ExpandableListView listView =
                (ExpandableListView) findViewById(R.id.expandableListView);
        adapter = new MainExpandableAdapter(this, emojiGroups);
        listView.setAdapter(adapter);
        listView.expandGroup(0, false);
        listView.expandGroup(adapter.getGroupCount() - 1, false);

        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                        int childPosition, long id) {
                Object emoji = adapter.getChild(groupPosition, childPosition);
                if (emoji != null) {
                    ClipboardManager clipboard =
                            (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                    ClipData clipData = ClipData.newPlainText("emoji", emoji.toString());
                    clipboard.setPrimaryClip(clipData);
                    Toast.makeText(MainApplication.this, R.string.toast_copied,
                            Toast.LENGTH_SHORT).show();
                    historyDataSource.updateHistory((Emoji) emoji);

                    String action = sharedPreferences.getString("action_after_copied", "MINIMIZE");
                    if (action.equals("MINIMIZE"))
                        getWindow().setWindowState(SmallAppWindow.WindowState.MINIMIZED);
                    else if (action.equals("CLOSE"))
                        finish();
                    if (! action.equals("CLOSE")) {
                        updateFavoriteGroup();
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    Intent intent = new Intent(MainApplication.this, SettingsActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                }
                return true;
            }
        });

    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private void loadAllGroupsOrDownload() {
        if (emojiGroups == null)
            emojiGroups = new ArrayList<EmojiGroup>();

        updateFavoriteGroup();
        updateCategoryGroups();

        // If all is empty, auto sync source.
        if (emojiGroups.size() == 1 && emojiGroups.get(0).size() == 0) {

            // Minimize the windows rather than mask the process dialog.
            getWindow().setWindowState(SmallAppWindow.WindowState.MINIMIZED);

            String sourceUrl = sharedPreferences.getString("sync_source",
                    getResources().getString(R.string.pref_source_address_default));
            new DownloadXmlAsyncTask(this) {
                @Override
                protected void onPostExecute(Integer result) {
                    super.onPostExecute(result);
                    updateCategoryGroups();
                    adapter.notifyDataSetChanged();
                    getWindow().setWindowState(SmallAppWindow.WindowState.NORMAL);
                }
            }.execute(sourceUrl, "emojis.xml");
        }
    }

    /**
     * Read favorite group into emojiGroups.
     * Create or update.
     */
    private void updateFavoriteGroup() {
        int count = Integer.parseInt(sharedPreferences.getString("favorites_count", "8"));
        EmojiGroup favoriteGroup = new EmojiGroup(
                getResources().getString(R.string.list_title_favorite),
                Arrays.asList(historyDataSource.getFavorites(count)));
        if (emojiGroups.size() == 0)
            emojiGroups.add(favoriteGroup);
        else
            emojiGroups.set(0, favoriteGroup);
    }

    /**
     * Read category groups into emojiGroups.
     * Create or update.
     */
    private void updateCategoryGroups() {
        List<EmojiGroup> categoryGroups;
        try {
            categoryGroups = XmlSourceParser.parserAll(this, "emojis.xml");
        } catch (IOException e) {
{'fraction_non_alphanumeric': 0.09208509007282484, 'fraction_numerical': 0.003737064009198927, 'mean_word_length': 2.1733049559136517, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6000445', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2575, 'n_words': 506}