Permalink
Please sign in to comment.
Browse files
Merge pull request #17 from bboyairwreck/ericChee-UniversalAPKWithIPA…
…ddressInput Universal APK - User Input to set IP address
- Loading branch information...
Showing
with
273 additions
and 20 deletions.
- +20 −15 PieMessage-Android/app/src/main/AndroidManifest.xml
- +4 −0 PieMessage-Android/app/src/main/java/com/ericchee/bboyairwreck/piemessage/ChatsActivity.java
- +0 −2 PieMessage-Android/app/src/main/java/com/ericchee/bboyairwreck/piemessage/Constants.java
- +90 −0 PieMessage-Android/app/src/main/java/com/ericchee/bboyairwreck/piemessage/MainActivity.java
- +10 −0 PieMessage-Android/app/src/main/java/com/ericchee/bboyairwreck/piemessage/PieMessageApplication.java
- +61 −0 PieMessage-Android/app/src/main/java/com/ericchee/bboyairwreck/piemessage/PiePreferenceActivity.java
- +5 −1 ...essage-Android/app/src/main/java/com/ericchee/bboyairwreck/piemessage/ReceiveMessagesService.java
- +5 −1 PieMessage-Android/app/src/main/java/com/ericchee/bboyairwreck/piemessage/SendMessageTask.java
- +66 −0 PieMessage-Android/app/src/main/res/layout/activity_main.xml
- +2 −0 PieMessage-Android/app/src/main/res/values/strings.xml
- +10 −0 PieMessage-Android/app/src/main/res/xml/user_settings.xml
- +0 −1 README.md
| @@ -0,0 +1,90 @@ | ||
| +package com.ericchee.bboyairwreck.piemessage; | ||
| + | ||
| +import android.content.Intent; | ||
| +import android.content.SharedPreferences; | ||
| +import android.preference.PreferenceManager; | ||
| +import android.support.v7.app.AppCompatActivity; | ||
| +import android.os.Bundle; | ||
| +import android.view.View; | ||
| +import android.widget.Button; | ||
| +import android.widget.EditText; | ||
| +import android.widget.Toast; | ||
| + | ||
| +public class MainActivity extends AppCompatActivity { | ||
| + private String prefSocketAddressKey; | ||
| + | ||
| + @Override | ||
| + protected void onCreate(Bundle savedInstanceState) { | ||
| + super.onCreate(savedInstanceState); | ||
| + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); | ||
| + this.prefSocketAddressKey = getString(R.string.pref_socket_address_key); | ||
| + | ||
| + // Check if Set IP Address | ||
| + if (!sharedPreferences.contains(prefSocketAddressKey)) { | ||
| + // Show IP Set up | ||
| + setContentView(R.layout.activity_main); | ||
| + sharedPreferences.getString(prefSocketAddressKey, "127.0.0.1"); | ||
| + | ||
| + Button btnStartPie = (Button) findViewById(R.id.btnStartPie); | ||
| + btnStartPie.setOnClickListener(new View.OnClickListener() { | ||
| + @Override | ||
| + public void onClick(View v) { | ||
| + // Get value and | ||
| + EditText etSocketAddress = (EditText) findViewById(R.id.etSocketAddress); | ||
| + | ||
| + // Check if IP is valid string | ||
| + if (validIP(etSocketAddress.getText().toString().trim())) { | ||
| + // Save preference | ||
| + SharedPreferences.Editor editor = sharedPreferences.edit(); | ||
| + editor.putString(prefSocketAddressKey, etSocketAddress.getText().toString().trim()); | ||
| + editor.apply(); | ||
| + | ||
| + // Start ReceiveMessagesService and load ChatActivity | ||
| + PieMessageApplication.getInstance().startReceieveMessagesService(); | ||
| + startChatActivity(); | ||
| + } else { | ||
| + CharSequence text = "In valid IP address"; | ||
| + Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); | ||
| + } | ||
| + } | ||
| + }); | ||
| + } else { | ||
| + // Load to ChatActivity | ||
| + startChatActivity(); | ||
| + } | ||
| + | ||
| + } | ||
| + | ||
| + public static boolean validIP (String ip) { | ||
| + try { | ||
| + if ( ip == null || ip.isEmpty() ) { | ||
| + return false; | ||
| + } | ||
| + | ||
| + String[] parts = ip.split( "\\." ); | ||
| + if ( parts.length != 4 ) { | ||
| + return false; | ||
| + } | ||
| + | ||
| + for ( String s : parts ) { | ||
| + int i = Integer.parseInt( s ); | ||
| + if ( (i < 0) || (i > 255) ) { | ||
| + return false; | ||
| + } | ||
| + } | ||
| + if ( ip.endsWith(".") ) { | ||
| + return false; | ||
| + } | ||
| + | ||
| + return true; | ||
| + } catch (NumberFormatException nfe) { | ||
| + return false; | ||
| + } | ||
| + } | ||
| + | ||
| + private void startChatActivity() { | ||
| + Intent chatActivityIntent = new Intent(this, ChatsActivity.class); | ||
| + startActivity(chatActivityIntent); | ||
| + finish(); | ||
| + } | ||
| +} |
| @@ -0,0 +1,61 @@ | ||
| +package com.ericchee.bboyairwreck.piemessage; | ||
| + | ||
| +import android.app.Activity; | ||
| +import android.content.Intent; | ||
| +import android.content.SharedPreferences; | ||
| +import android.preference.EditTextPreference; | ||
| +import android.preference.Preference; | ||
| +import android.preference.PreferenceFragment; | ||
| +import android.preference.PreferenceManager; | ||
| +import android.support.v7.app.AppCompatActivity; | ||
| +import android.os.Bundle; | ||
| +import android.util.Log; | ||
| + | ||
| +public class PiePreferenceActivity extends android.preference.PreferenceActivity { | ||
| + | ||
| + @Override | ||
| + protected void onCreate(Bundle savedInstanceState) { | ||
| + super.onCreate(savedInstanceState); | ||
| + getFragmentManager().beginTransaction().replace(android.R.id.content, new PiePreferenceFragment()).commit(); | ||
| + } | ||
| + | ||
| + public static class PiePreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener { | ||
| + private static final String TAG = PiePreferenceFragment.class.getSimpleName(); | ||
| + | ||
| + @Override | ||
| + public void onCreate(Bundle savedInstanceState) { | ||
| + super.onCreate(savedInstanceState); | ||
| + // Load user settings xml resource | ||
| + addPreferencesFromResource(R.xml.user_settings); | ||
| + updateSocketAddressSummary(findPreference(getString(R.string.pref_socket_address_key))); | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | ||
| + Preference pref = findPreference(key); | ||
| + | ||
| + Log.i(TAG, "Preference change detected"); | ||
| + if (pref instanceof EditTextPreference && key.equals(getString(R.string.pref_socket_address_key))) { | ||
| + Log.i(TAG, "Setting IP address"); | ||
| + | ||
| + PieMessageApplication.getInstance().stopService(new Intent(getActivity(), ReceiveMessagesService.class)); | ||
| + PieMessageApplication.getInstance().startReceieveMessagesService(); | ||
| + | ||
| + updateSocketAddressSummary(pref); | ||
| + } | ||
| + } | ||
| + | ||
| + private void updateSocketAddressSummary(Preference pref) { | ||
| + EditTextPreference editTextPreference = (EditTextPreference) pref; | ||
| + pref.setSummary(editTextPreference.getText()); | ||
| + } | ||
| + | ||
| + @Override | ||
| + public void onAttach(Activity activity) { | ||
| + super.onAttach(activity); | ||
| + PreferenceManager.getDefaultSharedPreferences(activity).registerOnSharedPreferenceChangeListener(this); | ||
| + } | ||
| + } | ||
| + | ||
| + | ||
| +} |
| @@ -0,0 +1,66 @@ | ||
| +<?xml version="1.0" encoding="utf-8"?> | ||
| +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| + xmlns:tools="http://schemas.android.com/tools" | ||
| + android:layout_width="match_parent" | ||
| + android:layout_height="match_parent" | ||
| + android:paddingBottom="@dimen/activity_vertical_margin" | ||
| + android:paddingLeft="@dimen/activity_horizontal_margin" | ||
| + android:paddingRight="@dimen/activity_horizontal_margin" | ||
| + android:paddingTop="@dimen/activity_vertical_margin" | ||
| + tools:context="com.ericchee.bboyairwreck.piemessage.MainActivity" | ||
| + android:background="#4b0a8e" | ||
| + > | ||
| + | ||
| + <RelativeLayout | ||
| + android:layout_width="wrap_content" | ||
| + android:layout_height="wrap_content" | ||
| + android:layout_marginTop="30dp" | ||
| + > | ||
| + | ||
| + <TextView | ||
| + android:id="@+id/tvWelcomeTitle" | ||
| + android:layout_width="wrap_content" | ||
| + android:layout_height="wrap_content" | ||
| + android:layout_centerHorizontal="true" | ||
| + android:text="@string/welcome_title" | ||
| + android:textSize="40dp" | ||
| + android:textColor="@color/white" | ||
| + android:textAlignment="center" | ||
| + /> | ||
| + | ||
| + <TextView | ||
| + android:id="@+id/tvEnterOSXIPAddress" | ||
| + android:layout_width="wrap_content" | ||
| + android:layout_height="wrap_content" | ||
| + android:layout_below="@id/tvWelcomeTitle" | ||
| + android:layout_marginTop="60dp" | ||
| + android:layout_centerHorizontal="true" | ||
| + android:textSize="20dp" | ||
| + android:textAlignment="center" | ||
| + android:textColor="@color/white" | ||
| + android:text="Please enter the OSX Client's IP Address" | ||
| + /> | ||
| + <EditText | ||
| + android:id="@+id/etSocketAddress" | ||
| + android:layout_width="match_parent" | ||
| + android:layout_height="wrap_content" | ||
| + android:layout_below="@id/tvEnterOSXIPAddress" | ||
| + android:layout_marginTop="10dp" | ||
| + android:hint="ex. 127.0.0.1" | ||
| + android:textColor="@color/white" | ||
| + android:textColorHint="@color/grey" | ||
| + tools:text="127.0.0.1" | ||
| + android:textAlignment="center" | ||
| + /> | ||
| + </RelativeLayout> | ||
| + | ||
| + <Button | ||
| + android:id="@+id/btnStartPie" | ||
| + android:layout_width="wrap_content" | ||
| + android:layout_height="wrap_content" | ||
| + android:text="Pie Me!" | ||
| + android:layout_alignParentBottom="true" | ||
| + android:layout_alignParentRight="true" | ||
| + /> | ||
| + | ||
| +</RelativeLayout> |
| @@ -0,0 +1,10 @@ | ||
| +<?xml version="1.0" encoding="utf-8"?> | ||
| +<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> | ||
| + <PreferenceCategory android:title="Settings"> | ||
| + <EditTextPreference | ||
| + android:title="Socket IP address" | ||
| + android:summary="Set your OSX Client's IP address" | ||
| + android:key="@string/pref_socket_address_key" | ||
| + /> | ||
| + </PreferenceCategory> | ||
| +</PreferenceScreen> |
0 comments on commit
fec3bbf