Last active
December 26, 2015 20:39
-
-
Save Suave/7209820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ore; | |
import android.app.ActionBar; | |
import android.app.Activity; | |
import android.app.Fragment; | |
import android.app.FragmentTransaction; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.graphics.Typeface; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.util.TypedValue; | |
import android.view.*; | |
import android.widget.*; | |
import com.github.kevinsawicki.http.HttpRequest; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import org.json.JSONTokener; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class DeviceActivity extends BaseActivity { | |
public static Context mContext; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
SharedPreferences settings = getSharedPreferences("ore_settings", MODE_PRIVATE); | |
final String authToken = settings.getString("auth_token", ""); | |
if (authToken.equals("")) { | |
// auth_token 在,就认为是已登录状态;不在,就去登陆界面 | |
Intent intent = new Intent(this, LoginActivity.class); | |
startActivity(intent); | |
finish(); | |
} | |
//Log.d("ORE", String.format("factory id:%s", factoryId)); | |
Intent intent = getIntent(); | |
Integer factoryId = intent.getIntExtra(LoginActivity.EXTRA_MESSAGE, 0); | |
setContentView(R.layout.device); | |
mContext = getApplicationContext(); | |
ActionBar actionbar = getActionBar(); | |
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); | |
ActionBar.Tab LiveTab = actionbar.newTab().setText("实时产量"); | |
ActionBar.Tab DailyTab = actionbar.newTab().setText("每日产量"); | |
LiveTab.setTabListener(new MyTabsListener(new LiveFragment(mContext, factoryId))); | |
DailyTab.setTabListener(new MyTabsListener(new DailyFragment())); | |
actionbar.addTab(LiveTab); | |
actionbar.addTab(DailyTab); | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putInt("tab", getActionBar().getSelectedNavigationIndex()); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
MenuInflater inflater = getMenuInflater(); | |
inflater.inflate(R.menu.refresh_menu, menu); | |
return true; | |
} | |
} | |
class LiveFragment extends Fragment { | |
public Integer factoryId; | |
public Context context; | |
public LiveFragment(Context context, Integer factoryId) { | |
this.context = context; | |
this.factoryId = factoryId; | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
new DeviceOutputTask(context, getActivity()).execute(factoryId.toString()); | |
return inflater.inflate(R.layout.live_fragment, container, false); | |
} | |
} | |
class DailyFragment extends Fragment { | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.daily_fragment, container, false); | |
} | |
} | |
class MyTabsListener implements ActionBar.TabListener { | |
public Fragment fragment; | |
public MyTabsListener(Fragment fragment) { | |
this.fragment = fragment; | |
} | |
@Override | |
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { | |
//Toast.makeText(DeviceActivity.mContext, "Reselected!", Toast.LENGTH_LONG).show(); | |
} | |
@Override | |
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { | |
ft.replace(R.id.fragment_container, fragment); | |
} | |
@Override | |
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { | |
ft.remove(fragment); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ore; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.graphics.Typeface; | |
import android.os.AsyncTask; | |
import android.widget.*; | |
import com.github.kevinsawicki.http.HttpRequest; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import org.json.JSONTokener; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: suave | |
* Date: 10/27/13 | |
* Time: 9:23 下午 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class DeviceOutputTask extends AsyncTask<String, Void, String> { | |
private Context mContext; | |
private Activity mActivity; | |
private String DEVICE_OUTPUT_API = "http://211.155.86.253/api/factories/%s?auth_token=%s"; | |
public DeviceOutputTask(Context context, Activity activity) { | |
mContext = context; | |
mActivity = activity; | |
} | |
@Override | |
protected String doInBackground(String... args) { | |
SharedPreferences settings = mActivity.getSharedPreferences("ore_settings", mContext.MODE_PRIVATE); | |
String authToken = settings.getString("auth_token", ""); | |
String response = HttpRequest.get(String.format(DEVICE_OUTPUT_API, args[0], authToken)) | |
.accept("application/json") | |
.contentType("application/json") | |
.body(); | |
return response; | |
} | |
@Override | |
protected void onPostExecute(String response) { | |
//Log.d("ORE", "device output response: "+response); | |
try { | |
final JSONObject deviceJson =(JSONObject) (new JSONTokener(response)).nextValue(); | |
Boolean success = deviceJson.getBoolean("success"); | |
if (success) { | |
Map<String, Object> factory = getFactory(deviceJson); | |
// 设置 activity 标题 | |
mActivity.setTitle(factory.get("name") + "设备产量 网络" + factory.get("status")); | |
TableLayout table = new TableLayout(mActivity); | |
table.setShrinkAllColumns(true); | |
table.setStretchAllColumns(true); | |
// 表格名字 | |
//TableRow rowFactoryName = new TableRow(context); | |
//rowFactoryName.setGravity(Gravity.CENTER_HORIZONTAL); | |
//TextView empty = new TextView(context); | |
//TextView tableTitle = new TextView(context); | |
//tableTitle.setText((String) factory.get("name")); | |
//tableTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18); | |
//tableTitle.setGravity(Gravity.CENTER); | |
//tableTitle.setTypeface(Typeface.SERIF, Typeface.BOLD); | |
//TableRow.LayoutParams params = new TableRow.LayoutParams(); | |
//params.span = 6; | |
//rowFactoryName.addView(tableTitle, params); | |
//table.addView(rowFactoryName); | |
// 表格标题栏 | |
TableRow rowColumnTitle = new TableRow(mActivity); | |
String columnsNames[] = {"设备", "流量(t)", "班产(t)", "日产(t)", "月产(t)", "年产(t)", "状态" }; | |
for (String name : columnsNames) { | |
TextView columnLabel = new TextView(mActivity); | |
columnLabel.setText(name); | |
columnLabel.setTypeface(Typeface.DEFAULT_BOLD); | |
columnLabel.setTextColor(0xFF000000); | |
rowColumnTitle.addView(columnLabel); | |
} | |
table.addView(rowColumnTitle); | |
List<Map<String, Object>> list = getDeviceOutput(deviceJson); | |
//Log.d("ORE", list.toString()); | |
//Log.d("ORE", list.toString()); | |
for (Map<String, Object> d : list) { | |
//Log.d("ORE", (String) d.get("name")); | |
TableRow rowDevice = new TableRow(mActivity); // 数据行 | |
// 设备名称 | |
TextView deviceName = new TextView(mActivity); | |
deviceName.setText((String) d.get("name")); | |
deviceName.setTypeface(Typeface.DEFAULT_BOLD); | |
deviceName.setTextColor(0xFF000000); | |
rowDevice.addView(deviceName); | |
// 流量 | |
TextView flowOutput = new TextView(mActivity); | |
flowOutput.setText((String) d.get("flow")); | |
flowOutput.setTextColor(0xFF000000); | |
rowDevice.addView(flowOutput); | |
// 班产 | |
TextView classOutput = new TextView(mActivity); | |
classOutput.setText((String) d.get("class_output")); | |
classOutput.setTextColor(0xFF000000); | |
rowDevice.addView(classOutput); | |
// 日产 | |
TextView dailyOutput = new TextView(mActivity); | |
dailyOutput.setText((String) d.get("daily")); | |
dailyOutput.setTextColor(0xFF000000); | |
rowDevice.addView(dailyOutput); | |
// 月产 | |
TextView monthlyOutput = new TextView(mActivity); | |
monthlyOutput.setText((String) d.get("monthly")); | |
monthlyOutput.setTextColor(0xFF000000); | |
rowDevice.addView(monthlyOutput); | |
// 年产 | |
TextView annualOutput = new TextView(mActivity); | |
annualOutput.setText((String) d.get("annual")); | |
annualOutput.setTextColor(0xFF000000); | |
rowDevice.addView(annualOutput); | |
// 设备状态 | |
TextView runStatus = new TextView(mActivity); | |
runStatus.setText((String) d.get("status")); | |
runStatus.setTextColor(0xFF000000); | |
rowDevice.addView(runStatus); | |
table.addView(rowDevice); | |
} | |
// Button last_seven_day = new Button(context); | |
// last_seven_day.setText("查看过去7天产量"); | |
// last_seven_day.setOnClickListener(new View.OnClickListener() { | |
// @Override | |
// public void onClick(View view) { | |
// // 把 factory_id 传到下一个 activity | |
// Intent intent = new Intent(getApplicationContext(), DailyChartActivity.class); | |
// intent.putExtra(LoginActivity.EXTRA_MESSAGE, (Integer) ((Map<String, Object>) getFactory(deviceJson)).get("id")); | |
// | |
// startActivity(intent); | |
// } | |
// }); | |
// table.addView(last_seven_day); | |
// | |
// Button last_half_year = new Button(context); | |
// last_half_year.setText("查看过去6个月产量"); | |
// table.addView(last_half_year); | |
mActivity.addContentView(table, new LinearLayout.LayoutParams( )); | |
} else { | |
String message = deviceJson.getString("message"); | |
Toast.makeText(mActivity, message, Toast.LENGTH_LONG).show(); | |
} | |
} catch (JSONException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
private Map<String, Object> getFactory(JSONObject deviceJson) { | |
Map<String, Object> factory = new HashMap<String, Object>(); | |
try { | |
JSONObject factoryJson = (JSONObject) deviceJson.getJSONObject("factory"); | |
factory.put("id", factoryJson.getInt("id")); | |
factory.put("name", factoryJson.getString("name")); | |
factory.put("status", factoryJson.opt("status")); | |
} catch (JSONException ex) { | |
throw new RuntimeException(ex); | |
} | |
return factory; | |
} | |
private List<Map<String, Object>> getDeviceOutput(JSONObject deviceJson) { | |
List<Map<String, Object>> devices = new ArrayList<Map<String, Object>>(); | |
try { | |
JSONArray deviceListJson = (JSONArray) deviceJson.getJSONArray("devices"); | |
//Log.d("ORE", "JSONArray: " + deviceJson.toString()); | |
for (int i=0; i<deviceListJson.length(); i++) { | |
JSONObject d = (JSONObject) deviceListJson.get(i); | |
Map<String, Object> map = new HashMap<String, Object>(); | |
map.put("id", d.get("id")); | |
map.put("name", d.get("name")); | |
map.put("status", d.get("status")); | |
map.put("flow", d.get("flow")); | |
map.put("class_output", d.get("class_output")); | |
map.put("daily", d.get("daily")); | |
map.put("monthly", d.get("monthly")); | |
map.put("annual", d.get("annual")); | |
devices.add(map); | |
} | |
} catch (JSONException ex) { | |
throw new RuntimeException(ex); | |
} | |
return devices; | |
} | |
// | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment