aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/git/GitOperationActivity.kt
blob: 93e861ec5dc33dbc5938f1b538b797b65cdea0c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */
package com.zeapo.pwdstore.git

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.UserPreference
import com.zeapo.pwdstore.utils.PasswordRepository

open class GitOperationActivity : BaseGitActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        when (intent.extras?.getInt(REQUEST_ARG_OP)) {
            REQUEST_PULL -> syncRepository(REQUEST_PULL)
            REQUEST_PUSH -> syncRepository(REQUEST_PUSH)
            REQUEST_SYNC -> syncRepository(REQUEST_SYNC)
            else -> {
                setResult(RESULT_CANCELED)
                finish()
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.git_clone, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.user_pref -> try {
                val intent = Intent(this, UserPreference::class.java)
                startActivity(intent)
                true
            } catch (e: Exception) {
                println("Exception caught :(")
                e.printStackTrace()
                false
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    /**
     * Syncs the local repository with the remote one (either pull or push)
     *
     * @param operation the operation to execute can be REQUEST_PULL or REQUEST_PUSH
     */
    private fun syncRepository(operation: Int) {
        if (serverUser.isEmpty() || serverHostname.isEmpty() || url.isNullOrEmpty())
            MaterialAlertDialogBuilder(this)
                .setMessage(getString(R.string.set_information_dialog_text))
                .setPositiveButton(getString(R.string.dialog_positive)) { _, _ ->
                    val intent = Intent(this, UserPreference::class.java)
                    startActivityForResult(intent, REQUEST_PULL)
                }
                .setNegativeButton(getString(R.string.dialog_negative)) { _, _ ->
                    // do nothing :(
                    setResult(RESULT_OK)
                    finish()
                }
                .show()
        else {
            // check that the remote origin is here, else add it
            PasswordRepository.addRemote("origin", url!!, true)
            launchGitOperation(operation)
        }
    }
}