Java tutorial
/* * Copyright 2016, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.heinousdog.glassesstory.beggar; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.PopupMenu; import android.util.Log; 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.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.heinousdog.glassesstory.R; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.KeyStore; import java.security.cert.Certificate; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; import static com.google.common.base.Preconditions.checkNotNull; /** * Display a grid of {@link }s. User can choose to view all, active or completed tasks. */ public class BeggarFragment extends Fragment implements BeggarContract.View { private BeggarContract.Presenter mPresenter; public BeggarFragment() { // Requires empty public constructor } public static BeggarFragment newInstance() { return new BeggarFragment(); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onResume() { super.onResume(); Button button1 = (Button) getView().findViewById(R.id.button); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPresenter.loadMoney("https://www.google.com/"); } }); //mPresenter.start(); } @Override public void setPresenter(@NonNull BeggarContract.Presenter presenter) { mPresenter = checkNotNull(presenter); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { //mPresenter.result(requestCode, resultCode); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.beggar_fragment, container, false); setHasOptionsMenu(true); return root; } @Override public void earnCoin() { } @Override public void showMoney() { Button button1 = (Button) getView().findViewById(R.id.button); button1.setText("a money"); /* Thread thread = new Thread(mutiThread); thread.start();*/ } @Override public void showUserTotal(String data) { TextView textView = (TextView) getView().findViewById(R.id.textView); textView.setText(data); } @Override public void showDrop() { } @Override public void showDataFail() { TextView textView = (TextView) getView().findViewById(R.id.button); textView.setText("fail"); } private Runnable mutiThread = new Runnable() { public void run() { // ?? try { // Load CAs from an InputStream // (could be from a resource or ByteArrayInputStream or ...) CertificateFactory cf = CertificateFactory.getInstance("X.509"); // From https://www.washington.edu/itconnect/security/ca/load-der.crt String cert = "-----BEGIN CERTIFICATE-----\n" + "MIIEujCCBCOgAwIBAgICMDIwDQYJKoZIhvcNAQELBQAwgZQxCzAJBgNVBAYTAlVT\n" + "MQswCQYDVQQIEwJXQTEhMB8GA1UEChMYVW5pdmVyc2l0eSBvZiBXYXNoaW5ndG9u\n" + "MRQwEgYDVQQLEwtVVyBTZXJ2aWNlczEXMBUGA1UEAxMOVVcgU2VydmljZXMgQ0Ex\n" + "JjAkBgkqhkiG9w0BCQEWF2hlbHBAY2FjLndhc2hpbmd0b24uZWR1MB4XDTE2MTAx\n" + "NDE5MDAwMVoXDTE5MTAxNTE5MDAwMVowgasxCzAJBgNVBAYTAlVTMQswCQYDVQQI\n" + "EwJXQTEQMA4GA1UEBxMHU2VhdHRsZTEhMB8GA1UEChMYVW5pdmVyc2l0eSBvZiBX\n" + "YXNoaW5ndG9uMQwwCgYDVQQLEwNDQUMxITAfBgNVBAMTGGNl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QS5jcmwwDQYJKoZIhvcNAQELBQADgYEAmqBWWvRoWnXPSE1kNj4OHGWY\n" + "rsF8q4GtiZy0QPNZjh1yHu9M9VcTpQjVgKpdnoC1iCcNqDEyi/2I4HTk3zCMiS5+\n" + "Qs34gWnmStoFY+YNhGyVLC6qwCjLEEtQXkSHtUDrGhXgWgIAa/6swb3jdxj35M+f\n" + "y+7YZMoXDVTKv6WtXHs=\n" + "-----END CERTIFICATE-----\n"; InputStream caInput = new ByteArrayInputStream(cert.getBytes(StandardCharsets.UTF_8)); Certificate ca; try { ca = cf.generateCertificate(caInput); System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN()); } finally { caInput.close(); } // Create a KeyStore containing our trusted CAs String keyStoreType = KeyStore.getDefaultType(); KeyStore keyStore = KeyStore.getInstance(keyStoreType); keyStore.load(null, null); keyStore.setCertificateEntry("ca", ca); // Create a TrustManager that trusts the CAs in our KeyStore String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); tmf.init(keyStore); // Create an SSLContext that uses our TrustManager SSLContext context = SSLContext.getInstance("TLS"); context.init(null, tmf.getTrustManagers(), null); // Tell the URLConnection to use a SocketFactory from our SSLContext URL url = new URL("https://certs.cac.washington.edu/CAtest/"); HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); urlConnection.setSSLSocketFactory(context.getSocketFactory()); InputStream in = urlConnection.getInputStream(); copyInputStreamToOutputStream(in, System.out); // } catch (Exception e) { Log.e("alex", e.getMessage()); } } }; private void copyInputStreamToOutputStream(InputStream in, PrintStream out) throws IOException { byte[] buffer = new byte[1024]; int c = 0; while ((c = in.read(buffer)) != -1) { out.write(buffer, 0, c); } } }