@Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //
    //	this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main_login);

    mEtAccount = (EditText) findViewById(R.id.mainLoginEditAccount);
    mEtPassword = (EditText) findViewById(R.id.mainLoginEditPassword);
    mBtnLogin = (Button) findViewById(R.id.mainLoginBtn);
    mBtnRegister = (Button) findViewById(R.id.main_btn_register);

    /* this is to render the password edittext font to be default */
    mEtPassword.setTypeface(Typeface.DEFAULT);
    mEtPassword.setTransformationMethod(new PasswordTransformationMethod());

    mBtnLogin.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            MainActivity.this.tryLogin();
          }
        });

    mBtnRegister.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            Intent intent0 = new Intent(MainActivity.this, RegisterActivity.class);
            startActivity(intent0);
          }
        });
  }
  private void showUserHashDialog() {
    String userHash = NavigineApp.Settings.getString("user_hash", "");

    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.user_hash_dialog, null);
    _userEdit = (EditText) view.findViewById(R.id.user_hash_edit);
    _userEdit.setText(userHash);
    _userEdit.setTypeface(Typeface.MONOSPACE);
    // _userEdit.addTextChangedListener(new TextWatcher()
    //  {
    //    public void afterTextChanged(Editable s) { }
    //    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    //    public void onTextChanged(CharSequence s, int start, int before, int count)
    //    {
    //      String text = _userEdit.getText().toString();
    //      int length  = _userEdit.getText().length();
    //
    //      if (text.endsWith("-"))
    //        return;
    //
    //      if (count <= before)
    //        return;
    //
    //      if (length == 4 || length == 9 || length == 14)
    //      {
    //        _userEdit.setText((text + "-"));
    //        _userEdit.setSelection(length + 1);
    //      }
    //    }
    //  });

    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(mContext);
    alertBuilder.setView(view);
    alertBuilder.setTitle("Enter user ID");
    alertBuilder.setNegativeButton(
        getString(R.string.cancel_button),
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dlg, int id) {}
        });

    alertBuilder.setPositiveButton(
        getString(R.string.ok_button),
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dlg, int id) {
            String userHash = _userEdit.getText().toString();
            SharedPreferences.Editor editor = NavigineApp.Settings.edit();
            editor.putString("user_hash", userHash);
            editor.commit();
            NavigineApp.applySettings();
            refreshMapList();
          }
        });

    AlertDialog dialog = alertBuilder.create();
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
  }
  public void sendMessage(View view) {
    EditText et = (EditText) findViewById(R.id.editText1);
    final String msg = et.getText().toString();
    et.setText("");
    new Thread(
            new Runnable() {
              public void run() {
                Socket sock = null;
                try {
                  sock = new Socket(ipAddr, sendPort);
                  Log.v(
                      TAG,
                      "send sock: "
                          + sock.getInetAddress().getHostAddress()
                          + sock.isConnected()
                          + Integer.toString(sock.getPort()));
                } catch (UnknownHostException e) {
                  Log.v(ERRORTAG, "" + e.getMessage());
                  e.printStackTrace();
                } catch (IOException e) {
                  Log.v(ERRORTAG, "" + e.getMessage());
                  e.printStackTrace();
                }

                try {
                  PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
                  out.println(msg);
                  updateTextView("<font color=\"#800000\"> <b>Sent: </b></font>" + msg);
                } catch (IOException e) {
                  Log.v(ERRORTAG, "" + e.getMessage());
                  e.printStackTrace();
                }
              }
            })
        .start();
  }
