<commit_before>package com.martin.kantidroid;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dropbox.sync.android.DbxAccountManager;
import com.dropbox.sync.android.DbxException;
import com.dropbox.sync.android.DbxException.Unauthorized;
import com.dropbox.sync.android.DbxFile;
import com.dropbox.sync.android.DbxFileInfo;
import com.dropbox.sync.android.DbxFileSystem;
import com.dropbox.sync.android.DbxFileSystem.SyncStatusListener;
import com.dropbox.sync.android.DbxPath;
import com.dropbox.sync.android.DbxPath.InvalidPathException;

import org.holoeverywhere.app.Activity;
import org.holoeverywhere.app.AlertDialog;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.preference.SharedPreferences.Editor;
import org.holoeverywhere.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Backup extends Activity implements OnClickListener {

    private static final int REQUEST_LINK_TO_DBX = 0;
    private TextView tvSyncprogress;
    private ProgressBar pbDbx;
    private ImageView ivSuccess;
    private final Context context = this;
    private File databases;
    private File preferences;
    private File backupdatabases;
    private File backuppreferences;
    private File[] files;
    private String[] prefnames;
    private DbxPath databasePath;
    private DbxPath prefPath;
    private DbxFile dbxFile;
    private DbxAccountManager mDbxAcctMgr;
    private DbxFileSystem dbxFs;

    // TODO: Look into progress indicator not starting to turn -> Changelog

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String appSecret = getApplicationContext().getString(R.string.appSecret);

        setContentView(R.layout.backup_combined);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ImageButton bBackup = (ImageButton) findViewById(R.id.bBackup);
        ImageButton bImport = (ImageButton) findViewById(R.id.bImport);
        TextView tvSync = (TextView) findViewById(R.id.tvSyncRoboto);
        TextView tvBackup = (TextView) findViewById(R.id.tvBackupRoboto);
        tvSyncprogress = (TextView) findViewById(R.id.tvSyncprogressRoboto);
        Button bDbxBackup = (Button) findViewById(R.id.bDbxBackup);
        Button bDbxImport = (Button) findViewById(R.id.bDbxImport);
        pbDbx = (ProgressBar) findViewById(R.id.pbDbx);
        ivSuccess = (ImageView) findViewById(R.id.ivSuccess);
        bBackup.setOnClickListener(this);
        bImport.setOnClickListener(this);
        bDbxBackup.setOnClickListener(this);
        bDbxImport.setOnClickListener(this);

        File appdir = getFilesDir().getParentFile();
        databases = new File(appdir + "/databases");
        preferences = new File(appdir + "/shared_prefs");
        File kdroiddir = new File(Environment.getExternalStorageDirectory(), "/Kantidroid");
        backupdatabases = new File(kdroiddir + "/backup/databases");
        backuppreferences = new File(kdroiddir + "/backup/shared_prefs");
        prefnames = getResources().getStringArray(R.array.prefnames);

        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf");
        tvSync.setTypeface(tf);
        tvBackup.setTypeface(tf);
        tvSyncprogress.setTypeface(tf);

        String appKey = "<KEY>";
        mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);

        if (!mDbxAcctMgr.hasLinkedAccount()) {
            tvSyncprogress.setVisibility(View.INVISIBLE);
            pbDbx.setVisibility(View.INVISIBLE);
            ivSuccess.setVisibility(View.INVISIBLE);
        }
        initFs();
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.bBackup:
                localBackup();
                break;
            case R.id.bImport:
                localImport();
                break;
            case R.id.bDbxBackup:
                if (!mDbxAcctMgr.hasLinkedAccount()) {
                    mDbxAcctMgr.startLink(this, REQUEST_LINK_TO_DBX);
                } else {
                    AlertDialog.Builder delDg = new AlertDialog.Builder(this);
                    delDg.setTitle("Dropbox");
                    delDg.setMessage("Damit werden bereits vorhandene lokale Backups berschrieben.\n\nWillst du fortfahren?\n");
                    delDg.setNegativeButton("Nein", null);
                    delDg.setPositiveButton("Ja", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Make local backup

                            localBackup_noDg();

                            // Sync to Dropbox

                            // databases

                            if (databases.isDirectory()) {
                                files = databases.listFiles();
                                for (int i = 0; i < files.length; i++) {
                                    try {
                                        if (dbxFs.exists(new DbxPath(databasePath, files[i].getName()))) {
                                            dbxFile = dbxFs.open(new DbxPath(databasePath, files[i].getName()));
                                        } else {
                                            dbxFile = dbxFs.create(new DbxPath(databasePath, files[i].getName()));
                                        }
                                        dbxFile.writeFromExistingFile(files[i], false);
                                        dbxFile.close();
                                    } catch (InvalidPathException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    } catch (DbxException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    }
                                }
                            }

                            // shared_prefs

                            if (preferences.isDirectory()) {
                                for (int i1 = 0; i1 < prefnames.length; i1++) {
                                    try {
                                        if (dbxFs.exists(new DbxPath(prefPath, prefnames[i1]))) {
                                            dbxFile = dbxFs.open(new DbxPath(prefPath, prefnames[i1]));
                                        } else {
                                            dbxFile = dbxFs.create(new DbxPath(prefPath, prefnames[i1]));
                                        }
                                        dbxFile.writeFromExistingFile(new File(backuppreferences + "/" + prefnames[i1]), false);
                                        dbxFile.close();
                                    } catch (InvalidPathException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    } catch (DbxException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    }
                                }
                            }
                            Toast.makeText(getApplicationContext(), "Daten werden hochgeladen", Toast.LENGTH_SHORT).show();
                        }

                    });
                    delDg.show();
                }
                break;
            case R.id.bDbxImport:
                if (!mDbxAcctMgr.hasLinkedAccount()) {
                    mDbxAcctMgr.startLink(this, REQUEST_LINK_TO_DBX);
                } else {
                    final Handler handler = new Handler();
                    AlertDialog.Builder delDg = new AlertDialog.Builder(this);
                    delDg.setTitle("Dropbox");
                    delDg.setMessage("Damit werden bereits vorhandene lokale Backups berschrieben.\n\nWillst du fortfahren?\n");
                    delDg.setNegativeButton("Nein", null);
                    delDg.setPositiveButton("Ja", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {<commit_msg>Fixed progressBar not spinning<commit_after>package com.martin.kantidroid;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dropbox.sync.android.DbxAccountManager;
import com.dropbox.sync.android.DbxException;
import com.dropbox.sync.android.DbxException.Unauthorized;
import com.dropbox.sync.android.DbxFile;
import com.dropbox.sync.android.DbxFileInfo;
import com.dropbox.sync.android.DbxFileSystem;
import com.dropbox.sync.android.DbxFileSystem.SyncStatusListener;
import com.dropbox.sync.android.DbxPath;
import com.dropbox.sync.android.DbxPath.InvalidPathException;

import org.holoeverywhere.app.Activity;
import org.holoeverywhere.app.AlertDialog;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.preference.SharedPreferences.Editor;
import org.holoeverywhere.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Backup extends Activity implements OnClickListener {

    private static final int REQUEST_LINK_TO_DBX = 0;
    private TextView tvSyncprogress;
    private ProgressBar pbDbx;
    private ImageView ivSuccess;
    private final Context context = this;
    private File databases;
    private File preferences;
    private File backupdatabases;
    private File backuppreferences;
    private File[] files;
    private String[] prefnames;
    private DbxPath databasePath;
    private DbxPath prefPath;
    private DbxFile dbxFile;
    private DbxAccountManager mDbxAcctMgr;
    private DbxFileSystem dbxFs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String appSecret = getApplicationContext().getString(R.string.appSecret);

        setContentView(R.layout.backup_combined);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ImageButton bBackup = (ImageButton) findViewById(R.id.bBackup);
        ImageButton bImport = (ImageButton) findViewById(R.id.bImport);
        TextView tvSync = (TextView) findViewById(R.id.tvSyncRoboto);
        TextView tvBackup = (TextView) findViewById(R.id.tvBackupRoboto);
        tvSyncprogress = (TextView) findViewById(R.id.tvSyncprogressRoboto);
        Button bDbxBackup = (Button) findViewById(R.id.bDbxBackup);
        Button bDbxImport = (Button) findViewById(R.id.bDbxImport);
        pbDbx = (ProgressBar) findViewById(R.id.pbDbx);
        ivSuccess = (ImageView) findViewById(R.id.ivSuccess);
        bBackup.setOnClickListener(this);
        bImport.setOnClickListener(this);
        bDbxBackup.setOnClickListener(this);
        bDbxImport.setOnClickListener(this);

        File appdir = getFilesDir().getParentFile();
        databases = new File(appdir + "/databases");
        preferences = new File(appdir + "/shared_prefs");
        File kdroiddir = new File(Environment.getExternalStorageDirectory(), "/Kantidroid");
        backupdatabases = new File(kdroiddir + "/backup/databases");
        backuppreferences = new File(kdroiddir + "/backup/shared_prefs");
        prefnames = getResources().getStringArray(R.array.prefnames);

        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf");
        tvSync.setTypeface(tf);
        tvBackup.setTypeface(tf);
        tvSyncprogress.setTypeface(tf);

        String appKey = "<KEY>";
        mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);

        if (!mDbxAcctMgr.hasLinkedAccount()) {
            tvSyncprogress.setVisibility(View.INVISIBLE);
            pbDbx.setVisibility(View.INVISIBLE);
            ivSuccess.setVisibility(View.INVISIBLE);
        }
        initFs();
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.bBackup:
                localBackup();
                break;
            case R.id.bImport:
                localImport();
                break;
            case R.id.bDbxBackup:
                if (!mDbxAcctMgr.hasLinkedAccount()) {
                    mDbxAcctMgr.startLink(this, REQUEST_LINK_TO_DBX);
                } else {
                    AlertDialog.Builder delDg = new AlertDialog.Builder(this);
                    delDg.setTitle("Dropbox");
                    delDg.setMessage("Damit werden bereits vorhandene lokale Backups berschrieben.\n\nWillst du fortfahren?\n");
                    delDg.setNegativeButton("Nein", null);
                    delDg.setPositiveButton("Ja", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Make local backup

                            localBackup_noDg();

                            // Sync to Dropbox

                            // databases

                            if (databases.isDirectory()) {
                                files = databases.listFiles();
                                for (int i = 0; i < files.length; i++) {
                                    try {
                                        if (dbxFs.exists(new DbxPath(databasePath, files[i].getName()))) {
                                            dbxFile = dbxFs.open(new DbxPath(databasePath, files[i].getName()));
                                        } else {
                                            dbxFile = dbxFs.create(new DbxPath(databasePath, files[i].getName()));
                                        }
                                        dbxFile.writeFromExistingFile(files[i], false);
                                        dbxFile.close();
                                    } catch (InvalidPathException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    } catch (DbxException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    }
                                }
                            }

                            // shared_prefs

                            if (preferences.isDirectory()) {
                                for (int i1 = 0; i1 < prefnames.length; i1++) {
                                    try {
                                        if (dbxFs.exists(new DbxPath(prefPath, prefnames[i1]))) {
                                            dbxFile = dbxFs.open(new DbxPath(prefPath, prefnames[i1]));
                                        } else {
                                            dbxFile = dbxFs.create(new DbxPath(prefPath, prefnames[i1]));
                                        }
                                        dbxFile.writeFromExistingFile(new File(backuppreferences + "/" + prefnames[i1]), false);
                                        dbxFile.close();
                                    } catch (InvalidPathException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    } catch (DbxException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                                        e.printStackTrace();
                                    }
                                }
                            }
                            Toast.makeText(getApplicationContext(), "Daten werden hochgeladen", Toast.LENGTH_SHORT).show();
                            if (dbxSynchronizing()) {
                                pbDbx.setVisibility(View.VISIBLE);
                                ivSuccess.setVisibility(View.INVISIBLE);
                                tvSyncprogress.setText("Synchronisieren");
                            } else {
                                pbDbx.setVisibility(View.INVISIBLE);
                                ivSuccess.setVisibility(View.VISIBLE);
                                tvSyncprogress.setText("Synchronisiert");
                            }
                        }

                    });
                    delDg.show();
                }
                break;
            case R.id.bDbxImport:
                if (!mDbxAcctMgr.hasLinkedAccount()) {
                    mDbxAcctMgr.startLink(this, REQUEST_LINK_TO_DBX);
                } else {
                    final Handler handler = new Handler();
                    AlertDialog.Builder delDg = new AlertDialog.Builder(this);
                    delDg.setTitle("Dropbox");
                    delDg.setMessage("Damit werden bereits vorhandene lokale Backups berschrieben.\n\nWillst du fortfahren?\n");
                    delDg.setNegativeButton("Nein", null);
                    delDg.setPositiveButton("Ja", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
{'fraction_non_alphanumeric': 0.09060172481379851, 'fraction_numerical': 0.0011760094080752645, 'mean_word_length': 1.641257926750356, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 96, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '371287', 'n_tokens_mistral': 5101, 'n_tokens_neox': 4871, 'n_words': 902}