Back to project page AndroidWifiServer.
The source code is released under:
Apache License
If you think the Android project AndroidWifiServer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package jp.maju.wifiserver.server; //from ww w. j ava 2 s . c om import java.util.List; import java.util.Map; import java.util.Set; import jp.maju.wifiserver.DBAdapter; import jp.maju.wifiserver.R; import jp.maju.wifiserver.util.CommonUtil; import jp.maju.wifiserver.util.Logger; import jp.maju.wifiserver.util.PreferenceUtil; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Color; import android.graphics.Rect; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Pair; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnLongClickListener; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; /* * Copyright {2014} {Matsuda Jumpei} * * 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. */ public class ServerActivity extends ActionBarActivity implements OnCheckedChangeListener, OnLongClickListener { private static final String TAG = ServerActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_server); getHostTextView() .setText(CommonUtil.getCurrentWifiIP(getApplication())); String ssid = CommonUtil.getCurrentSSID(getApplicationContext()); if (CommonUtil.isServiceRunning(this, ServerService.class)) { int port = PreferenceUtil.getServerPortOnServer(getApplication(), ssid); generateQRCode( ssid + ":" + CommonUtil.getCurrentWifiIP(getApplication()) + ":" + port, 200); getPortEditText().setText(String.valueOf(port)); } getLaunchToggleButton().setChecked( CommonUtil.isServiceRunning(this, ServerService.class)); getPortEditText().setEnabled( !CommonUtil.isServiceRunning(this, ServerService.class)); getLaunchToggleButton().setOnCheckedChangeListener(this); getQRCodeImageView().setOnLongClickListener(this); } private ToggleButton getLaunchToggleButton() { return (ToggleButton) findViewById(R.id.server_toggle_launch); } private EditText getPortEditText() { return (EditText) findViewById(R.id.server_edit_port); } private TextView getHostTextView() { return (TextView) findViewById(R.id.server_text_host); } private ImageView getQRCodeImageView() { return (ImageView) findViewById(R.id.server_qr_image); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { getPortEditText().setEnabled(!isChecked); if (isChecked) { String ssid = CommonUtil.getCurrentSSID(getApplicationContext()); int port = Integer.parseInt(getPortEditText().getText().toString()); PreferenceUtil.setServerPortOnServer(getApplication(), ssid, port); generateQRCode( ssid + ":" + CommonUtil.getCurrentWifiIP(getApplication()) + ":" + port + ":" + PreferenceUtil.getAnchor(getApplication()), 200); CommonUtil.startService(this, ServerService.class); } else { getQRCodeImageView().setImageBitmap(null); CommonUtil.stopService(this, ServerService.class); } } private void generateQRCode(String contents, int length) { try { BitMatrix bitMatrix = new QRCodeWriter().encode(contents, BarcodeFormat.QR_CODE, length, length); Rect origin = new Rect(0, 0, bitMatrix.getWidth(), bitMatrix.getHeight()); Rect pointer = new Rect(0, 0, 0, 0); int[] pixels = new int[origin.width() * origin.height()]; while (pointer.top < origin.bottom) { while (pointer.left < origin.right) { pixels[origin.width() * pointer.top + pointer.left] = bitMatrix .get(pointer.left, pointer.top) ? Color.BLACK : Color.WHITE; pointer.left++; } pointer.left = 0; pointer.top++; } Bitmap qrBitmap = Bitmap.createBitmap(pixels, 0, origin.width(), origin.width(), origin.height(), Config.ARGB_8888); getQRCodeImageView().setImageBitmap(qrBitmap); getQRCodeImageView().setTag(contents); } catch (WriterException e) { Logger.e(TAG, e); } } @Override public boolean onLongClick(View view) { if (view == getQRCodeImageView() && getQRCodeImageView().getTag() != null) { Toast.makeText(getApplicationContext(), (String) getQRCodeImageView().getTag(), Toast.LENGTH_SHORT) .show(); } return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.server, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_server_export) { DBAdapter dbAdapter = new DBAdapter(this).open(); Map<String, List<Pair<Long, Long>>> dataMap = dbAdapter.getAllLoginList(); dbAdapter.close(); Set<String> keys = dataMap.keySet(); StringBuilder sb = new StringBuilder(); sb.append("["); for (String key : keys) { appendData(sb, PreferenceUtil.getUsername(getApplication(), key), dataMap.get(key)); sb.append(","); } if (sb.lastIndexOf(",") == sb.length() - 1) { sb.deleteCharAt(sb.length() - 1); } sb.append("]"); String jsonString = sb.toString(); AlertDialog.Builder builder = new Builder(this); builder.setCancelable(false); EditText tv = new EditText(this); tv.setText(jsonString); builder.setView(tv); builder.setPositiveButton(android.R.string.ok, null); builder.create().show(); } return true; } private void appendData(StringBuilder sb, String username, List<Pair<Long,Long>> timeList) { sb.append("{"); sb.append("username:"); sb.append(username); sb.append(","); sb.append("times:"); sb.append("["); for (Pair<Long, Long> pair : timeList) { sb.append("{"); sb.append("login:"); sb.append(pair.first); sb.append(","); sb.append("logout:"); if (pair.second != null) { sb.append(pair.second); } else { sb.append("-1"); } sb.append("}"); sb.append(","); } if (sb.lastIndexOf(",") == sb.length() - 1) { sb.deleteCharAt(sb.length() - 1); } sb.append("]"); sb.append("}"); } }