Beispiel #4
0
 public void mOnClick(View v) {
   switch (v.getId()) {
     case R.id.test:
       String rootdir = Environment.getRootDirectory().getAbsolutePath();
       String datadir = Environment.getDataDirectory().getAbsolutePath();
       String cachedir = Environment.getDownloadCacheDirectory().getAbsolutePath();
       mEdit.setText(
           String.format(
               "ext = %s\nroot=%s\ndata=%s\ncache=%s", mSdPath, rootdir, datadir, cachedir));
       break;
     case R.id.save:
       File dir = new File(mSdPath + "/dir");
       dir.mkdir();
       File file = new File(mSdPath + "/dir/file.txt");
       try {
         FileOutputStream fos = new FileOutputStream(file);
         String str = "This file exists in SDcard";
         fos.write(str.getBytes());
         fos.close();
         mEdit.setText("write success");
       } catch (FileNotFoundException e) {
         mEdit.setText("File Not Found." + e.getMessage());
       } catch (SecurityException e) {
         mEdit.setText("Security Exception");
       } catch (Exception e) {
         mEdit.setText(e.getMessage());
       }
       break;
     case R.id.load:
       try {
         FileInputStream fis = new FileInputStream(mSdPath + "/dir/file.txt");
         byte[] data = new byte[fis.available()];
         while (fis.read(data) != -1) {;
         }
         fis.close();
         mEdit.setText(new String(data));
       } catch (FileNotFoundException e) {
         mEdit.setText("File Not Found");
       } catch (Exception e) {;
       }
       break;
   }
 }
 @Override
 public void onClick(View v) {
   // TODO Auto-generated method stub
   switch (v.getId()) {
       // save button
     case R.id.bspref1:
       String str = getd.getText().toString();
       /*	File f=new File(FILENAME);
       try {
       	fos=new FileOutputStream(f);
       	fos.close();
       } catch (FileNotFoundException e) {
       	// TODO Auto-generated catch block
       	e.printStackTrace();
       } catch (IOException e) {
       	// TODO Auto-generated catch block
       	e.printStackTrace();
       }
       */
       try {
         fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
         fos.write(str.getBytes());
         fos.close();
       } catch (FileNotFoundException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
       break;
       // load button
     case R.id.bspref2:
       new LoadStuff().execute(FILENAME);
       break;
   }
 }
  public void tryLogin() {
    mName = mEtAccount.getText().toString();
    mPassword = mEtPassword.getText().toString();

    if (mName.equals("") || mPassword.length() < 5) { // Please Specify Your Name and Sex"
      Toast.makeText(
              MainActivity.this,
              "Please Specify Your Name and Password correctly",
              Toast.LENGTH_LONG)
          .show();
    } else {
      mUserInfo = new UserInfo(mName, 0, 0, 0, 0, 0, 0);

      /*  if mNetcon is connected already, close it first  */
      /*  here we use try because mNetCon might not have been instantiated yet  */
      /*			try {
      				NetConnect.getnetConnect().closeNetConnect();
      			} catch (Exception e) {}
      			try {
      				InitData.closeInitData();
      				FriendListInfo.closeFriendListInfo();
      				ChatServiceData.closeChatServiceData();
      			} catch (Exception e) {}
      */
      CloseAll.closeAll();
      /*  to establish a new connect  */

      NetworkService.getInstance().onInit(this);
      NetworkService.getInstance().setupConnection();
      if (NetworkService.getInstance().getIsConnected()) {
        String usrInfo =
            mUserInfo.toString()
                + GlobalStrings.signinDivider
                + mPassword
                + GlobalStrings.signinDivider;
        NetworkService.getInstance().sendUpload(GlobalMsgTypes.msgHandShake, usrInfo);
      } else {
        NetworkService.getInstance().closeConnection();
        Toast.makeText(this, "failed to connect to Server", Toast.LENGTH_LONG).show();
        return;
      }

      InitData initData = InitData.getInitData();
      initData.start();
      try {
        initData.join();
      } catch (Exception e) {
      }
      mUserInfo = initData.getUserInfo();

      Log.d(
          "connectedApp isonline : ",
          ""
              + mUserInfo.getIsOnline()
              + "+++++++++++++"
              + "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

      if (mUserInfo.getId() < 0) {
        Toast.makeText(this, "invalid username or password", Toast.LENGTH_SHORT).show();
        return;
      }

      Log.d(
          "connectedApp isonline : ",
          ""
              + mUserInfo.getIsOnline()
              + "+++++++++++++"
              + "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

      ConnectedApp connected_app0 = ConnectedApp.getInstance();
      //		    connected_app0.setConnect(mNetCon);
      connected_app0.setUserInfo(mUserInfo);
      connected_app0.clearListActivity();
      connected_app0.instantiateListActivity();

      Intent intent0 = new Intent(MainActivity.this, MainBodyActivity.class);
      //			intent0.putExtra("username", mUserInfo.getName());
      //			intent0.putExtra("usersex", mUserInfo.getSex());
      startActivity(intent0);

      finish();
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // new thread to create connection
    new Thread(
            new Runnable() {
              public void run() {
                String portStr = get_portStr();
                Log.v(TAG, portStr);

                if (portStr.equals("5554")) {
                  sendPort = 11112;
                } else if (portStr.equals("5556")) {
                  sendPort = 11108;
                } else Log.v(TAG, "AVD portStr is neither 5554 nor 5556");

                Log.v(
                    TAG,
                    "recvPort: "
                        + Integer.toString(recvPort)
                        + " sendPort: "
                        + Integer.toString(sendPort));
              }
            })
        .start();

    // listener embedded in this class only
    new Thread(
            new Runnable() {
              public void run() {
                Socket sock1 = null;
                DataInputStream din = null;
                ServerSocket servSocket = null;
                try {
                  servSocket = new ServerSocket(recvPort);
                  Log.v(TAG, "Server Socket port: " + Integer.toString(servSocket.getLocalPort()));
                } catch (IOException e) {
                  Log.v(ERRORTAG, "" + e.getMessage());
                  e.printStackTrace();
                }

                while (true) {
                  try {
                    sock1 = servSocket.accept();
                    Log.v(
                        TAG,
                        "sock1: "
                            + sock1.getInetAddress().getHostAddress()
                            + sock1.isConnected()
                            + Integer.toString(sock1.getLocalPort())
                            + Integer.toString(sock1.getPort()));
                    BufferedReader br =
                        new BufferedReader(new InputStreamReader(sock1.getInputStream()));
                    String str = br.readLine();
                    Log.v(TAG, "recvd msg: " + str);
                    updateTextView("<font color=\"#0000A0\"> <b>Rcvd: </b></font>" + str);
                  } catch (IOException e) {
                    Log.v(ERRORTAG, "" + e.getMessage());
                    e.printStackTrace();
                  } finally {
                    if (din != null)
                      try {
                        din.close();
                      } catch (IOException e) {
                        Log.v(ERRORTAG, "" + e.getMessage());
                        e.printStackTrace();
                      }
                    if (sock1 != null)
                      try {
                        sock1.close();
                      } catch (IOException e) {
                        Log.v(ERRORTAG, "" + e.getMessage());
                        e.printStackTrace();
                      }
                  }
                }
              }
            })
        .start();

    // now just create a thread to send a message
    final EditText editText = (EditText) findViewById(R.id.editText1);

    editText.setOnKeyListener(
        new OnKeyListener() {

          @Override
          public boolean onKey(View arg0, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                && (keyCode == KeyEvent.KEYCODE_ENTER)) {
              sendMessage(arg0);
            }
            return false;
          }
        });
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      getWindow().setStatusBarColor(getResources().getColor(R.color.holo_blue_bright));
    }
    listmain = new ArrayList<ConRecItem>();
    tm = (EditText) findViewById(R.id.setext);
    final RecycleItemClickListener itemClickListener =
        new RecycleItemClickListener() {
          @Override
          public void onItemClick(View view, int position) {
            switch (position) {
              default:
                Intent intent = new Intent(searchActivity.this, conActivity.class);
                Data data = new Data();
                File fl = new File("/storage/sdcard0/M++/data/slist.txt");
                String spos = data.getData(fl, position + 1);
                int pos = Integer.valueOf(spos);
                intent.putExtra("pos", pos);
                startActivity(intent);
                break;
            }
          }
        };
    int displayWidth = 0;
    int displayHeight = 0;
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    displayWidth = displayMetrics.widthPixels;
    displayHeight = displayMetrics.heightPixels;
    final int rpos = displayWidth / 320;
    listmain = new ArrayList<ConRecItem>();
    ConRecAdapter adapter = new ConRecAdapter(listmain, itemClickListener);
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
    recyclerView.setLayoutManager(
        new StaggeredGridLayoutManager(rpos, StaggeredGridLayoutManager.VERTICAL));
    recyclerView.setAdapter(adapter);
    tm.addTextChangedListener(
        new TextWatcher() {

          @Override
          public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4) {
            // TODO: Implement this method
          }

          @Override
          public void onTextChanged(CharSequence p1, int p2, int p3, int p4) {
            // TODO: Implement this method
          }

          @Override
          public void afterTextChanged(Editable edit) {

            // TODO
            listmain = new ArrayList<ConRecItem>();
            System.out.println("okokok");
            File fs = new File("/storage/sdcard0/M++/data/fs_con.txt");
            File fl = new File("/storage/sdcard0/M++/data/slist.txt");
            String text = tm.getText().toString();
            if (fs.exists()) {
              listmain = new ArrayList<ConRecItem>();
              if (fl.exists()) {
                fl.delete();
              }

              try {
                fl.createNewFile();

                String reg = ".*" + text + ".*";

                FileReader fr = new FileReader(fs);

                BufferedReader br = new BufferedReader(fr);

                String temp = null;
                String s = "";

                while ((temp = br.readLine()) != null) {
                  s += temp + "\n";
                }

                String[] ss = s.split("\n");
                for (int i = 0; i <= ss.length; i++) {
                  try {
                    if (ss[10 * i].matches(reg)) {
                      ConRecItem p = new ConRecItem(ss[10 * i + 1], ss[10 * i]);
                      listmain.add(p);
                      String spos = String.valueOf(i);
                      Data data = new Data();
                      data.writeData(fl, spos + "\n", false);
                    }
                  } catch (ArrayIndexOutOfBoundsException e) {
                  }
                }
                ConRecAdapter adapter = new ConRecAdapter(listmain, itemClickListener);
                RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
                recyclerView.setLayoutManager(
                    new StaggeredGridLayoutManager(rpos, StaggeredGridLayoutManager.VERTICAL));
                recyclerView.setAdapter(adapter);

              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          };
        });
  }