Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CredentialDisplayFragment.java « fragments « passman « app « wolfi « es « java « main « src « app - github.com/nextcloud/passman-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a63f5d941b7fa00588d1ca8a3fce9566be2c8572 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
 * Passman Android App
 *
 * @copyright Copyright (c) 2016, Sander Brand (brantje@gmail.com)
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel (wolfi@wolfi.es)
 * @license GNU AGPL version 3 or any later version
 * <p>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * <p>
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.wolfi.app.passman.fragments;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.webkit.URLUtil;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import net.bierbaumer.otp_authenticator.TOTPHelper;

import org.apache.commons.codec.binary.Base32;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.wolfi.app.passman.CopyTextItem;
import es.wolfi.app.passman.R;
import es.wolfi.app.passman.SettingValues;
import es.wolfi.app.passman.SingleTon;
import es.wolfi.app.passman.adapters.CustomFieldViewAdapter;
import es.wolfi.app.passman.adapters.FileViewAdapter;
import es.wolfi.passman.API.Credential;
import es.wolfi.passman.API.File;
import es.wolfi.passman.API.Vault;
import es.wolfi.utils.IconUtils;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CredentialDisplayFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CredentialDisplayFragment extends Fragment {
    public static String CREDENTIAL = "credential";

    @BindView(R.id.credentialIcon)
    ImageView credentialIcon;
    @BindView(R.id.credential_label)
    TextView label;
    @BindView(R.id.credential_user)
    CopyTextItem user;
    @BindView(R.id.credential_password)
    CopyTextItem password;
    @BindView(R.id.credential_email)
    CopyTextItem email;
    @BindView(R.id.credential_url)
    CopyTextItem url;
    @BindView(R.id.credential_description)
    TextView description;
    @BindView(R.id.credential_otp)
    CopyTextItem otp;
    @BindView(R.id.credential_otp_progress)
    ProgressBar otp_progress;
    @BindView(R.id.filesList)
    RecyclerView filesList;
    @BindView(R.id.customFieldsList)
    RecyclerView customFieldsList;

    private Credential credential;
    private Handler handler;
    private Runnable otp_refresh = null;

    private OnCredentialFragmentInteraction mListener;
    private OnListFragmentInteractionListener filelistListener;

    public CredentialDisplayFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param credentialGUID The guid of the credential to display.
     * @return A new instance of fragment CredentialDisplayFragment.
     */
    public static CredentialDisplayFragment newInstance(String credentialGUID) {
        CredentialDisplayFragment fragment = new CredentialDisplayFragment();

        Bundle b = new Bundle();
        b.putString(CREDENTIAL, credentialGUID);
        fragment.setArguments(b);

        return fragment;
    }

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

        if (getArguments() != null) {
            Vault v = (Vault) SingleTon.getTon().getExtra(SettingValues.ACTIVE_VAULT.toString());
            if (v != null) {
                credential = v.findCredentialByGUID(getArguments().getString(CREDENTIAL));
            }
        }

        if (credential != null) {
            handler = new Handler();
            try {
                JSONObject otpObj = new JSONObject(credential.getOtp());
                if (otpObj.has("secret") && otpObj.getString("secret").length() > 4) {
                    String otpSecret = otpObj.getString("secret");
                    otp_refresh = new Runnable() {
                        @Override
                        public void run() {
                            int progress = (int) (System.currentTimeMillis() / 1000) % 30;
                            otp_progress.setProgress(progress * 100);

                            ObjectAnimator animation = ObjectAnimator.ofInt(otp_progress, "progress", (progress + 1) * 100);
                            animation.setDuration(1000);
                            animation.setInterpolator(new LinearInterpolator());
                            animation.start();

                            otp.setText(TOTPHelper.generate(new Base32().decode(otpSecret)));
                            handler.postDelayed(this, 1000);
                        }
                    };
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(getContext(), getString(R.string.error_occurred), Toast.LENGTH_LONG).show();
            Log.e("CredentialDisplayFrag", "credential or vault is null");
        }
    }

    public String getGuid() {
        return this.credential.getGuid();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (otp_refresh != null) {
            handler.post(otp_refresh);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (otp_refresh != null) {
            handler.removeCallbacks(otp_refresh);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_credential_display, container, false);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnCredentialFragmentInteraction) {
            mListener = (OnCredentialFragmentInteraction) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
        if (context instanceof OnListFragmentInteractionListener) {
            filelistListener = (OnListFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);

        if (credential != null) {
            FloatingActionButton editCredentialButton = view.findViewById(R.id.editCredentialButton);
            editCredentialButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getParentFragmentManager()
                            .beginTransaction()
                            .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
                            .replace(R.id.content_password_list, CredentialEditFragment.newInstance(credential.getGuid()), "credentialEdit")
                            .addToBackStack(null)
                            .commit();
                }
            });
            editCredentialButton.setVisibility(View.VISIBLE);


            RecyclerView filesListRecyclerView = (RecyclerView) view.findViewById(R.id.filesList);
            filesListRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            filesListRecyclerView.setAdapter(new FileViewAdapter(credential.getFilesList(), filelistListener));

            RecyclerView customFieldsListRecyclerView = (RecyclerView) view.findViewById(R.id.customFieldsList);
            customFieldsListRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            customFieldsListRecyclerView.setAdapter(new CustomFieldViewAdapter(credential.getCustomFieldsList(), filelistListener));

            if (credential.getCompromised().equals("true")) {
                TextView passwordLabel = view.findViewById(R.id.credential_password_label);
                passwordLabel.setBackgroundColor(getResources().getColor(R.color.compromised));
            }

            label.setText(credential.getLabel());
            user.setText(credential.getUsername());
            password.setModePassword();
            password.setText(credential.getPassword());
            email.setModeEmail();
            email.setText(credential.getEmail());
            url.setText(credential.getUrl());
            description.setText(credential.getDescription());
            otp.setEnabled(false);
            IconUtils.loadIconToImageView(credential.getFavicon(), credentialIcon);

            if (URLUtil.isValidUrl(credential.getUrl())) {
                url.setModeURL();
            }

            if (otp_refresh == null) {
                otp_progress.setProgress(0);
            }
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
        filelistListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnCredentialFragmentInteraction {
        // TODO: Update argument type and name
        void onCredentialFragmentInteraction(Credential credential);
    }

    public interface OnListFragmentInteractionListener {
        // TODO: Update argument type and name
        void onListFragmentInteraction(File item);
    }
}