package cn.edu.buaa.act.sdp.malwaredetector.activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cn.edu.buaa.act.sdp.malwaredetector.Constant;
import cn.edu.buaa.act.sdp.malwaredetector.R;
import cn.edu.buaa.act.sdp.malwaredetector.adapter.APKListAdapter;
import cn.edu.buaa.act.sdp.malwaredetector.adapter.AppListAdapter;
import cn.edu.buaa.act.sdp.malwaredetector.adapter.ProcessAdapter;
import cn.edu.buaa.act.sdp.malwaredetector.adapter.ServiceAdapter;
import cn.edu.buaa.act.sdp.malwaredetector.entity.PackageInfoAsAPK;
import cn.edu.buaa.act.sdp.malwaredetector.entity.PackageInfoWithSize;
import cn.edu.buaa.act.sdp.malwaredetector.services.MyService;
import cn.edu.buaa.act.sdp.malwaredetector.services.StraceService;
import cn.edu.buaa.act.sdp.malwaredetector.util.AppManager;
public class MainActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private List<PackageInfoWithSize> appList;
private List<PackageInfoAsAPK> apkList;
private List<ActivityManager.RunningAppProcessInfo> processList;
private List<ActivityManager.RunningServiceInfo> serviceList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// AppManager.getLauncherActivity(this);
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent);
// startIntent = new Intent(this, StraceService.class);
// startService(startIntent);
getSupportActionBar().setTitle(getResources().getString(R.string.action_app_manager));
mRecyclerView = (RecyclerView) findViewById(R.id.app_list);
mRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
// appList = AppManager.getAllInstalledApps(this);
appList = new ArrayList<>();
adapter = new AppListAdapter(this, appList);
mRecyclerView.setAdapter(adapter);
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... params) {
String result = "";
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
List<NameValuePair> ps = new ArrayList<NameValuePair>();
TelephonyManager tm = (TelephonyManager) MainActivity.this.getSystemService(TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
ps.add(new BasicNameValuePair("imei", imei));
HttpEntity entity = new UrlEncodedFormEntity(ps, HTTP.UTF_8);
post.setEntity(entity);
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String msg) {
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
}.execute(Constant.TEST_URL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_app) {
getSupportActionBar().setTitle(getResources().getString(R.string.action_app_manager));
appList = AppManager.getAllInstalledApps(this);
adapter = new AppListAdapter(this, appList);
mRecyclerView.setAdapter(adapter);
return true;
}
if (id == R.id.action_apk) {
getSupportActionBar().setTitle(getResources().getString(R.string.action_apk_manager));
apkList = AppManager.getAllApk(this);
adapter = new APKListAdapter(this, apkList);
mRecyclerView.setAdapter(adapter);
return true;
}
if (id == R.id.action_process) {
// getSupportActionBar().setTitle(getResources().getString(R.string.action_process_manager));
// processList = AppManager.getRunningAppProcessInfo(this);
// adapter = new ProcessAdapter(this, processList);
// mRecyclerView.setAdapter(adapter);
AppManager.add(1, 2);
return true;
}
if (id == R.id.action_service) {
getSupportActionBar().setTitle(getResources().getString(R.string.action_service_manager));
serviceList = AppManager.getRunningServices(this);
adapter = new ServiceAdapter(this, serviceList);
mRecyclerView.setAdapter(adapter);
return true;
}
if (id == R.id.action_anti_virus) {
Intent intent = new Intent(MainActivity.this, AntiVirusActivity.class);
startActivity(intent);
return true;
}
if (id == R.id.action_battery_status) {
Intent intent = new Intent(MainActivity.this, BatteryStatusActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}