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
|
package tab.androiddev.com.slide2;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import java.io.InputStream;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
protected static int tmp =0, cpt, Autorise =55, ErreurAndro;
protected SectionsPagerAdapter mSectionsPagerAdapter;
protected static final int TAILLE_IN = 228;
protected static final int TAILLE_OUT = 36;
protected static final int CA_SOC_ID = 79820500;
protected static final int CA_SOC_LI = 79820501;
protected static String ipPcControl = "192.168.0.42";
//protected static String ipPcControl ="192.168.0.108";
protected static String portControl = "54321";
protected static Socket comm = null;
protected static byte[] dataOut = new byte[TAILLE_OUT];
protected static byte[] dataIn = new byte[TAILLE_IN];
protected ViewPager mViewPager;
protected GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
//---------------------------------------------------------------------------
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
View rootView = inflater.inflate(R.layout.page1, container, false);
Timer myTimer;
final TextView tCpt = (TextView) rootView.findViewById(R.id.tCpt);
final TextView tErreur = (TextView) rootView.findViewById(R.id.tErreur);
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
TimerMethod();
}
}, 0, 1000);
return rootView;
case 2:
return inflater.inflate(R.layout.page2, container, false);
case 3:
return inflater.inflate(R.layout.page3, container, false);
}
return inflater.inflate(R.layout.page1, container, false);
}
protected void TimerMethod() {
runOnUiThread (new Thread(new Runnable() {
public void run() {
try {
new ClientSocket().execute(ipPcControl, portControl, "0");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
));
}
}
//---------------------------------------------------------------------------
protected static class ClientSocket extends AsyncTask<String, Void, String> { // obligatoirement déclenché depuis un thread
@Override // ... par ex un Timer
protected String doInBackground(String... params) {
InputStream is = null;
String returnString = null;
int rec;
// recupere la chaine du port a utiliser et la trasnforme en int
int port = Integer.parseInt(params[1]);
if (Autorise !=55) return returnString;
if (comm == null)
{ try { comm = new Socket(params[0], port);
}
catch (Exception e)
{ ErreurAndro |= 2;
////comm = null;
returnString = "Exception " + e.toString();
}
} else
{ try
{ comm.getOutputStream().write(dataOut, 0, TAILLE_OUT);
is = comm.getInputStream();
rec = is.read(dataIn, 0, TAILLE_IN); //This is blocking
if (rec != TAILLE_IN) ErreurAndro |= 8;
else ErreurAndro &= 0xFFF0; // pr Err 2 et 4 et 8
}
catch (Exception e)
{ ErreurAndro |= 4;
////comm = null;
returnString = "Exception " + e.toString();
}
}
return returnString;
}
}
//---------------------------------------------------------------------------
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
//---------------------------------------------------------------------------
} |
Partager