fragments) {
18 | super(fm);
19 | this.tabNames = tabNames;
20 | this.fragments = fragments;
21 | }
22 |
23 | @Override
24 | public int getCount() {
25 | return fragments.size();
26 | }
27 |
28 | @Override
29 | public Fragment getItem(int position) {
30 | return fragments.get(position);
31 | }
32 |
33 | @Override
34 | public CharSequence getPageTitle(int position) {
35 | return tabNames.get(position);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/util/CustomViewpager.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.util;
2 |
3 | import android.content.Context;
4 | import android.support.v4.view.ViewPager;
5 | import android.util.AttributeSet;
6 | import android.view.MotionEvent;
7 |
8 | /**
9 | * Created by stew on 16/9/6.
10 | * mail: stewforani@gmail.com
11 | */
12 | public class CustomViewpager extends ViewPager {
13 |
14 | private boolean isPagingEnabled = true;
15 |
16 | public CustomViewpager(Context context) {
17 | super(context);
18 | }
19 |
20 | public CustomViewpager(Context context, AttributeSet attrs) {
21 | super(context, attrs);
22 | }
23 |
24 | @Override
25 | public boolean onTouchEvent(MotionEvent event) {
26 | return this.isPagingEnabled && super.onTouchEvent(event);
27 | }
28 |
29 | @Override
30 | public boolean onInterceptTouchEvent(MotionEvent event) {
31 | return this.isPagingEnabled && super.onInterceptTouchEvent(event);
32 | }
33 |
34 | public void setPagingEnabled(boolean b) {
35 | this.isPagingEnabled = b;
36 | }}
37 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/wifi_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
9 |
10 |
11 |
21 |
22 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/util/CommandUtils.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.util;
2 |
3 | /**
4 | * Created by stew on 16/9/7.
5 | * mail: stewforani@gmail.com
6 | */
7 | public class CommandUtils {
8 |
9 | public static final String ON="";
10 | public static final String OFF="";
11 |
12 | public static final String BRIGHT_1="";
13 | public static final String BRIGHT_2="";
14 | public static final String BRIGHT_3="";
15 |
16 | public static final String TWIN_1="";
17 | public static final String TWIN_2="";
18 | public static final String TWIN_3="";
19 |
20 | public static final String GRAD_1="";
21 | public static final String GRAD_2="";
22 | public static final String GRAD_3="";
23 |
24 | public static final String COLOR_1="";
25 | public static final String COLOR_2="";
26 | public static final String COLOR_3="";
27 |
28 | public static final String LIGHT_1="";
29 | public static final String LIGHT_2="";
30 |
31 | public static final String SPEED_1="";
32 | public static final String SPEED_2="";
33 | public static final String SPEED_3="";
34 |
35 | public static final String TIMING_OFF="";
36 | public static final String TIMING_ON_1="";
37 | public static final String TIMING_ON_2="";
38 | public static final String TIMING_CURRENT="";
39 | public static final String RESTART="";
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_color.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
20 |
21 |
27 |
28 |
32 |
33 |
38 |
39 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_always_bright.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
20 |
21 |
27 |
28 |
32 |
33 |
38 |
39 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 3-Clause License
2 |
3 | Copyright (c) 2017, stew
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | * Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | * Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | * Neither the name of the copyright holder nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
20 |
21 |
27 |
28 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/activity/ChangeSSIDActivity.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.activity;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.view.View;
6 | import android.widget.Button;
7 | import android.widget.EditText;
8 | import android.widget.ImageView;
9 | import android.widget.Toast;
10 |
11 | import com.lamps.lamps.R;
12 | import com.lamps.lamps.util.ControlLight;
13 |
14 | /**
15 | * Created by stew on 16/9/4.
16 | * mail: stewforani@gmail.com
17 | */
18 | public class ChangeSSIDActivity extends BaseActivity {
19 | @Override
20 | protected void onCreate(@Nullable Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_chnage_ssid);
23 | initView();
24 | }
25 |
26 |
27 | private void initView() {
28 |
29 | ImageView back = (ImageView) findViewById(R.id.setting_back);
30 | final EditText ssid = (EditText) findViewById(R.id.edit_ssid);
31 | Button btn = (Button) findViewById(R.id.button_send);
32 |
33 | if (back == null || ssid == null || btn == null) {
34 | return;
35 | }
36 |
37 | back.setOnClickListener(new View.OnClickListener() {
38 | @Override
39 | public void onClick(View v) {
40 | finish();
41 | }
42 | });
43 | btn.setOnClickListener(new View.OnClickListener() {
44 | @Override
45 | public void onClick(View v) {
46 | if (ssid.getText().toString().isEmpty()) {
47 | Toast.makeText(ChangeSSIDActivity.this, R.string.ssid_can_not_be_null, Toast.LENGTH_SHORT).show();
48 | return;
49 | }
50 |
51 | ControlLight.sendSSID(ssid.getText().toString());
52 | finish();
53 |
54 | }
55 | });
56 |
57 |
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
31 |
32 |
35 |
36 |
39 |
40 |
43 |
44 |
47 |
48 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Lamp
2 | A simple controller of craft lamp for Android.
3 |
4 | #### Intro:
5 | It’s a simple app which can control artificial lights. There is a wifi list on the App’s home page. You can use the wifi of your phone to connect with the lights’wifi module. The data is sent through the Socket in the app, then a new thread is used to monitor the data received by Socket. The data sent are **hexadecimal instructions**, the specific instructions are set based on the wifi module. Due to privacy, the instruction set has been deleted. You can set your own instruction set. Just copy the code if you need it.(Thanks to my friend **Lela** for the translation work)
6 |
7 | Any suggestion, please send to:stewforani@gmail.com,welcome star and fork. Thank you!
8 |
9 | #### show:
10 |
11 | You can see it live downloading [**this apk**](https://github.com/stewForAni/Lamp/raw/master/app-debug.apk)
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 | # License
22 | ```
23 | BSD 3-Clause License
24 |
25 | Copyright (c) 2017, stew
26 | All rights reserved.
27 |
28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/activity/ChangeWifiPsdActivity.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.activity;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.view.View;
6 | import android.widget.Button;
7 | import android.widget.EditText;
8 | import android.widget.ImageView;
9 | import android.widget.Toast;
10 |
11 | import com.lamps.lamps.R;
12 | import com.lamps.lamps.util.ControlLight;
13 |
14 | /**
15 | * Created by stew on 16/9/4.
16 | * mail: stewforani@gmail.com
17 | */
18 | public class ChangeWifiPsdActivity extends BaseActivity {
19 | @Override
20 | protected void onCreate(@Nullable Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_change_wifi_psd);
23 | initView();
24 | }
25 |
26 | private void initView() {
27 |
28 | ImageView back = (ImageView) findViewById(R.id.setting_back);
29 | final EditText ssid = (EditText) findViewById(R.id.edit_psd);
30 | Button btn = (Button) findViewById(R.id.btn_reset);
31 |
32 | if (back == null || ssid == null || btn == null) {
33 | return;
34 | }
35 |
36 | back.setOnClickListener(new View.OnClickListener() {
37 | @Override
38 | public void onClick(View v) {
39 | finish();
40 | }
41 | });
42 | btn.setOnClickListener(new View.OnClickListener() {
43 | @Override
44 | public void onClick(View v) {
45 | if (ssid.getText().toString().isEmpty()) {
46 | Toast.makeText(ChangeWifiPsdActivity.this, R.string.psd_null, Toast.LENGTH_SHORT).show();
47 | return;
48 | }
49 |
50 | if (ssid.getText().toString().length() > 12 || ssid.getText().toString().length() < 8) {
51 | Toast.makeText(ChangeWifiPsdActivity.this, R.string.psd_8_12, Toast.LENGTH_SHORT).show();
52 | return;
53 | }
54 |
55 | ControlLight.sendPassword(ssid.getText().toString());
56 | Toast.makeText(ChangeWifiPsdActivity.this, R.string.psd_change_success, Toast.LENGTH_SHORT).show();
57 | finish();
58 |
59 | }
60 | });
61 |
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_catch_light.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
20 |
21 |
27 |
28 |
32 |
33 |
38 |
39 |
40 |
41 |
45 |
46 |
47 |
52 |
53 |
59 |
60 |
64 |
65 |
70 |
71 |
76 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/fragment/ColorFragment.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.SeekBar;
10 |
11 | import com.lamps.lamps.R;
12 | import com.lamps.lamps.activity.MainActivity;
13 | import com.lamps.lamps.util.CommandUtils;
14 |
15 | /**
16 | * Created by stew on 16/9/3.
17 | * mail: stewforani@gmail.com
18 | */
19 | public class ColorFragment extends Fragment {
20 | private View rootView;
21 | private String order = CommandUtils.COLOR_1;
22 |
23 | @Nullable
24 | @Override
25 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
26 | rootView = inflater.inflate(R.layout.fragment_color, container, false);
27 | return rootView;
28 | }
29 |
30 | @Override
31 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
32 | super.onViewCreated(view, savedInstanceState);
33 | initView();
34 | }
35 |
36 | private void initView() {
37 | SeekBar seekBar = (SeekBar) rootView.findViewById(R.id.seekBar_color);
38 | seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
39 | @Override
40 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
41 |
42 | }
43 |
44 | @Override
45 | public void onStartTrackingTouch(SeekBar seekBar) {
46 |
47 | }
48 |
49 | @Override
50 | public void onStopTrackingTouch(SeekBar seekBar) {
51 |
52 | int progress = seekBar.getProgress();
53 | if (progress < 25) {
54 | seekBar.setProgress(0);
55 | order=CommandUtils.COLOR_1;
56 | } else if (progress < 75) {
57 | seekBar.setProgress(50);
58 | order=CommandUtils.COLOR_2;
59 | } else {
60 | seekBar.setProgress(100);
61 | order=CommandUtils.COLOR_3;
62 | }
63 | ((MainActivity) getActivity()).sendOrder(order);
64 | }
65 | });
66 |
67 |
68 | }
69 |
70 | public String getOrder() {
71 | return order;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/fragment/BrightFragment.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.SeekBar;
10 |
11 | import com.lamps.lamps.R;
12 | import com.lamps.lamps.activity.MainActivity;
13 | import com.lamps.lamps.util.CommandUtils;
14 |
15 | /**
16 | * Created by stew on 16/8/30.
17 | * mail: stewforani@gmail.com
18 | */
19 | public class BrightFragment extends Fragment {
20 | private View rootView;
21 | private String order = CommandUtils.BRIGHT_1;
22 |
23 | @Nullable
24 | @Override
25 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
26 | rootView = inflater.inflate(R.layout.fragment_always_bright, container, false);
27 | return rootView;
28 | }
29 |
30 | @Override
31 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
32 | super.onViewCreated(view, savedInstanceState);
33 | initView();
34 | }
35 |
36 | private void initView() {
37 | SeekBar seekBar = (SeekBar) rootView.findViewById(R.id.seekBar_bright);
38 | seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
39 | @Override
40 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
41 |
42 | }
43 |
44 | @Override
45 | public void onStartTrackingTouch(SeekBar seekBar) {
46 |
47 | }
48 |
49 | @Override
50 | public void onStopTrackingTouch(SeekBar seekBar) {
51 |
52 | int progress = seekBar.getProgress();
53 | if (progress < 25) {
54 | seekBar.setProgress(0);
55 | order = CommandUtils.BRIGHT_1;
56 | } else if (progress < 75) {
57 | seekBar.setProgress(50);
58 | order = CommandUtils.BRIGHT_2;
59 | } else {
60 | seekBar.setProgress(100);
61 | order = CommandUtils.BRIGHT_3;
62 | }
63 | ((MainActivity) getActivity()).sendOrder(order);
64 | }
65 | });
66 | }
67 |
68 | public String getOrder() {
69 | return order;
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_change_language.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
11 |
12 |
19 |
20 |
29 |
30 |
31 |
32 |
33 |
38 |
39 |
40 |
49 |
50 |
56 |
57 |
66 |
67 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_gradient.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
20 |
21 |
27 |
28 |
32 |
33 |
38 |
39 |
44 |
45 |
46 |
47 |
51 |
52 |
53 |
58 |
59 |
65 |
66 |
70 |
71 |
76 |
77 |
82 |
83 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_twinkle.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
20 |
21 |
27 |
28 |
32 |
33 |
38 |
39 |
44 |
45 |
46 |
47 |
51 |
52 |
53 |
58 |
59 |
65 |
66 |
70 |
71 |
76 |
77 |
82 |
83 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/activity/SettingActivity.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.activity;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.view.View;
7 | import android.widget.Button;
8 | import android.widget.ImageView;
9 | import android.widget.TextView;
10 |
11 | import com.lamps.lamps.R;
12 | import com.lamps.lamps.util.CommandUtils;
13 | import com.lamps.lamps.util.ControlLight;
14 |
15 | /**
16 | * Created by stew on 16/9/4.
17 | * mail: stewforani@gmail.com
18 | */
19 | public class SettingActivity extends BaseActivity implements View.OnClickListener {
20 |
21 | @Override
22 | protected void onCreate(@Nullable Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 | setContentView(R.layout.activity_setting);
25 | initView();
26 | }
27 |
28 | private void initView() {
29 |
30 | ImageView back = (ImageView) findViewById(R.id.setting_back);
31 | TextView tv1 = (TextView) findViewById(R.id.change_ssid);
32 | TextView tv2 = (TextView) findViewById(R.id.change_wifi_psd);
33 | TextView tv3 = (TextView) findViewById(R.id.change_language);
34 | Button restart = (Button) findViewById(R.id.restart);
35 | if (back == null || tv1 == null || tv2 == null || tv3 == null||restart==null) {
36 | return;
37 | }
38 |
39 | back.setOnClickListener(new View.OnClickListener() {
40 | @Override
41 | public void onClick(View v) {
42 | finish();
43 | }
44 | });
45 |
46 | tv1.setOnClickListener(this);
47 | tv2.setOnClickListener(this);
48 | tv3.setOnClickListener(this);
49 | restart.setOnClickListener(this);
50 | }
51 |
52 | @Override
53 | public void onClick(View v) {
54 | switch (v.getId()) {
55 |
56 |
57 | case R.id.change_ssid: {
58 | Intent intent = new Intent(SettingActivity.this, ChangeSSIDActivity.class);
59 | startActivity(intent);
60 | }
61 | break;
62 |
63 |
64 | case R.id.change_wifi_psd: {
65 | Intent intent = new Intent(SettingActivity.this, ChangeWifiPsdActivity.class);
66 | startActivity(intent);
67 | }
68 | break;
69 |
70 |
71 | case R.id.change_language: {
72 | Intent intent = new Intent(SettingActivity.this, ChangeLanguageActivity.class);
73 | startActivity(intent);
74 | }
75 | break;
76 |
77 | case R.id.restart: {
78 | ControlLight.sendOrder(CommandUtils.RESTART);
79 | }
80 | break;
81 |
82 | }
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Lamp
3 | wifi未连接,请先
4 | 连接
5 | 亮度
6 | 暗
7 | 正常
8 | 亮
9 | 闪烁
10 | 速度
11 | 交替
12 | 整体
13 | 组合
14 | 慢
15 | 中
16 | 快
17 | 渐明暗
18 | 颜色
19 | 颜色一
20 | 颜色二
21 | 彩色
22 | 追光
23 | 修改SSID
24 | 修改SSID
25 | 修改密码
26 | 设置语言
27 | 重启
28 | 设置
29 | 重置密码
30 | 设置语言
31 | 重置
32 | 简体中文
33 | English
34 | 连接失败
35 |
36 |
37 | 常亮
38 | 闪烁
39 | 渐变
40 | 颜色
41 | 追光
42 | 新密码
43 | 请输入SSID
44 | 进入
45 | 定时
46 | 保存
47 | SSID不能为空!
48 | 开启时间
49 | 关闭时间
50 | 请先打开定时开关!
51 | 提示
52 | 未保存,确定退出么?
53 | 确定
54 | 取消
55 | 请稍等...
56 | 模式二
57 | 模式一
58 | 密码不能为空!
59 | 密码长度必须8-12位
60 | 密码修改成功,请重新连接wifi!
61 |
62 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_change_wifi_psd.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
12 |
13 |
20 |
21 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
42 |
43 |
44 |
56 |
57 |
58 |
65 |
66 |
67 |
68 |
79 |
80 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
22 |
23 |
29 |
30 |
31 |
39 |
40 |
41 |
42 |
48 |
49 |
55 |
56 |
62 |
63 |
69 |
70 |
75 |
76 |
77 |
78 |
79 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_chnage_ssid.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
13 |
14 |
21 |
22 |
31 |
32 |
33 |
34 |
35 |
36 |
42 |
43 |
44 |
56 |
57 |
58 |
65 |
66 |
67 |
68 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_wifi_state.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
17 |
18 |
26 |
27 |
28 |
35 |
36 |
44 |
45 |
46 |
47 |
48 |
57 |
58 |
68 |
69 |
70 |
83 |
84 |
--------------------------------------------------------------------------------
/app/src/main/res/values-en/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Lamp
3 | WiFi not connected , please
4 | Connect
5 | Lighting levels
6 | Dark
7 | Normal
8 | Bright
9 | Twinkle
10 | Speed
11 | Alternation
12 | Whole
13 | Combination
14 | Slow
15 | Medium
16 | Fast
17 |
18 | Gradient
19 | Color
20 | Color_1
21 | Color_2
22 | colour
23 | Light
24 |
25 | change SSID
26 | change password
27 | change language
28 |
29 | change SSID
30 | change password
31 | change language
32 |
33 |
34 | Restart
35 | Set
36 | Reset
37 | 简体中文
38 | English
39 | Connect failed
40 |
41 | Steady
42 | Flashing
43 | Fading
44 | Color
45 | Chasing
46 | New Password
47 | Enter SSID
48 | Enter
49 | Timer
50 | Save
51 | SSID is null !
52 | Start Time
53 | End Time
54 | please open Timing switch first!
55 | Remind
56 | Unsaved, sure to exit ?
57 |
58 | yes
59 | no
60 | One moment please…
61 |
62 | Mode-2
63 | Mode-1
64 |
65 | Please enter password!
66 | The password must be 8-12
67 | reset succeeded,please connect again!
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/util/TcpClient.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.util;
2 |
3 | /**
4 | * Created by stew on 16/8/29.
5 | * mail: stewforani@gmail.com
6 | * TCP Socket客户端
7 | */
8 |
9 | import java.net.InetAddress;
10 | import java.net.Socket;
11 |
12 | public abstract class TcpClient implements Runnable {
13 |
14 | private int port;
15 | private String hostIP;
16 | private boolean connect = false;
17 | private SocketTransceiver transceiver;
18 |
19 | /**
20 | * 建立连接
21 | *
22 | * 连接的建立将在新线程中进行
23 | *
24 | * 连接建立成功,回调{@code onConnect()}
25 | *
26 | * 连接建立失败,回调{@code onConnectFailed()}
27 | *
28 | * @param hostIP 服务器主机IP
29 | * @param port 端口
30 | */
31 | public void connect(String hostIP, int port) {
32 | this.hostIP = hostIP;
33 | this.port = port;
34 | new Thread(this).start();
35 | }
36 |
37 | @Override
38 | public void run() {
39 | try {
40 | Socket socket = new Socket(hostIP, port);
41 | transceiver = new SocketTransceiver(socket) {
42 |
43 | @Override
44 | public void onReceive(InetAddress addr, String s) {
45 | TcpClient.this.onReceive(this, s);
46 | }
47 |
48 | @Override
49 | public void onDisconnect(InetAddress addr) {
50 | connect = false;
51 | TcpClient.this.onDisconnect(this);
52 | }
53 | };
54 | transceiver.start();
55 | connect = true;
56 | this.onConnect(transceiver);
57 | } catch (Exception e) {
58 | e.printStackTrace();
59 | this.onConnectFailed();
60 | }
61 | }
62 |
63 | /**
64 | * 断开连接
65 | *
66 | * 连接断开,回调{@code onDisconnect()}
67 | */
68 | public void disconnect() {
69 | if (transceiver != null) {
70 | transceiver.stop();
71 | transceiver = null;
72 | }
73 | }
74 |
75 | /**
76 | * 判断是否连接
77 | *
78 | * @return 当前处于连接状态,则返回true
79 | */
80 | public boolean isConnected() {
81 | return connect;
82 | }
83 |
84 | /**
85 | * 获取Socket收发器
86 | *
87 | * @return 未连接则返回null
88 | */
89 | public SocketTransceiver getTransceiver() {
90 | return isConnected() ? transceiver : null;
91 | }
92 |
93 | /**
94 | * 连接建立
95 | *
96 | * @param transceiver SocketTransceiver对象
97 | */
98 | public abstract void onConnect(SocketTransceiver transceiver);
99 |
100 | /**
101 | * 连接建立失败
102 | */
103 | public abstract void onConnectFailed();
104 |
105 | /**
106 | * 接收到数据
107 | *
108 | * 注意:此回调是在新线程中执行的
109 | *
110 | * @param transceiver SocketTransceiver对象
111 | * @param s 字符串
112 | */
113 | public abstract void onReceive(SocketTransceiver transceiver, String s);
114 |
115 | /**
116 | * 连接断开
117 | *
118 | * 注意:此回调是在新线程中执行的
119 | *
120 | * @param transceiver SocketTransceiver对象
121 | */
122 | public abstract void onDisconnect(SocketTransceiver transceiver);
123 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/activity/ChangeLanguageActivity.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.activity;
2 |
3 | import android.content.Intent;
4 | import android.content.SharedPreferences;
5 | import android.content.res.Configuration;
6 | import android.content.res.Resources;
7 | import android.os.Bundle;
8 | import android.support.annotation.Nullable;
9 | import android.util.DisplayMetrics;
10 | import android.view.View;
11 | import android.widget.ImageView;
12 | import android.widget.TextView;
13 |
14 | import com.lamps.lamps.R;
15 |
16 | import java.util.Locale;
17 |
18 | /**
19 | * Created by stew on 16/9/4.
20 | * mail: stewforani@gmail.com
21 | */
22 | public class ChangeLanguageActivity extends BaseActivity {
23 | @Override
24 | protected void onCreate(@Nullable Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_change_language);
27 | initView();
28 | }
29 |
30 | private void initView() {
31 | /**
32 | * 保存设置
33 | */
34 | SharedPreferences sharedPreferences = getSharedPreferences("set", MODE_PRIVATE);
35 | final SharedPreferences.Editor editor = sharedPreferences.edit();
36 |
37 | /**
38 | * 获取系统配置
39 | */
40 | final Resources resources = getResources();
41 | final DisplayMetrics dm = resources.getDisplayMetrics();
42 | final Configuration config = resources.getConfiguration();
43 |
44 | ImageView back = (ImageView) findViewById(R.id.setting_back);
45 | TextView CN = (TextView) findViewById(R.id.language_cn);
46 | TextView EN = (TextView) findViewById(R.id.language_en);
47 |
48 | if (back == null || CN == null || EN == null) {
49 | return;
50 | }
51 |
52 | back.setOnClickListener(new View.OnClickListener() {
53 | @Override
54 | public void onClick(View v) {
55 | finish();
56 | }
57 | });
58 |
59 |
60 | CN.setOnClickListener(new View.OnClickListener() {
61 | @Override
62 | public void onClick(View v) {
63 |
64 | config.locale = Locale.SIMPLIFIED_CHINESE;
65 | resources.updateConfiguration(config, dm);
66 |
67 | editor.putString("language","0");
68 | editor.apply();
69 |
70 | Intent intent = new Intent(ChangeLanguageActivity.this, MainActivity.class);
71 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
72 | startActivity(intent);
73 |
74 |
75 | }
76 | });
77 |
78 |
79 | EN.setOnClickListener(new View.OnClickListener() {
80 | @Override
81 | public void onClick(View v) {
82 |
83 |
84 | config.locale = Locale.ENGLISH;
85 | resources.updateConfiguration(config, dm);
86 |
87 | editor.putString("language","1");
88 | editor.apply();
89 |
90 | Intent intent = new Intent(ChangeLanguageActivity.this, MainActivity.class);
91 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
92 | startActivity(intent);
93 |
94 | }
95 | });
96 |
97 |
98 |
99 | }
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/fragment/LightFragment.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.SeekBar;
10 |
11 | import com.lamps.lamps.R;
12 | import com.lamps.lamps.activity.MainActivity;
13 | import com.lamps.lamps.util.CommandUtils;
14 |
15 | /**
16 | * Created by stew on 16/9/3.
17 | * mail: stewforani@gmail.com
18 | */
19 | public class LightFragment extends Fragment {
20 | private static final String TAG = "LightFragment";
21 | private View rootView;
22 | private String order = CommandUtils.LIGHT_1;
23 | private String speed = CommandUtils.SPEED_1;
24 |
25 | @Nullable
26 | @Override
27 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
28 | rootView = inflater.inflate(R.layout.fragment_catch_light, container, false);
29 | return rootView;
30 | }
31 |
32 | @Override
33 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
34 | super.onViewCreated(view, savedInstanceState);
35 | initView();
36 | }
37 |
38 | private void initView() {
39 |
40 | SeekBar seekBar = (SeekBar) rootView.findViewById(R.id.seekBar_catch_light);
41 | seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
42 | @Override
43 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
44 |
45 | }
46 |
47 | @Override
48 | public void onStartTrackingTouch(SeekBar seekBar) {
49 |
50 | }
51 |
52 | @Override
53 | public void onStopTrackingTouch(SeekBar seekBar) {
54 |
55 | int progress = seekBar.getProgress();
56 |
57 | if (progress < 50) {
58 | seekBar.setProgress(0);
59 | order = CommandUtils.LIGHT_1;
60 | } else if (progress < 100) {
61 | seekBar.setProgress(100);
62 | order = CommandUtils.LIGHT_2;
63 | }
64 | ((MainActivity) getActivity()).sendOrder(order);
65 | }
66 | });
67 |
68 |
69 | SeekBar seekBarSpeed = (SeekBar) rootView.findViewById(R.id.seekBar_speed);
70 | seekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
71 | @Override
72 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
73 |
74 | }
75 |
76 | @Override
77 | public void onStartTrackingTouch(SeekBar seekBar) {
78 |
79 | }
80 |
81 | @Override
82 | public void onStopTrackingTouch(SeekBar seekBar) {
83 |
84 | int progress = seekBar.getProgress();
85 | if (progress < 25) {
86 | seekBar.setProgress(0);
87 | speed = CommandUtils.SPEED_1;
88 | } else if (progress < 75) {
89 | seekBar.setProgress(50);
90 | speed = CommandUtils.SPEED_2;
91 | } else {
92 | seekBar.setProgress(100);
93 | speed = CommandUtils.SPEED_3;
94 | }
95 | ((MainActivity) getActivity()).sendSpeed(speed);
96 | }
97 | });
98 |
99 | }
100 |
101 | public String getOrder() {
102 | return order;
103 | }
104 |
105 |
106 | public String getSpeed() {
107 | return speed;
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_setting.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
13 |
14 |
21 |
22 |
31 |
32 |
33 |
34 |
35 |
40 |
41 |
42 |
51 |
52 |
58 |
59 |
68 |
69 |
75 |
76 |
85 |
86 |
87 |
88 |
89 |
90 |
100 |
101 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/fragment/GradientFragment.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.SeekBar;
10 |
11 | import com.lamps.lamps.R;
12 | import com.lamps.lamps.activity.MainActivity;
13 | import com.lamps.lamps.util.CommandUtils;
14 | import com.lamps.lamps.util.ControlLight;
15 |
16 | /**
17 | * Created by stew on 16/9/3.
18 | * mail: stewforani@gmail.com
19 | */
20 | public class GradientFragment extends Fragment {
21 | private View rootView;
22 | private String order = CommandUtils.GRAD_1;
23 | private String speed = CommandUtils.SPEED_1;
24 | @Nullable
25 | @Override
26 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
27 | rootView = inflater.inflate(R.layout.fragment_gradient, container, false);
28 | return rootView;
29 | }
30 |
31 | @Override
32 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
33 | super.onViewCreated(view, savedInstanceState);
34 | initView();
35 | }
36 |
37 | private void initView() {
38 |
39 | SeekBar seekBar = (SeekBar) rootView.findViewById(R.id.seekBar_gradient);
40 | seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
41 | @Override
42 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
43 |
44 | }
45 |
46 | @Override
47 | public void onStartTrackingTouch(SeekBar seekBar) {
48 |
49 | }
50 |
51 | @Override
52 | public void onStopTrackingTouch(SeekBar seekBar) {
53 |
54 | int progress = seekBar.getProgress();
55 | if (progress < 25) {
56 | seekBar.setProgress(0);
57 | order=CommandUtils.GRAD_1;
58 | } else if (progress < 75) {
59 | seekBar.setProgress(50);
60 | order=CommandUtils.GRAD_2;
61 | } else {
62 | seekBar.setProgress(100);
63 | order=CommandUtils.GRAD_3;
64 | }
65 |
66 | ((MainActivity)getActivity()).sendOrder(order);
67 |
68 | }
69 | });
70 |
71 |
72 | SeekBar seekBarSpeed = (SeekBar) rootView.findViewById(R.id.seekBar_speed);
73 | seekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
74 | @Override
75 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
76 |
77 | }
78 |
79 | @Override
80 | public void onStartTrackingTouch(SeekBar seekBar) {
81 |
82 | }
83 |
84 | @Override
85 | public void onStopTrackingTouch(SeekBar seekBar) {
86 |
87 | int progress = seekBar.getProgress();
88 | if (progress < 25) {
89 | seekBar.setProgress(0);
90 | speed = CommandUtils.SPEED_1;
91 | } else if (progress < 75) {
92 | seekBar.setProgress(50);
93 | speed = CommandUtils.SPEED_2;
94 | } else {
95 | seekBar.setProgress(100);
96 | speed = CommandUtils.SPEED_3;
97 | }
98 | ((MainActivity) getActivity()).sendSpeed(speed);
99 | }
100 | });
101 |
102 | }
103 |
104 | public String getOrder() {
105 | return order;
106 | }
107 |
108 |
109 | public String getSpeed() {
110 | return speed;
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/fragment/TwinkleFragment.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.util.Log;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.SeekBar;
11 |
12 | import com.lamps.lamps.R;
13 | import com.lamps.lamps.activity.MainActivity;
14 | import com.lamps.lamps.util.CommandUtils;
15 | import com.lamps.lamps.util.ControlLight;
16 |
17 | /**
18 | * Created by stew on 16/9/3.
19 | * mail: stewforani@gmail.com
20 | */
21 | public class TwinkleFragment extends Fragment {
22 | private View rootView;
23 | private String order = CommandUtils.TWIN_1;
24 | private String speed = CommandUtils.SPEED_1;
25 |
26 | @Nullable
27 | @Override
28 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
29 | rootView = inflater.inflate(R.layout.fragment_twinkle, container, false);
30 | return rootView;
31 | }
32 |
33 | @Override
34 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
35 | super.onViewCreated(view, savedInstanceState);
36 | initView();
37 | }
38 |
39 | private void initView() {
40 | SeekBar seekBar = (SeekBar) rootView.findViewById(R.id.seekBar_twinkle);
41 | seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
42 | @Override
43 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
44 |
45 | }
46 |
47 | @Override
48 | public void onStartTrackingTouch(SeekBar seekBar) {
49 |
50 | }
51 |
52 | @Override
53 | public void onStopTrackingTouch(SeekBar seekBar) {
54 |
55 |
56 | int progress = seekBar.getProgress();
57 | if (progress < 25) {
58 | seekBar.setProgress(0);
59 | order = CommandUtils.TWIN_1;
60 | } else if (progress < 75) {
61 | seekBar.setProgress(50);
62 | order = CommandUtils.TWIN_2;
63 | } else {
64 | seekBar.setProgress(100);
65 | order = CommandUtils.TWIN_3;
66 | }
67 |
68 | ((MainActivity) getActivity()).sendOrder(order);
69 |
70 | }
71 | });
72 |
73 |
74 | SeekBar seekBarSpeed = (SeekBar) rootView.findViewById(R.id.seekBar_speed);
75 | seekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
76 | @Override
77 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
78 |
79 | }
80 |
81 | @Override
82 | public void onStartTrackingTouch(SeekBar seekBar) {
83 |
84 | }
85 |
86 | @Override
87 | public void onStopTrackingTouch(SeekBar seekBar) {
88 |
89 | int progress = seekBar.getProgress();
90 | if (progress < 25) {
91 | seekBar.setProgress(0);
92 | speed = CommandUtils.SPEED_1;
93 | } else if (progress < 75) {
94 | seekBar.setProgress(50);
95 | speed = CommandUtils.SPEED_2;
96 | } else {
97 | seekBar.setProgress(100);
98 | speed = CommandUtils.SPEED_3;
99 | }
100 |
101 | ((MainActivity) getActivity()).sendSpeed(speed);
102 |
103 | }
104 | });
105 | }
106 |
107 |
108 | public String getOrder() {
109 | return order;
110 | }
111 |
112 | public String getSpeed() {
113 | return speed;
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/util/SocketTransceiver.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.util;
2 |
3 | /**
4 | * Created by stew on 16/8/29.
5 | * mail: stewforani@gmail.com
6 | * Socket收发器 通过Socket发送数据,并使用新线程监听Socket接收到的数据
7 | */
8 |
9 | import java.io.DataInputStream;
10 | import java.io.DataOutputStream;
11 | import java.io.IOException;
12 | import java.net.InetAddress;
13 | import java.net.Socket;
14 |
15 |
16 | public abstract class SocketTransceiver implements Runnable {
17 |
18 | protected Socket socket;
19 | protected InetAddress addr;
20 | protected DataInputStream in;
21 | protected DataOutputStream out;
22 | private boolean runFlag;
23 |
24 | /**
25 | * 实例化
26 | *
27 | * @param socket 已经建立连接的socket
28 | */
29 | public SocketTransceiver(Socket socket) {
30 | this.socket = socket;
31 | this.addr = socket.getInetAddress();
32 | }
33 |
34 | /**
35 | * 获取连接到的Socket地址
36 | *
37 | * @return InetAddress对象
38 | */
39 | public InetAddress getInetAddress() {
40 | return addr;
41 | }
42 |
43 | /**
44 | * 开启Socket收发
45 | *
46 | * 如果开启失败,会断开连接并回调{@code onDisconnect()}
47 | */
48 | public void start() {
49 | runFlag = true;
50 | new Thread(this).start();
51 | }
52 |
53 | /**
54 | * 断开连接(主动)
55 | *
56 | * 连接断开后,会回调{@code onDisconnect()}
57 | */
58 | public void stop() {
59 | runFlag = false;
60 | try {
61 | socket.shutdownInput();
62 | in.close();
63 | } catch (Exception e) {
64 | e.printStackTrace();
65 | }
66 | }
67 |
68 | /**
69 | * 发送字符串
70 | *
71 | * @param s 字符串
72 | * @return 发送成功返回true
73 | */
74 | public boolean send(byte s) {
75 | if (out != null) {
76 | try {
77 | out.write(s);
78 | out.flush();
79 | return true;
80 | } catch (Exception e) {
81 | e.printStackTrace();
82 | }
83 | }
84 | return false;
85 | }
86 |
87 | /**修改ssid
88 | * @param s
89 | * @return
90 | */
91 | public boolean send(byte[] s) {
92 | if (out != null) {
93 | try {
94 | out.write(s);
95 | out.flush();
96 | return true;
97 | } catch (Exception e) {
98 | e.printStackTrace();
99 | }
100 | }
101 | return false;
102 | }
103 |
104 | /**
105 | * 监听Socket接收的数据(新线程中运行)
106 | */
107 | @Override
108 | public void run() {
109 | try {
110 | in = new DataInputStream(this.socket.getInputStream());
111 | out = new DataOutputStream(this.socket.getOutputStream());
112 | } catch (IOException e) {
113 | e.printStackTrace();
114 | runFlag = false;
115 | }
116 | while (runFlag) {
117 | try {
118 | final String s = in.readUTF();
119 | this.onReceive(addr, s);
120 | } catch (IOException e) {
121 | // 连接被断开(被动)
122 | runFlag = false;
123 | }
124 | }
125 | // 断开连接
126 | try {
127 | in.close();
128 | out.close();
129 | socket.close();
130 | in = null;
131 | out = null;
132 | socket = null;
133 | } catch (IOException e) {
134 | e.printStackTrace();
135 | }
136 | this.onDisconnect(addr);
137 | }
138 |
139 | /**
140 | * 接收到数据
141 | *
142 | * 注意:此回调是在新线程中执行的
143 | *
144 | * @param addr 连接到的Socket地址
145 | * @param s 收到的字符串
146 | */
147 | public abstract void onReceive(InetAddress addr, String s);
148 |
149 | /**
150 | * 连接断开
151 | *
152 | * 注意:此回调是在新线程中执行的
153 | *
154 | * @param addr 连接到的Socket地址
155 | */
156 | public abstract void onDisconnect(InetAddress addr);
157 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/util/ControlLight.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.util;
2 |
3 | import android.util.Log;
4 |
5 | /**
6 | * Created by stew on 16/9/4.
7 | * mail: stewforani@gmail.com
8 | */
9 |
10 | public class ControlLight {
11 |
12 |
13 | //ip and port set by yourself
14 |
15 | private static final String IP = "";
16 | private static final int PORT = 0;
17 | private static TcpClient tcpClient = null;
18 | private static ConnectStateListener listener;
19 |
20 | public static TcpClient newInstance() {
21 | if (null == tcpClient) {
22 | tcpClient = new TcpClient() {
23 |
24 | @Override
25 | public void onConnect(SocketTransceiver transceiver) {
26 | listener.connect();
27 | }
28 |
29 | @Override
30 | public void onConnectFailed() {
31 | listener.connectFailed();
32 | }
33 |
34 | @Override
35 | public void onReceive(SocketTransceiver transceiver, String s) {
36 |
37 | }
38 |
39 | @Override
40 | public void onDisconnect(SocketTransceiver transceiver) {
41 |
42 | }
43 | };
44 | }
45 |
46 | return tcpClient;
47 | }
48 |
49 |
50 | public static void connectLight() {
51 | try {
52 | ControlLight.newInstance().connect(IP, PORT);
53 | } catch (NumberFormatException e) {
54 | e.printStackTrace();
55 | }
56 | }
57 |
58 |
59 | public interface ConnectStateListener {
60 | void connect();
61 |
62 | void connectFailed();
63 | }
64 |
65 | public static void setOnConnectStateListener(ConnectStateListener listener) {
66 | ControlLight.listener = listener;
67 | }
68 |
69 | /**
70 | * 发送指令(修改灯的效果)
71 | */
72 |
73 | public static void sendOrder(String order) {
74 |
75 | Log.d("MainActivity", order + "");
76 |
77 | try {
78 | byte b = (byte) Integer.parseInt(("0x" + order).substring(2), 16);
79 | ControlLight.newInstance().getTransceiver().send(b);
80 | } catch (Exception e) {
81 | e.printStackTrace();
82 | }
83 | }
84 |
85 | /**
86 | * 发送指令(修改SSID)
87 | */
88 | public static void sendSSID(String ssid) {
89 | try {
90 |
91 | byte b = (byte) Integer.parseInt(("0xE1").substring(2), 16);
92 | int count = ssid.getBytes().length;
93 | byte[] c = ssid.getBytes();
94 | byte[] a = new byte[count + 1];
95 | a[0] = b;
96 | for (int i = 0; i < count; i++) {
97 | a[i + 1] = c[i];
98 | }
99 |
100 | ControlLight.newInstance().getTransceiver().send(a);
101 | } catch (Exception e) {
102 | e.printStackTrace();
103 | }
104 | }
105 |
106 |
107 | /**
108 | * 发送指令(修改密码)
109 | */
110 | public static void sendPassword(String ssid) {
111 | try {
112 |
113 | byte b = (byte) Integer.parseInt(("0xE2").substring(2), 16);
114 | int count = ssid.getBytes().length;
115 | byte[] c = ssid.getBytes();
116 | byte[] a = new byte[count + 1];
117 | a[0] = b;
118 | for (int i = 0; i < count; i++) {
119 | a[i + 1] = c[i];
120 | }
121 |
122 | ControlLight.newInstance().getTransceiver().send(a);
123 | } catch (Exception e) {
124 | e.printStackTrace();
125 | }
126 | }
127 |
128 |
129 | /**
130 | * 发送指令(定时)
131 | */
132 | public static void sendTime(String header, String time1, String time2) {
133 | try {
134 | byte a = (byte) Integer.parseInt(header, 16);
135 | byte b = (byte) Integer.parseInt(time1, 16);
136 | byte c = (byte) Integer.parseInt(time2, 16);
137 | byte[] d = {a, b, c};
138 | ControlLight.newInstance().getTransceiver().send(d);
139 | } catch (Exception e) {
140 | e.printStackTrace();
141 | }
142 | }
143 |
144 | }
145 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_timing.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
11 |
12 |
19 |
20 |
29 |
30 |
42 |
43 |
44 |
45 |
46 |
51 |
52 |
61 |
62 |
71 |
72 |
73 |
74 |
75 |
82 |
83 |
90 |
91 |
104 |
105 |
118 |
119 |
120 |
121 |
122 |
129 |
130 |
137 |
138 |
151 |
152 |
165 |
166 |
167 |
168 |
169 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/activity/WifiConnectActivity.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.activity;
2 |
3 | import android.Manifest;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.SharedPreferences;
7 | import android.content.pm.PackageManager;
8 | import android.content.res.Configuration;
9 | import android.content.res.Resources;
10 | import android.net.wifi.ScanResult;
11 | import android.net.wifi.WifiInfo;
12 | import android.net.wifi.WifiManager;
13 | import android.os.Build;
14 | import android.os.Bundle;
15 | import android.provider.Settings;
16 | import android.support.annotation.NonNull;
17 | import android.support.annotation.Nullable;
18 | import android.util.DisplayMetrics;
19 | import android.util.Log;
20 | import android.view.LayoutInflater;
21 | import android.view.View;
22 | import android.view.ViewGroup;
23 | import android.widget.AdapterView;
24 | import android.widget.BaseAdapter;
25 | import android.widget.Button;
26 | import android.widget.ImageView;
27 | import android.widget.LinearLayout;
28 | import android.widget.ListView;
29 | import android.widget.TextView;
30 | import android.widget.Toast;
31 |
32 | import com.lamps.lamps.R;
33 | import com.lamps.lamps.util.ControlLight;
34 |
35 | import java.util.List;
36 | import java.util.Locale;
37 |
38 | /**
39 | * Created by stew on 16/8/30.
40 | * mail: stewforani@gmail.com
41 | */
42 | public class WifiConnectActivity extends BaseActivity {
43 | public static final String TAG = "WifiConnectActivity";
44 | public static final String LANGUAGE_CN = "0";
45 | public static final String LANGUAGE_EN = "1";
46 | private static final int PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION = 0;
47 | private ImageView wifiConnectIcon;
48 | private LinearLayout wifiStateLL;
49 | private TextView wifiName;
50 | private Button enter;
51 |
52 | @Override
53 | protected void onCreate(@Nullable Bundle savedInstanceState) {
54 | super.onCreate(savedInstanceState);
55 | Log.d(TAG, "onCreate: ");
56 |
57 | setContentView(R.layout.activity_wifi_state);
58 | checkLanguage();
59 | initView();
60 |
61 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
62 | requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION);
63 | //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
64 |
65 | } else {
66 | initWifi();
67 | //do something, permission was previously granted; or legacy device
68 | }
69 |
70 | }
71 |
72 | private void initWifi() {
73 | WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
74 | if (!wifiManager.isWifiEnabled()) {
75 | Toast.makeText(this, "wifi未打开!", Toast.LENGTH_SHORT).show();
76 | return;
77 | }
78 | List list = wifiManager.getScanResults();
79 | ListView listView = (ListView) findViewById(R.id.wifi_list);
80 |
81 | if (listView == null) {
82 | return;
83 | }
84 |
85 | if (list.size() == 0) {
86 | Toast.makeText(this, "未搜索到wifi信号!", Toast.LENGTH_SHORT).show();
87 |
88 | } else {
89 | listView.setAdapter(new MyAdapter(this, list));
90 | }
91 |
92 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
93 | @Override
94 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
95 |
96 | Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
97 | startActivity(intent);
98 |
99 | }
100 | });
101 |
102 | }
103 |
104 |
105 | @Override
106 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
107 | if (requestCode == PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION
108 | && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
109 | // Do something with granted permission
110 | initWifi();
111 | }
112 | }
113 |
114 | private void checkLanguage() {
115 |
116 | SharedPreferences sharedPreferences = this.getSharedPreferences("set", MODE_PRIVATE);
117 | /**
118 | * 获取系统配置
119 | */
120 | Resources resources = getResources();
121 | DisplayMetrics dm = resources.getDisplayMetrics();
122 | Configuration config = resources.getConfiguration();
123 |
124 |
125 | if (LANGUAGE_CN.equals(sharedPreferences.getString("language", "0"))) {
126 |
127 | config.locale = Locale.SIMPLIFIED_CHINESE;
128 | resources.updateConfiguration(config, dm);
129 | } else if (LANGUAGE_EN.equals(sharedPreferences.getString("language", "0"))) {
130 |
131 | config.locale = Locale.ENGLISH;
132 | resources.updateConfiguration(config, dm);
133 | } else {
134 |
135 | config.locale = Locale.SIMPLIFIED_CHINESE;
136 | resources.updateConfiguration(config, dm);
137 | }
138 |
139 | }
140 |
141 | private void initView() {
142 | wifiConnectIcon = (ImageView) findViewById(R.id.wifi_connect_icon);
143 | wifiStateLL = (LinearLayout) findViewById(R.id.wifi_state_ll);
144 | wifiName = (TextView) findViewById(R.id.wifi_name);
145 | enter = (Button) findViewById(R.id.button_enter);
146 |
147 | if (enter == null) {
148 | return;
149 | }
150 |
151 | enter.setOnClickListener(new View.OnClickListener() {
152 | @Override
153 | public void onClick(View v) {
154 | Intent intent = new Intent(WifiConnectActivity.this, MainActivity.class);
155 | startActivity(intent);
156 | }
157 | });
158 | }
159 |
160 | @Override
161 | protected void onResume() {
162 | super.onResume();
163 |
164 | Log.d(TAG, "onResume: ");
165 | /**
166 | * 在onPause()中判断wifi连接状态
167 | *暂时不做wifi状态监听(wifi确认链接后返回页面,或者重启app,才会生效)
168 | */
169 |
170 | ControlLight.setOnConnectStateListener(new ControlLight.ConnectStateListener() {
171 | @Override
172 | public void connect() {
173 | Log.d(TAG, "connect: ");
174 |
175 | runOnUiThread(new Runnable() {
176 | @Override
177 | public void run() {
178 | connectY();
179 | }
180 | });
181 |
182 | }
183 |
184 | @Override
185 | public void connectFailed() {
186 | Log.d(TAG, "connectFailed: ");
187 |
188 | runOnUiThread(new Runnable() {
189 | @Override
190 | public void run() {
191 | connectN();
192 | }
193 | });
194 |
195 | }
196 | });
197 |
198 |
199 | ControlLight.connectLight();
200 | }
201 |
202 |
203 | public void connectY() {
204 | /**
205 | * 连接成功
206 | */
207 |
208 | WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
209 | WifiInfo wifiInfo = wifiManager.getConnectionInfo();
210 | wifiConnectIcon.setImageResource(R.drawable.wifi_connect_y);
211 | wifiStateLL.setVisibility(View.INVISIBLE);
212 | wifiName.setVisibility(View.VISIBLE);
213 | enter.setVisibility(View.VISIBLE);
214 | wifiName.setText(wifiInfo.getSSID());
215 | }
216 |
217 | public void connectN() {
218 | /**
219 | *连接失败
220 | */
221 |
222 | wifiConnectIcon.setImageResource(R.drawable.wifi_connect_n);
223 | wifiStateLL.setVisibility(View.VISIBLE);
224 | wifiName.setVisibility(View.INVISIBLE);
225 | enter.setVisibility(View.VISIBLE);
226 | }
227 |
228 | private class MyAdapter extends BaseAdapter {
229 |
230 | LayoutInflater inflater;
231 | List list;
232 |
233 | public MyAdapter(Context context, List list) {
234 | this.inflater = LayoutInflater.from(context);
235 | this.list = list;
236 | }
237 |
238 | @Override
239 | public int getCount() {
240 | return list.size();
241 | }
242 |
243 | @Override
244 | public Object getItem(int position) {
245 | return position;
246 | }
247 |
248 | @Override
249 | public long getItemId(int position) {
250 | return position;
251 | }
252 |
253 | @Override
254 | public View getView(int position, View convertView, ViewGroup parent) {
255 | ViewHolder holder = null;
256 |
257 | if (convertView == null) {
258 | holder = new ViewHolder();
259 | convertView = inflater.inflate(R.layout.wifi_list, null);
260 | holder.wifiName = (TextView) convertView.findViewById(R.id.wifi_name);
261 | convertView.setTag(holder);
262 |
263 | } else {
264 | holder = (ViewHolder) convertView.getTag();
265 | }
266 |
267 | ScanResult scanResult = list.get(position);
268 | holder.wifiName.setText(scanResult.SSID);
269 | return convertView;
270 | }
271 |
272 |
273 | public final class ViewHolder {
274 | public TextView wifiName;
275 | }
276 |
277 | }
278 |
279 |
280 | @Override
281 | protected void onDestroy() {
282 | Log.d(TAG, "onDestroy: ");
283 | ControlLight.setOnConnectStateListener(null);
284 | super.onDestroy();
285 | }
286 | }
287 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/activity/TimingActivity.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.activity;
2 |
3 | import android.app.TimePickerDialog;
4 | import android.content.DialogInterface;
5 | import android.content.SharedPreferences;
6 | import android.os.Bundle;
7 | import android.support.annotation.Nullable;
8 | import android.support.v7.app.AlertDialog;
9 | import android.view.View;
10 | import android.widget.CompoundButton;
11 | import android.widget.ImageView;
12 | import android.widget.LinearLayout;
13 | import android.widget.Switch;
14 | import android.widget.TextView;
15 | import android.widget.TimePicker;
16 |
17 | import com.lamps.lamps.R;
18 | import com.lamps.lamps.util.CommandUtils;
19 | import com.lamps.lamps.util.ControlLight;
20 |
21 | import java.util.Calendar;
22 |
23 |
24 | /**
25 | * Created by stew on 16/9/6.
26 | * mail: stewforani@gmail.com
27 | */
28 | public class TimingActivity extends BaseActivity implements View.OnClickListener {
29 | private TextView startAPM;
30 | private TextView endAPM;
31 | private TextView startTimeTv;
32 | private TextView endTimeTv;
33 | private Boolean switchOpen = false;
34 | private String[] time = {"0", "0", "0", "0"};
35 | private SharedPreferences.Editor editor;
36 |
37 | @Override
38 | protected void onCreate(@Nullable Bundle savedInstanceState) {
39 | super.onCreate(savedInstanceState);
40 | setContentView(R.layout.activity_timing);
41 | initView();
42 | }
43 |
44 | private void initView() {
45 |
46 | /**
47 | * 保存设置
48 | */
49 | SharedPreferences sharedPreferences = getSharedPreferences("timing", MODE_PRIVATE);
50 | editor = sharedPreferences.edit();
51 |
52 | ImageView back = (ImageView) findViewById(R.id.setting_back);
53 | TextView save = (TextView) findViewById(R.id.save);
54 | Switch aSwitch = (Switch) findViewById(R.id.switch_timing);
55 | startAPM = (TextView) findViewById(R.id.start_apm);
56 | endAPM = (TextView) findViewById(R.id.end_apm);
57 | startTimeTv = (TextView) findViewById(R.id.start_time_tv);
58 | endTimeTv = (TextView) findViewById(R.id.end_time_tv);
59 | LinearLayout startTime = (LinearLayout) findViewById(R.id.start_time);
60 | LinearLayout endTime = (LinearLayout) findViewById(R.id.end_time);
61 |
62 | if (back == null || save == null || startTime == null || endTime == null || aSwitch == null) {
63 | return;
64 | }
65 |
66 | back.setOnClickListener(this);
67 | save.setOnClickListener(this);
68 | startTime.setOnClickListener(this);
69 | endTime.setOnClickListener(this);
70 |
71 | if (sharedPreferences.getBoolean("checkState", false)) {
72 | aSwitch.setChecked(true);
73 | switchOpen = true;
74 | }
75 |
76 | aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
77 | @Override
78 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
79 | switchOpen = isChecked;
80 | }
81 | });
82 |
83 |
84 | setStartTimeTv(Integer.parseInt(sharedPreferences.getString("startH", "00")), Integer.parseInt(sharedPreferences.getString("startM", "00")));
85 | setEndTimeTv(Integer.parseInt(sharedPreferences.getString("endH", "00")), Integer.parseInt(sharedPreferences.getString("endM", "00")));
86 | }
87 |
88 | @Override
89 | public void onClick(View v) {
90 | switch (v.getId()) {
91 | case R.id.setting_back: {
92 |
93 | AlertDialog.Builder builder = new AlertDialog.Builder(this);
94 | builder.setTitle(getResources().getString(R.string.remind));
95 | builder.setMessage(getResources().getString(R.string.remind_save));
96 | builder.setPositiveButton(getString(R.string.sure), new DialogInterface.OnClickListener() {
97 | @Override
98 | public void onClick(DialogInterface dialog, int which) {
99 | finish();
100 | }
101 | });
102 |
103 | builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
104 | @Override
105 | public void onClick(DialogInterface dialog, int which) {
106 |
107 | }
108 | });
109 |
110 | builder.show();
111 |
112 | }
113 | break;
114 |
115 | case R.id.save: {
116 | sendTime();
117 | }
118 | break;
119 |
120 | case R.id.start_time: {
121 | showStartTimePickerDialog();
122 | }
123 | break;
124 |
125 | case R.id.end_time: {
126 | showEndTimePickerDialog();
127 | }
128 | break;
129 | }
130 |
131 | }
132 |
133 | private void sendTime() {
134 | if (switchOpen) {
135 |
136 | final AlertDialog dialog = new AlertDialog.Builder(this).create();
137 | dialog.setTitle(getResources().getString(R.string.remind));
138 | dialog.setMessage(getResources().getString(R.string.wait));
139 | dialog.show();
140 |
141 | Calendar c = Calendar.getInstance();
142 | int hour = c.get(Calendar.HOUR_OF_DAY);
143 | int min = c.get(Calendar.MINUTE);
144 |
145 | String hourStr = hour + "";
146 | String minStr = min + "";
147 |
148 | if (hour < 10) {
149 | hourStr = "0" + hour + "";
150 | }
151 |
152 | if (min < 10) {
153 | minStr = "0" + min + "";
154 | }
155 |
156 | ControlLight.sendTime(CommandUtils.TIMING_CURRENT, hourStr, minStr);
157 | startAPM.postDelayed(new Runnable() {
158 | @Override
159 | public void run() {
160 | ControlLight.sendTime(CommandUtils.TIMING_ON_1, time[0], time[1]);
161 |
162 | startAPM.postDelayed(new Runnable() {
163 | @Override
164 | public void run() {
165 | ControlLight.sendTime(CommandUtils.TIMING_ON_2, time[2], time[3]);
166 | dialog.cancel();
167 | finishThis();
168 | }
169 | }, 1000);
170 |
171 | }
172 | }, 1000);
173 |
174 |
175 | } else {
176 | ControlLight.sendOrder(CommandUtils.TIMING_OFF);
177 |
178 | finishThis();
179 | }
180 |
181 | }
182 |
183 | private void finishThis() {
184 | editor.putString("startH", time[0]);
185 | editor.putString("startM", time[1]);
186 | editor.putString("endH", time[2]);
187 | editor.putString("endM", time[3]);
188 | editor.putBoolean("checkState", switchOpen);
189 | editor.apply();
190 | finish();
191 | }
192 |
193 | private void showStartTimePickerDialog() {
194 | Calendar calendar = Calendar.getInstance();
195 | new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
196 | @Override
197 | public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
198 | setStartTimeTv(hourOfDay, minute);
199 | }
200 | }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();
201 | }
202 |
203 |
204 | private void showEndTimePickerDialog() {
205 | Calendar calendar = Calendar.getInstance();
206 | new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
207 | @Override
208 | public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
209 |
210 |
211 | setEndTimeTv(hourOfDay, minute);
212 | }
213 | }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();
214 |
215 | }
216 |
217 | private void setStartTimeTv(int hourOfDay, int minute) {
218 |
219 | if (hourOfDay >= 12) {
220 | startAPM.setText("PM");
221 | } else {
222 | startAPM.setText("AM");
223 | }
224 |
225 | String newH = null;
226 | String newM = null;
227 | if (hourOfDay < 10) {
228 | newH = "0" + hourOfDay;
229 | } else {
230 | newH = hourOfDay + "";
231 | }
232 | if (minute < 10) {
233 | newM = "0" + minute;
234 | } else {
235 | newM = minute + "";
236 | }
237 |
238 | String tvH = null;
239 | if (hourOfDay > 12) {
240 | tvH = (hourOfDay - 12) + "";
241 | if ((hourOfDay - 12) < 10) {
242 | tvH = "0" + (hourOfDay - 12) + "";
243 | }
244 |
245 | } else {
246 | tvH = (hourOfDay) + "";
247 | if (hourOfDay < 10) {
248 | tvH = "0" + (hourOfDay) + "";
249 | }
250 | }
251 |
252 | startTimeTv.setText(tvH + ":" + newM);
253 | time[0] = newH;
254 | time[1] = newM;
255 | }
256 |
257 |
258 | private void setEndTimeTv(int hourOfDay, int minute) {
259 | if (hourOfDay >= 12) {
260 | endAPM.setText("PM");
261 | } else {
262 | endAPM.setText("AM");
263 | }
264 |
265 | String newH = null;
266 | String newM = null;
267 | if (hourOfDay < 10) {
268 | newH = "0" + hourOfDay;
269 | } else {
270 | newH = hourOfDay + "";
271 | }
272 | if (minute < 10) {
273 | newM = "0" + minute;
274 | } else {
275 | newM = minute + "";
276 | }
277 |
278 | String tvH = null;
279 | if (hourOfDay > 12) {
280 | tvH = (hourOfDay - 12) + "";
281 | if ((hourOfDay - 12) < 10) {
282 | tvH = "0" + (hourOfDay - 12) + "";
283 | }
284 |
285 | } else {
286 | tvH = (hourOfDay) + "";
287 | if (hourOfDay < 10) {
288 | tvH = "0" + (hourOfDay) + "";
289 | }
290 | }
291 |
292 | endTimeTv.setText(tvH + ":" + newM);
293 | time[2] = newH;
294 | time[3] = newM;
295 | }
296 |
297 |
298 | }
299 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lamps/lamps/activity/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.lamps.lamps.activity;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.support.v4.app.Fragment;
6 | import android.support.v4.view.ViewPager;
7 | import android.os.Bundle;
8 | import android.util.Log;
9 | import android.view.View;
10 | import android.widget.ImageView;
11 |
12 | import com.lamps.lamps.adapter.MyPagerAdapter;
13 | import com.lamps.lamps.R;
14 | import com.lamps.lamps.fragment.BrightFragment;
15 | import com.lamps.lamps.fragment.LightFragment;
16 | import com.lamps.lamps.fragment.ColorFragment;
17 | import com.lamps.lamps.fragment.GradientFragment;
18 | import com.lamps.lamps.fragment.TwinkleFragment;
19 | import com.lamps.lamps.util.CommandUtils;
20 | import com.lamps.lamps.util.ControlLight;
21 | import com.lamps.lamps.util.CustomViewpager;
22 |
23 | import net.lucode.hackware.magicindicator.MagicIndicator;
24 | import net.lucode.hackware.magicindicator.SimpleViewPagerDelegate;
25 | import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
26 | import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
27 | import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
28 | import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
29 | import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
30 | import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
31 |
32 | import java.util.ArrayList;
33 | import java.util.List;
34 |
35 | public class MainActivity extends BaseActivity implements View.OnClickListener {
36 | private ImageView lightOff;
37 | private ImageView lightOn;
38 | private String currentOrder = CommandUtils.BRIGHT_1;
39 | private String currentSpeed = CommandUtils.SPEED_1;
40 | private boolean shouldSendSpeed = true;
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | setContentView(R.layout.activity_main);
46 | initTab();
47 | initView();
48 | }
49 |
50 | private void initView() {
51 | lightOff = (ImageView) findViewById(R.id.light_off);
52 | lightOn = (ImageView) findViewById(R.id.light_on);
53 | ImageView setImg = (ImageView) findViewById(R.id.main_setting);
54 | ImageView backWifiList = (ImageView) findViewById(R.id.back_wifi_list);
55 | ImageView setTimeImg = (ImageView) findViewById(R.id.main_set_time);
56 |
57 | if (lightOff != null && lightOn != null && setImg != null && setTimeImg != null && backWifiList != null) {
58 | lightOff.setOnClickListener(this);
59 | lightOn.setOnClickListener(this);
60 | setImg.setOnClickListener(this);
61 | backWifiList.setOnClickListener(this);
62 | setTimeImg.setOnClickListener(this);
63 | }
64 |
65 | }
66 |
67 | private void initTab() {
68 | final List tabNames = new ArrayList<>();
69 | final List fragments = new ArrayList<>();
70 |
71 | tabNames.add(getString(R.string.tab_bright));
72 | tabNames.add(getString(R.string.tab_twinkle));
73 | tabNames.add(getString(R.string.tab_gradient));
74 | tabNames.add(getString(R.string.tab_color));
75 | tabNames.add(getString(R.string.tab_light));
76 |
77 | fragments.add(new BrightFragment());
78 | fragments.add(new TwinkleFragment());
79 | fragments.add(new GradientFragment());
80 | fragments.add(new ColorFragment());
81 | fragments.add(new LightFragment());
82 |
83 | final CustomViewpager viewPager = (CustomViewpager) findViewById(R.id.viewpager);
84 |
85 | MagicIndicator magicIndicator = (MagicIndicator) findViewById(R.id.indicator);
86 |
87 | if (viewPager == null || magicIndicator == null) {
88 | return;
89 | }
90 |
91 | /**
92 | * 禁止viewpager 滑动
93 | */
94 | viewPager.setPagingEnabled(false);
95 |
96 |
97 | CommonNavigator commonNavigator = new CommonNavigator(this);
98 | commonNavigator.setAdapter(new CommonNavigatorAdapter() {
99 | @Override
100 | public int getCount() {
101 | return tabNames.size();
102 | }
103 |
104 | @Override
105 | public IPagerTitleView getTitleView(Context context, final int index) {
106 | ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
107 | colorTransitionPagerTitleView.setNormalColor(context.getResources().getColor(R.color.tabUnSelected));
108 | colorTransitionPagerTitleView.setSelectedColor(context.getResources().getColor(R.color.white));
109 | colorTransitionPagerTitleView.setText(tabNames.get(index));
110 | colorTransitionPagerTitleView.setTextSize(18);
111 | colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
112 | @Override
113 | public void onClick(View view) {
114 |
115 | // if (index == 3 || index == 4) {
116 | // return;
117 | // }
118 |
119 | viewPager.setCurrentItem(index);
120 | }
121 | });
122 | return colorTransitionPagerTitleView;
123 | }
124 |
125 | @Override
126 | public IPagerIndicator getIndicator(Context context) {
127 | LinePagerIndicator indicator = new LinePagerIndicator(context);
128 | indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
129 | indicator.setLineWidth(context.getResources().getDimension(R.dimen.indicator_line_width));
130 | indicator.setLineHeight(context.getResources().getDimension(R.dimen.indicator_line_height));
131 | indicator.setColors(context.getResources().getColor(R.color.white));
132 | return indicator;
133 | }
134 | });
135 | commonNavigator.setAdjustMode(true);
136 | magicIndicator.setNavigator(commonNavigator);
137 | SimpleViewPagerDelegate.with(magicIndicator, viewPager).delegate();
138 |
139 | MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), tabNames, fragments);
140 | viewPager.setAdapter(pagerAdapter);
141 |
142 | viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
143 | @Override
144 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
145 |
146 | }
147 |
148 | @Override
149 | public void onPageSelected(int position) {
150 | if (fragments.get(position) instanceof BrightFragment) {
151 |
152 | shouldSendSpeed = true;
153 |
154 | currentOrder = ((BrightFragment) fragments.get(position)).getOrder();
155 | } else if (fragments.get(position) instanceof TwinkleFragment) {
156 |
157 | shouldSendSpeed = false;
158 |
159 | currentOrder = ((TwinkleFragment) fragments.get(position)).getOrder();
160 | currentSpeed = ((TwinkleFragment) fragments.get(position)).getSpeed();
161 | ControlLight.sendOrder(currentSpeed);
162 | } else if (fragments.get(position) instanceof GradientFragment) {
163 |
164 | shouldSendSpeed = false;
165 |
166 | currentOrder = ((GradientFragment) fragments.get(position)).getOrder();
167 | currentSpeed = ((GradientFragment) fragments.get(position)).getSpeed();
168 | ControlLight.sendOrder(currentSpeed);
169 | } else if (fragments.get(position) instanceof ColorFragment) {
170 |
171 | shouldSendSpeed = true;
172 |
173 | currentOrder = ((ColorFragment) fragments.get(position)).getOrder();
174 | } else if (fragments.get(position) instanceof LightFragment) {
175 |
176 | shouldSendSpeed = false;
177 |
178 | currentOrder = ((LightFragment) fragments.get(position)).getOrder();
179 | currentSpeed = ((LightFragment) fragments.get(position)).getSpeed();
180 | ControlLight.sendOrder(currentSpeed);
181 | }
182 |
183 |
184 | lightOff.postDelayed(new Runnable() {
185 | @Override
186 | public void run() {
187 | ControlLight.sendOrder(currentOrder);
188 | }
189 | }, 1000);
190 |
191 | }
192 |
193 | @Override
194 | public void onPageScrollStateChanged(int state) {
195 |
196 | }
197 | });
198 |
199 | }
200 |
201 | @Override
202 | public void onClick(View v) {
203 | switch (v.getId()) {
204 | case R.id.light_off:
205 | /**
206 | * 灯总开关:关闭
207 | */
208 | lightOff.setVisibility(View.INVISIBLE);
209 | lightOn.setVisibility(View.VISIBLE);
210 |
211 | ControlLight.sendOrder(CommandUtils.OFF);
212 |
213 | break;
214 | case R.id.light_on:
215 | /**
216 | * 灯总开关:开启
217 | */
218 | lightOff.setVisibility(View.VISIBLE);
219 | lightOn.setVisibility(View.INVISIBLE);
220 |
221 | /**
222 | * 先开启灯,再获取当前的模式
223 | */
224 |
225 | ControlLight.sendOrder(CommandUtils.ON);
226 | lightOff.postDelayed(new Runnable() {
227 | @Override
228 | public void run() {
229 |
230 | ControlLight.sendOrder(currentOrder);
231 |
232 | lightOff.postDelayed(new Runnable() {
233 | @Override
234 | public void run() {
235 | if (!shouldSendSpeed) {
236 | ControlLight.sendOrder(currentSpeed);
237 | }
238 | }
239 | }, 1000);
240 |
241 |
242 | }
243 | }, 1000);
244 |
245 | break;
246 | case R.id.main_setting: {
247 | /**
248 | * 跳转至设置界面
249 | */
250 | Intent intent = new Intent(MainActivity.this, SettingActivity.class);
251 | startActivity(intent);
252 | }
253 |
254 | break;
255 | case R.id.main_set_time: {
256 | /**
257 | * 跳转定时界面
258 | */
259 | Intent intent = new Intent(MainActivity.this, TimingActivity.class);
260 | startActivity(intent);
261 | }
262 |
263 | break;
264 |
265 | case R.id.back_wifi_list:
266 | /**
267 | * 跳转到wifi列表
268 | */
269 |
270 | finish();
271 |
272 | break;
273 | }
274 |
275 | }
276 |
277 | public void sendOrder(String order) {
278 | currentOrder = order;
279 | ControlLight.sendOrder(order);
280 | }
281 |
282 | public void sendSpeed(String speed) {
283 | currentSpeed = speed;
284 | ControlLight.sendOrder(speed);
285 | }
286 |
287 | }
288 |
--------------------------------------------------------------------------------