summaryrefslogtreecommitdiff
path: root/main/src/main/java/de/blinkt/openvpn/fragments/ShowConfigFragment.java
blob: c3a3196db26d93f4e0b570098b82f6ac675e7ea8 (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
/*
 * Copyright (c) 2012-2016 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */

package de.blinkt.openvpn.fragments;

import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import de.blinkt.openvpn.R;
import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.ProfileManager;


public class ShowConfigFragment extends Fragment {
	private String configtext;
    private TextView mConfigView;
	private ImageButton mfabButton;

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.viewconfig, container,false);
		mConfigView = (TextView) v.findViewById(R.id.configview);
		

		mfabButton = (ImageButton) v.findViewById(R.id.share_config);
        if (mfabButton!=null) {
			mfabButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					shareConfig();
				}
			});
			mfabButton.setVisibility(View.INVISIBLE);
		}
		return v;
	}

	private void startGenConfig(final VpnProfile vp, final TextView cv) {
		
		new Thread() {
			public void run() {
				/* Add a few newlines to make the textview scrollable past the FAB */
				configtext = vp.getConfigFile(getActivity(),false) + "\n\n\n";
				getActivity().runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						cv.setText(configtext);
                        if (mfabButton!=null)
						    mfabButton.setVisibility(View.VISIBLE);
					}
				});
				
				
			}
		}.start();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
		    inflater.inflate(R.menu.configmenu, menu);
	}
	
	private void shareConfig() {
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		shareIntent.putExtra(Intent.EXTRA_TEXT, configtext);
		shareIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.export_config_title));
		shareIntent.setType("text/plain");
		startActivity(Intent.createChooser(shareIntent, getString(R.string.export_config_chooser_title)));
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		final int itemId = item.getItemId();
		if (itemId == R.id.sendConfig) {
			shareConfig();
			return true;
		} else {
			return super.onOptionsItemSelected(item);
		}
	}

    @Override
    public void onResume() {
        super.onResume();

		populateConfigText();
    }

	private void populateConfigText() {
		String profileUUID = getArguments().getString(getActivity().getPackageName() + ".profileUUID");
		final VpnProfile vp = ProfileManager.get(getActivity(),profileUUID);
		int check=vp.checkProfile(getActivity());

		if(check!= R.string.no_error_found) {
            mConfigView.setText(check);
            configtext = getString(check);
        }
        else {
            // Run in own Thread since Keystore does not like to be queried from the main thread

            mConfigView.setText("Generating config...");
            startGenConfig(vp, mConfigView);
        }
	}

	@Override
	public void setUserVisibleHint(boolean visible)
	{
		super.setUserVisibleHint(visible);
		if (visible && isResumed())
			populateConfigText();
	}
}