Repo created
Some checks are pending
Continuous Integration / ktlint (push) Waiting to run
Continuous Integration / detekt (push) Waiting to run
Continuous Integration / build (push) Waiting to run

This commit is contained in:
Fr4nz D13trich 2025-11-24 09:22:21 +01:00
parent 368e9c2100
commit 4fa38b691e
262 changed files with 17567 additions and 2 deletions

2
.gitattributes vendored Normal file
View file

@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto

2
.github/FUNDING.yml vendored Normal file
View file

@ -0,0 +1,2 @@
github: Domi04151309
custom: ['https://www.paypal.com/donate/?hosted_button_id=487FTCX52P9WA']

31
.github/ISSUE_TEMPLATE/bug-report.md vendored Normal file
View file

@ -0,0 +1,31 @@
---
name: Bug Report
about: Create a report to help improving the app
title: "[Bug Report] Your Title"
labels: bug
assignees: ''
---
**Description**
_A clear and concise description of what the bug is.
Please add steps to reproduce the behavior._
<details>
<summary><b>Screenshots</b></summary>
_Add screenshots here to describe the problem._
</details>
<details>
<summary><b>Logs</b></summary>
_Add a detailed stack trace / crash log here if applicable_
</details>
**Additional information**
- Android version: _System Settings > About > Android version (varies per device)_
- Home App version: _Settings > About > Version_
- Installation source: _Google Play / F-Drod / Own Build_

View file

@ -0,0 +1,18 @@
---
name: Feature Request
about: Suggest an idea for this project
title: "[Feature Request] Your Title"
labels: enhancement
assignees: ''
---
**Describe the solution you'd like**
_A clear and concise description of what you want to happen._
<details>
<summary><b>Additional context</b></summary>
_Add any other context or screenshots about the feature request here._
</details>

72
.github/workflows/ci.yml vendored Normal file
View file

@ -0,0 +1,72 @@
name: Continuous Integration
on:
push:
branches:
- main
pull_request:
branches:
- main
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
ktlint:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- name: Install
run: |
curl -sSLO https://github.com/pinterest/ktlint/releases/download/1.0.1/ktlint
chmod a+x ktlint
sudo mv ktlint /usr/local/bin/
- name: Run
run: ktlint --reporter sarif -l none > ktlint.sarif
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v3
if: success() || failure()
with:
sarif_file: ktlint.sarif
detekt:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Run
run: |
chmod +x gradlew
./gradlew detekt
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v3
if: success() || failure()
with:
sarif_file: app/build/reports/detekt/detekt.sarif
- name: Job Summary
if: success() || failure()
run: cat ./app/build/reports/detekt/detekt.md >> $GITHUB_STEP_SUMMARY
build:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Run
run: |
chmod +x gradlew
./gradlew build

89
.gitignore vendored Normal file
View file

@ -0,0 +1,89 @@
# Built application files
*.apk
*.aar
*.ap_
*.aab
# Files for the ART/Dalvik VM
*.dex
# Java class files
*.class
# Generated files
bin/
gen/
out/
# Uncomment the following line in case you need and you don't have the release build type files in your app
# release/
# Gradle files
.gradle/
build/
# Local configuration file (sdk path, etc)
local.properties
# Proguard folder generated by Eclipse
proguard/
# Log Files
*.log
# Android Studio Navigation editor temp files
.navigation/
# Android Studio captures folder
captures/
# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
.idea/jarRepositories.xml
# Android Studio 3 in .gitignore file.
.idea/caches
.idea/modules.xml
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
.idea/navEditor.xml
# Keystore files
# Uncomment the following lines if you do not want to check your keystore files in.
#*.jks
#*.keystore
# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild
.cxx/
# Google Services (e.g. APIs or Firebase)
# google-services.json
# Freeline
freeline.py
freeline/
freeline_project_description.json
# fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/readme.md
# Version control
vcs.xml
# lint
lint/intermediates/
lint/generated/
lint/outputs/
lint/tmp/
# lint/reports/
# Android Profiling
*.hprof

6
.idea/AndroidProjectSystem.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AndroidProjectSystem">
<option name="providerId" value="com.android.tools.idea.GradleProjectSystem" />
</component>
</project>

123
.idea/codeStyles/Project.xml generated Normal file
View file

@ -0,0 +1,123 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JetCodeStyleSettings>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<codeStyleSettings language="XML">
<option name="FORCE_REARRANGE_MODE" value="1" />
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
</indentOptions>
<arrangement>
<rules>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:android</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:id</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>style</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
<order>ANDROID_ATTRIBUTE_ORDER</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>.*</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
</rules>
</arrangement>
</codeStyleSettings>
<codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</codeStyleSettings>
</code_scheme>
</component>

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View file

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>

6
.idea/compiler.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="21" />
</component>
</project>

10
.idea/deploymentTargetDropDown.xml generated Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<value>
<entry key="app">
<State />
</entry>
</value>
</component>
</project>

10
.idea/deploymentTargetSelector.xml generated Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetSelector">
<selectionStates>
<SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" />
</SelectionState>
</selectionStates>
</component>
</project>

4
.idea/encodings.xml generated Normal file
View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>

1006
.idea/inspectionProfiles/Strict.xml generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="PROJECT_PROFILE" value="Strict" />
<version value="1.0" />
</settings>
</component>

6
.idea/kotlinc.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinJpsPluginSettings">
<option name="version" value="2.1.10" />
</component>
</project>

10
.idea/migrations.xml generated Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectMigrations">
<option name="MigrateToGradleLocalJavaHome">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</component>
</project>

58
.idea/misc.xml generated Normal file
View file

@ -0,0 +1,58 @@
<project version="4">
<component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.annotation.NonNull" />
<option name="myNullables">
<value>
<list size="17">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
<item index="4" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.Nullable" />
<item index="6" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNullable" />
<item index="7" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.Nullable" />
<item index="8" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableDecl" />
<item index="9" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableType" />
<item index="10" class="java.lang.String" itemvalue="android.annotation.Nullable" />
<item index="11" class="java.lang.String" itemvalue="com.android.annotations.Nullable" />
<item index="12" class="java.lang.String" itemvalue="org.eclipse.jdt.annotation.Nullable" />
<item index="13" class="java.lang.String" itemvalue="io.reactivex.annotations.Nullable" />
<item index="14" class="java.lang.String" itemvalue="io.reactivex.rxjava3.annotations.Nullable" />
<item index="15" class="java.lang.String" itemvalue="org.jspecify.annotations.Nullable" />
<item index="16" class="java.lang.String" itemvalue="jakarta.annotation.Nullable" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="17">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
<item index="4" class="java.lang.String" itemvalue="androidx.annotation.NonNull" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNonNull" />
<item index="6" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.NonNull" />
<item index="7" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullDecl" />
<item index="8" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullType" />
<item index="9" class="java.lang.String" itemvalue="android.annotation.NonNull" />
<item index="10" class="java.lang.String" itemvalue="com.android.annotations.NonNull" />
<item index="11" class="java.lang.String" itemvalue="org.eclipse.jdt.annotation.NonNull" />
<item index="12" class="java.lang.String" itemvalue="io.reactivex.annotations.NonNull" />
<item index="13" class="java.lang.String" itemvalue="io.reactivex.rxjava3.annotations.NonNull" />
<item index="14" class="java.lang.String" itemvalue="lombok.NonNull" />
<item index="15" class="java.lang.String" itemvalue="org.jspecify.annotations.NonNull" />
<item index="16" class="java.lang.String" itemvalue="jakarta.annotation.Nonnull" />
</list>
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
<option name="id" value="Android" />
</component>
</project>

17
.idea/runConfigurations.xml generated Normal file
View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.intellij.execution.junit.AbstractAllInDirectoryConfigurationProducer" />
<option value="com.intellij.execution.junit.AllInPackageConfigurationProducer" />
<option value="com.intellij.execution.junit.PatternConfigurationProducer" />
<option value="com.intellij.execution.junit.TestInClassConfigurationProducer" />
<option value="com.intellij.execution.junit.UniqueIdConfigurationProducer" />
<option value="com.intellij.execution.junit.testDiscovery.JUnitTestDiscoveryConfigurationProducer" />
<option value="org.jetbrains.kotlin.idea.junit.KotlinJUnitRunConfigurationProducer" />
<option value="org.jetbrains.kotlin.idea.junit.KotlinPatternConfigurationProducer" />
</set>
</option>
</component>
</project>

674
LICENSE Normal file
View file

@ -0,0 +1,674 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.

View file

@ -1,3 +1,56 @@
# home-app
![App Icon](https://raw.githubusercontent.com/Domi04151309/HomeApp/main/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp)
# Home App for Android™
HomeApp is a small and easy to use smart home app with a simple framework. The goal of this application is to make remote execution of predefined features as easy and user-friendly as possible to help you get started with smart home technology.
Home-Lab Integration for Android
<a href="https://f-droid.org/packages/io.github.domi04151309.home">
<img src="https://fdroid.gitlab.io/artwork/badge/get-it-on.png"
alt="Get it on F-Droid"
height="80"/>
</a>
## Donate
Support the development by donating.
<a href="https://www.paypal.com/donate/?hosted_button_id=487FTCX52P9WA">
<img src="https://img.shields.io/badge/-Donate-black?style=for-the-badge&logo=paypal" alt="Donate">
</a>
## Supported devices
Home App natively supports the following devices:
- [Philips Hue Bridge](https://github.com/Domi04151309/HomeApp/wiki/Hue-API-%28v1%29)
- [Shelly](https://github.com/Domi04151309/HomeApp/wiki/Shelly) Gen 1 devices
- [Shelly](https://github.com/Domi04151309/HomeApp/wiki/Shelly) Gen 2 devices
- Devices using [ESP Easy](https://github.com/Domi04151309/HomeApp/wiki/ESP-Easy)
- Devices using [Tasmota](https://github.com/Domi04151309/HomeApp/wiki/Tasmota)
- Devices using the [Node-RED dashboard](https://github.com/Domi04151309/HomeApp/wiki/Node-RED-Dashboard)
- Devices using the [SimpleHome API](https://github.com/Domi04151309/HomeApp/wiki/SimpleHome-API)
- Devices with a [web interface](https://github.com/Domi04151309/HomeApp/wiki/Websites)
## How it works
Communication between the devices uses HTTP requests and JSON strings. After the commanding device has send a HTTP request to the smart home device, the smart home device sends back a JSON string containing the information the app needs.
This app is especially useful if you are using microcontrollers or other small devices such as the Raspberry Pi for smart home automation.
## Previews
<img src="https://raw.githubusercontent.com/Domi04151309/HomeApp/main/fastlane/metadata/android/en-US/images/phoneScreenshots/1.jpg" width="18%" /><img src="https://raw.githubusercontent.com/Domi04151309/HomeApp/main/fastlane/metadata/android/en-US/images/phoneScreenshots/2.jpg" width="18%" /><img src="https://raw.githubusercontent.com/Domi04151309/HomeApp/main/fastlane/metadata/android/en-US/images/phoneScreenshots/3.jpg" width="18%" /><img src="https://raw.githubusercontent.com/Domi04151309/HomeApp/main/fastlane/metadata/android/en-US/images/phoneScreenshots/4.jpg" width="18%" /><img src="https://raw.githubusercontent.com/Domi04151309/HomeApp/main/fastlane/metadata/android/en-US/images/phoneScreenshots/5.jpg" width="18%" /><img src="https://raw.githubusercontent.com/Domi04151309/HomeApp/main/fastlane/metadata/android/en-US/images/phoneScreenshots/6.jpg" width="18%" />
<i style="color:gray;">Android, Google Play and the Google Play logo are trademarks of Google LLC.</i>
## Legal Notice
Copyright (C) 2020 Domi04151309
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
<a href="https://anvil-solutions.com/de/imprint">Impressum</a>

1
app/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/build

103
app/build.gradle.kts Normal file
View file

@ -0,0 +1,103 @@
import com.android.build.gradle.internal.tasks.factory.dependsOn
private val readAndUnderstoodLicense = false
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("io.gitlab.arturbosch.detekt")
}
android {
namespace = "io.github.domi04151309.home"
compileSdk = 34
defaultConfig {
applicationId = "io.github.domi04151309.home"
minSdk = 23
//noinspection EditedTargetSdkVersion
targetSdk = 34
versionCode = 1120
versionName = "1.12.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
}
release {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
}
}
testOptions {
unitTests.isIncludeAndroidResources = true
}
buildFeatures {
buildConfig = true
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
detekt {
config.setFrom(file("detekt-config.yml"))
buildUponDefaultConfig = true
basePath = rootProject.projectDir.absolutePath
}
lint {
disable += "MissingTranslation"
}
project.tasks.preBuild.dependsOn("license")
}
tasks.register("license") {
doFirst {
val data =
file("./src/main/res/xml/pref_about.xml")
.readText()
.contains("app:key=\"license\"")
if (!data) {
throw Exception(
"Please note that removing the license from the about page is not allowed if you " +
"plan to publish your modified version of this app. " +
"Please read the project's LICENSE.",
)
}
if (!(
android.defaultConfig.applicationId?.contains("domi04151309") == true ||
readAndUnderstoodLicense
)
) {
throw Exception(
"Please make sure you have read and understood the LICENSE!",
)
}
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.7.1")
implementation("com.google.android.material:material:1.12.0")
implementation("androidx.preference:preference-ktx:1.2.1")
implementation("androidx.annotation:annotation:1.9.1")
implementation("com.android.volley:volley:1.2.1")
implementation("androidx.security:security-crypto-ktx:1.1.0-beta01")
implementation("com.github.skydoves:colorpickerview:2.3.0")
testImplementation("junit:junit:4.13.2")
testImplementation("org.robolectric:robolectric:4.14.1")
}

784
app/detekt-config.yml Normal file
View file

@ -0,0 +1,784 @@
build:
maxIssues: 0
excludeCorrectable: true
weights:
# complexity: 2
# LongParameterList: 1
# style: 1
# comments: 1
config:
validation: true
warningsAsErrors: true
checkExhaustiveness: true
# when writing own rules with new properties, exclude the property path e.g.: 'my_rule_set,.*>.*>[my_property]'
excludes: ''
processors:
active: true
exclude:
- 'DetektProgressListener'
# - 'KtFileCountProcessor'
# - 'PackageCountProcessor'
# - 'ClassCountProcessor'
# - 'FunctionCountProcessor'
# - 'PropertyCountProcessor'
# - 'ProjectComplexityProcessor'
# - 'ProjectCognitiveComplexityProcessor'
# - 'ProjectLLOCProcessor'
# - 'ProjectCLOCProcessor'
# - 'ProjectLOCProcessor'
# - 'ProjectSLOCProcessor'
# - 'LicenseHeaderLoaderExtension'
console-reports:
active: true
exclude:
- 'ProjectStatisticsReport'
- 'ComplexityReport'
- 'NotificationReport'
- 'FindingsReport'
- 'FileBasedFindingsReport'
# - 'LiteFindingsReport'
output-reports:
active: true
exclude:
# - 'TxtOutputReport'
# - 'XmlOutputReport'
# - 'HtmlOutputReport'
# - 'MdOutputReport'
# - 'SarifOutputReport'
comments:
active: true
AbsentOrWrongFileLicense:
active: false
licenseTemplateFile: 'license.template'
licenseTemplateIsRegex: true
CommentOverPrivateFunction:
active: true
CommentOverPrivateProperty:
active: true
DeprecatedBlockTag:
active: true
EndOfSentenceFormat:
active: true
endOfSentenceFormat: '([.?!][ \t\n\r\f<])|([.?!:]$)'
KDocReferencesNonPublicProperty:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
OutdatedDocumentation:
active: true
matchTypeParameters: true
matchDeclarationsOrder: true
allowParamOnConstructorProperties: true
UndocumentedPublicClass:
active: false
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
searchInNestedClass: true
searchInInnerClass: true
searchInInnerObject: true
searchInInnerInterface: true
searchInProtectedClass: true
UndocumentedPublicFunction:
active: false
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
searchProtectedFunction: true
UndocumentedPublicProperty:
active: false
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
searchProtectedProperty: true
complexity:
active: true
CognitiveComplexMethod:
active: true
threshold: 15
ComplexCondition:
active: true
threshold: 4
ComplexInterface:
active: true
threshold: 10
includeStaticDeclarations: true
includePrivateDeclarations: true
ignoreOverloaded: true
CyclomaticComplexMethod:
active: true
threshold: 15
ignoreSingleWhenExpression: true
ignoreSimpleWhenEntries: true
ignoreNestingFunctions: true
nestingFunctions:
- 'also'
- 'apply'
- 'forEach'
- 'isNotNull'
- 'ifNull'
- 'let'
- 'run'
- 'use'
- 'with'
LabeledExpression:
active: true
ignoredLabels: []
LargeClass:
active: true
threshold: 600
LongMethod:
active: true
threshold: 60
LongParameterList:
active: true
functionThreshold: 6
constructorThreshold: 7
ignoreDefaultParameters: true
ignoreDataClasses: true
ignoreAnnotatedParameter: []
MethodOverloading:
active: true
threshold: 6
NamedArguments:
active: true
threshold: 3
ignoreArgumentsMatchingNames: true
NestedBlockDepth:
active: true
threshold: 4
NestedScopeFunctions:
active: true
threshold: 1
functions:
- 'kotlin.apply'
- 'kotlin.run'
- 'kotlin.with'
- 'kotlin.let'
- 'kotlin.also'
ReplaceSafeCallChainWithRun:
active: true
StringLiteralDuplication:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
threshold: 3
ignoreAnnotation: true
excludeStringsWithLessThan5Characters: true
ignoreStringsRegex: '$^'
TooManyFunctions:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
thresholdInFiles: 11
thresholdInClasses: 11
thresholdInInterfaces: 11
thresholdInObjects: 11
thresholdInEnums: 11
ignoreDeprecated: true
ignorePrivate: true
ignoreOverridden: true
coroutines:
active: true
GlobalCoroutineUsage:
active: true
InjectDispatcher:
active: true
dispatcherNames:
- 'IO'
- 'Default'
- 'Unconfined'
RedundantSuspendModifier:
active: true
SleepInsteadOfDelay:
active: true
SuspendFunSwallowedCancellation:
active: true
SuspendFunWithCoroutineScopeReceiver:
active: true
SuspendFunWithFlowReturnType:
active: true
empty-blocks:
active: true
EmptyCatchBlock:
active: true
allowedExceptionNameRegex: '_|(ignore|expected).*'
EmptyClassBlock:
active: true
EmptyDefaultConstructor:
active: true
EmptyDoWhileBlock:
active: true
EmptyElseBlock:
active: true
EmptyFinallyBlock:
active: true
EmptyForBlock:
active: true
EmptyFunctionBlock:
active: true
ignoreOverridden: true
EmptyIfBlock:
active: true
EmptyInitBlock:
active: true
EmptyKtFile:
active: true
EmptySecondaryConstructor:
active: true
EmptyTryBlock:
active: true
EmptyWhenBlock:
active: true
EmptyWhileBlock:
active: true
exceptions:
active: true
ExceptionRaisedInUnexpectedLocation:
active: true
methodNames:
- 'equals'
- 'finalize'
- 'hashCode'
- 'toString'
InstanceOfCheckForException:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
NotImplementedDeclaration:
active: true
ObjectExtendsThrowable:
active: true
PrintStackTrace:
active: true
RethrowCaughtException:
active: true
ReturnFromFinally:
active: true
ignoreLabeled: true
SwallowedException:
active: true
ignoredExceptionTypes:
- 'InterruptedException'
- 'MalformedURLException'
- 'NumberFormatException'
- 'ParseException'
allowedExceptionNameRegex: '_|(ignore|expected).*'
ThrowingExceptionFromFinally:
active: true
ThrowingExceptionInMain:
active: true
ThrowingExceptionsWithoutMessageOrCause:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
exceptions:
- 'ArrayIndexOutOfBoundsException'
- 'Exception'
- 'IllegalArgumentException'
- 'IllegalMonitorStateException'
- 'IllegalStateException'
- 'IndexOutOfBoundsException'
- 'NullPointerException'
- 'RuntimeException'
- 'Throwable'
ThrowingNewInstanceOfSameException:
active: true
TooGenericExceptionCaught:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
exceptionNames:
- 'ArrayIndexOutOfBoundsException'
- 'Error'
- 'Exception'
- 'IllegalMonitorStateException'
- 'IndexOutOfBoundsException'
- 'NullPointerException'
- 'RuntimeException'
- 'Throwable'
allowedExceptionNameRegex: '_|(ignore|expected).*'
TooGenericExceptionThrown:
active: true
exceptionNames:
- 'Error'
- 'Exception'
- 'RuntimeException'
- 'Throwable'
naming:
active: true
BooleanPropertyNaming:
active: true
allowedPattern: '^(is|has|are)'
ClassNaming:
active: true
classPattern: '[A-Z][a-zA-Z0-9]*'
ConstructorParameterNaming:
active: true
parameterPattern: '[a-z][A-Za-z0-9]*'
privateParameterPattern: '[a-z][A-Za-z0-9]*'
excludeClassPattern: '$^'
EnumNaming:
active: true
enumEntryPattern: '[A-Z][_a-zA-Z0-9]*'
ForbiddenClassName:
active: true
forbiddenName: []
FunctionMaxLength:
active: true
maximumFunctionNameLength: 30
FunctionMinLength:
active: true
minimumFunctionNameLength: 3
FunctionNaming:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
functionPattern: '[a-z][a-zA-Z0-9]*'
excludeClassPattern: '$^'
FunctionParameterNaming:
active: true
parameterPattern: '[a-z][A-Za-z0-9]*'
excludeClassPattern: '$^'
InvalidPackageDeclaration:
active: true
rootPackage: ''
requireRootInDeclaration: false
LambdaParameterNaming:
active: true
parameterPattern: '[a-z][A-Za-z0-9]*|_'
MatchingDeclarationName:
active: true
mustBeFirst: true
MemberNameEqualsClassName:
active: true
ignoreOverridden: true
NoNameShadowing:
active: true
NonBooleanPropertyPrefixedWithIs:
active: true
ObjectPropertyNaming:
active: true
constantPattern: '[A-Za-z][_A-Za-z0-9]*'
propertyPattern: '[A-Za-z][_A-Za-z0-9]*'
privatePropertyPattern: '(_)?[A-Za-z][_A-Za-z0-9]*'
PackageNaming:
active: true
packagePattern: '[a-z]+(\.[a-z][A-Za-z0-9]*)*'
TopLevelPropertyNaming:
active: true
constantPattern: '[A-Z][_A-Z0-9]*'
propertyPattern: '[A-Za-z][_A-Za-z0-9]*'
privatePropertyPattern: '_?[A-Za-z][_A-Za-z0-9]*'
VariableMaxLength:
active: true
maximumVariableNameLength: 64
VariableMinLength:
active: true
minimumVariableNameLength: 1
VariableNaming:
active: true
variablePattern: '[a-z][A-Za-z0-9]*'
privateVariablePattern: '(_)?[a-z][A-Za-z0-9]*'
excludeClassPattern: '$^'
performance:
active: true
ArrayPrimitive:
active: true
CouldBeSequence:
active: true
threshold: 3
ForEachOnRange:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
SpreadOperator:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
UnnecessaryPartOfBinaryExpression:
active: true
UnnecessaryTemporaryInstantiation:
active: true
potential-bugs:
active: true
AvoidReferentialEquality:
active: true
forbiddenTypePatterns:
- 'kotlin.String'
CastNullableToNonNullableType:
active: true
CastToNullableType:
active: true
Deprecation:
active: true
DontDowncastCollectionTypes:
active: true
DoubleMutabilityForCollection:
active: true
mutableTypes:
- 'kotlin.collections.MutableList'
- 'kotlin.collections.MutableMap'
- 'kotlin.collections.MutableSet'
- 'java.util.ArrayList'
- 'java.util.LinkedHashSet'
- 'java.util.HashSet'
- 'java.util.LinkedHashMap'
- 'java.util.HashMap'
ElseCaseInsteadOfExhaustiveWhen:
active: true
ignoredSubjectTypes: []
EqualsAlwaysReturnsTrueOrFalse:
active: true
EqualsWithHashCodeExist:
active: true
ExitOutsideMain:
active: true
ExplicitGarbageCollectionCall:
active: true
HasPlatformType:
active: true
IgnoredReturnValue:
active: true
restrictToConfig: true
returnValueAnnotations:
- 'CheckResult'
- '*.CheckResult'
- 'CheckReturnValue'
- '*.CheckReturnValue'
ignoreReturnValueAnnotations:
- 'CanIgnoreReturnValue'
- '*.CanIgnoreReturnValue'
returnValueTypes:
- 'kotlin.sequences.Sequence'
- 'kotlinx.coroutines.flow.*Flow'
- 'java.util.stream.*Stream'
ignoreFunctionCall: []
ImplicitDefaultLocale:
active: true
ImplicitUnitReturnType:
active: true
allowExplicitReturnType: true
InvalidRange:
active: true
IteratorHasNextCallsNextMethod:
active: true
IteratorNotThrowingNoSuchElementException:
active: true
LateinitUsage:
active: false
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
ignoreOnClassesPattern: ''
MapGetWithNotNullAssertionOperator:
active: true
MissingPackageDeclaration:
active: true
excludes: ['**/*.kts']
NullCheckOnMutableProperty:
active: true
NullableToStringCall:
active: true
PropertyUsedBeforeDeclaration:
active: true
UnconditionalJumpStatementInLoop:
active: true
UnnecessaryNotNullCheck:
active: true
UnnecessaryNotNullOperator:
active: true
UnnecessarySafeCall:
active: true
UnreachableCatchBlock:
active: true
UnreachableCode:
active: true
UnsafeCallOnNullableType:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
UnsafeCast:
active: true
UnusedUnaryOperator:
active: true
UselessPostfixExpression:
active: true
WrongEqualsTypeParameter:
active: true
style:
active: true
AlsoCouldBeApply:
active: true
BracesOnIfStatements:
active: true
singleLine: 'never'
multiLine: 'always'
BracesOnWhenStatements:
active: true
singleLine: 'necessary'
multiLine: 'consistent'
CanBeNonNullable:
active: true
CascadingCallWrapping:
active: true
includeElvis: true
ClassOrdering:
active: true
CollapsibleIfStatements:
active: true
DataClassContainsFunctions:
active: true
conversionFunctionPrefix:
- 'to'
allowOperators: true
DataClassShouldBeImmutable:
active: true
DestructuringDeclarationWithTooManyEntries:
active: true
maxDestructuringEntries: 3
DoubleNegativeLambda:
active: true
negativeFunctions:
- reason: 'Use `takeIf` instead.'
value: 'takeUnless'
- reason: 'Use `all` instead.'
value: 'none'
negativeFunctionNameParts:
- 'not'
- 'non'
EqualsNullCall:
active: true
EqualsOnSignatureLine:
active: true
ExplicitCollectionElementAccessMethod:
active: true
ExplicitItLambdaParameter:
active: true
ExpressionBodySyntax:
active: true
includeLineWrapping: true
ForbiddenAnnotation:
active: true
annotations:
- reason: 'it is a java annotation. Use `Suppress` instead.'
value: 'java.lang.SuppressWarnings'
- reason: 'it is a java annotation. Use `kotlin.Deprecated` instead.'
value: 'java.lang.Deprecated'
- reason: 'it is a java annotation. Use `kotlin.annotation.MustBeDocumented` instead.'
value: 'java.lang.annotation.Documented'
- reason: 'it is a java annotation. Use `kotlin.annotation.Target` instead.'
value: 'java.lang.annotation.Target'
- reason: 'it is a java annotation. Use `kotlin.annotation.Retention` instead.'
value: 'java.lang.annotation.Retention'
- reason: 'it is a java annotation. Use `kotlin.annotation.Repeatable` instead.'
value: 'java.lang.annotation.Repeatable'
- reason: 'Kotlin does not support @Inherited annotation, see https://youtrack.jetbrains.com/issue/KT-22265'
value: 'java.lang.annotation.Inherited'
ForbiddenComment:
active: true
comments:
- reason: 'Forbidden FIXME todo marker in comment, please fix the problem.'
value: 'FIXME:'
- reason: 'Forbidden STOPSHIP todo marker in comment, please address the problem before shipping the code.'
value: 'STOPSHIP:'
- reason: 'Forbidden TODO todo marker in comment, please do the changes.'
value: 'TODO:'
allowedPatterns: ''
ForbiddenImport:
active: true
imports: []
forbiddenPatterns: ''
ForbiddenMethodCall:
active: true
methods:
- reason: 'print does not allow you to configure the output stream. Use a logger instead.'
value: 'kotlin.io.print'
- reason: 'println does not allow you to configure the output stream. Use a logger instead.'
value: 'kotlin.io.println'
ForbiddenSuppress:
active: true
rules: []
ForbiddenVoid:
active: true
ignoreOverridden: true
ignoreUsageInGenerics: true
FunctionOnlyReturningConstant:
active: true
ignoreOverridableFunction: true
ignoreActualFunction: true
excludedFunctions: []
LoopWithTooManyJumpStatements:
active: true
maxJumpCount: 1
MagicNumber:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**', '**/*.kts']
ignoreNumbers:
- '-1'
- '0'
- '1'
- '2'
ignoreHashCodeFunction: true
ignorePropertyDeclaration: true
ignoreLocalVariableDeclaration: true
ignoreConstantDeclaration: true
ignoreCompanionObjectPropertyDeclaration: true
ignoreAnnotation: true
ignoreNamedArgument: true
ignoreEnums: true
ignoreRanges: true
ignoreExtensionFunctions: true
MandatoryBracesLoops:
active: true
MaxChainedCallsOnSameLine:
active: true
maxChainedCalls: 5
MaxLineLength:
active: true
maxLineLength: 120
excludePackageStatements: true
excludeImportStatements: true
excludeCommentStatements: true
excludeRawStrings: true
MayBeConst:
active: true
ModifierOrder:
active: true
MultilineLambdaItParameter:
active: true
MultilineRawStringIndentation:
active: false
indentSize: 4
trimmingMethods:
- 'trimIndent'
- 'trimMargin'
NestedClassesVisibility:
active: true
NewLineAtEndOfFile:
active: true
NoTabs:
active: true
NullableBooleanCheck:
active: true
ObjectLiteralToLambda:
active: true
OptionalAbstractKeyword:
active: true
OptionalUnit:
active: true
PreferToOverPairSyntax:
active: true
ProtectedMemberInFinalClass:
active: true
RedundantExplicitType:
active: true
RedundantHigherOrderMapUsage:
active: true
RedundantVisibilityModifierRule:
active: true
ReturnCount:
active: true
max: 2
excludedFunctions:
- 'equals'
excludeLabeled: true
excludeReturnFromLambda: true
excludeGuardClauses: true
SafeCast:
active: true
SerialVersionUIDInSerializableClass:
active: true
SpacingBetweenPackageAndImports:
active: true
StringShouldBeRawString:
active: true
maxEscapedCharacterCount: 2
ignoredCharacters: []
ThrowsCount:
active: true
max: 2
excludeGuardClauses: true
TrailingWhitespace:
active: true
TrimMultilineRawString:
active: false
trimmingMethods:
- 'trimIndent'
- 'trimMargin'
UnderscoresInNumericLiterals:
active: true
acceptableLength: 4
allowNonStandardGrouping: true
UnnecessaryAbstractClass:
active: true
UnnecessaryAnnotationUseSiteTarget:
active: true
UnnecessaryApply:
active: true
UnnecessaryBackticks:
active: true
UnnecessaryBracesAroundTrailingLambda:
active: true
UnnecessaryFilter:
active: true
UnnecessaryInheritance:
active: true
UnnecessaryInnerClass:
active: true
UnnecessaryLet:
active: true
UnnecessaryParentheses:
active: true
allowForUnclearPrecedence: true
UntilInsteadOfRangeTo:
active: true
UnusedImports:
active: true
UnusedParameter:
active: true
allowedNames: 'ignored|expected'
UnusedPrivateClass:
active: true
UnusedPrivateMember:
active: true
allowedNames: ''
UnusedPrivateProperty:
active: true
allowedNames: '_|ignored|expected|serialVersionUID'
UseAnyOrNoneInsteadOfFind:
active: true
UseArrayLiteralsInAnnotations:
active: true
UseCheckNotNull:
active: true
UseCheckOrError:
active: true
UseDataClass:
active: true
allowVars: true
UseEmptyCounterpart:
active: true
UseIfEmptyOrIfBlank:
active: true
UseIfInsteadOfWhen:
active: true
ignoreWhenContainingVariableDeclaration: true
UseIsNullOrEmpty:
active: true
UseLet:
active: true
UseOrEmpty:
active: true
UseRequire:
active: true
UseRequireNotNull:
active: true
UseSumOfInsteadOfFlatMapSize:
active: true
UselessCallOnNotNull:
active: true
UtilityClassWithPublicConstructor:
active: true
VarCouldBeVal:
active: true
ignoreLateinitVar: true
WildcardImport:
active: true
excludeImports:
- 'java.util.*'

27
app/proguard-rules.pro vendored Normal file
View file

@ -0,0 +1,27 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
-keepattributes SourceFile,LineNumberTable
-dontobfuscate
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
# This is generated automatically by the Android Gradle plugin.
-dontwarn com.google.errorprone.annotations.Immutable
-dontwarn javax.annotation.concurrent.GuardedBy
-dontwarn javax.annotation.Nullable

View file

@ -0,0 +1,157 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="preferExternal">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"
tools:ignore="ScopedStorage" />
<queries>
<package android:name="com.philips.lighting.hue2" />
</queries>
<application
android:name=".Application"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/Theme.Material3.DayNight"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute"
android:dataExtractionRules="@xml/data_extraction_rules">
<activity
android:name=".activities.HueSceneActivity"
android:theme="@style/Theme.Material3.DayNight.NoActionBar"
android:parentActivityName=".activities.MainActivity" />
<activity
android:name=".activities.AboutActivity"
android:label="@string/about"
android:launchMode="singleTop"
android:parentActivityName=".activities.SettingsActivity" />
<activity
android:name=".activities.LibraryActivity"
android:label="@string/about_libraries"
android:launchMode="singleTop"
android:parentActivityName=".activities.AboutActivity" />
<activity
android:name=".activities.SearchDevicesActivity"
android:label="@string/pref_add"
android:parentActivityName=".activities.DevicesActivity" />
<activity
android:name=".activities.HueConnectActivity"
android:theme="@style/Theme.Material3.DayNight.NoActionBar" />
<activity
android:name=".activities.HueLampActivity"
android:theme="@style/Theme.Material3.DayNight.NoActionBar"
android:parentActivityName=".activities.MainActivity" />
<activity
android:name=".activities.MainActivity"
android:launchMode="singleTop"
android:configChanges="orientation|screenSize"
android:theme="@style/Theme.Material3.DayNight.NoActionBar"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.SettingsActivity"
android:label="@string/pref"
android:launchMode="singleTop"
android:parentActivityName=".activities.MainActivity" />
<activity
android:name=".activities.WebActivity"
android:label="@string/webView"
android:configChanges="orientation|screenSize"
android:parentActivityName=".activities.MainActivity" />
<activity
android:name=".activities.DevicesActivity"
android:label="@string/pref"
android:launchMode="singleTop"
android:parentActivityName=".activities.SettingsActivity" />
<activity
android:name=".activities.EditDeviceActivity"
android:launchMode="singleTop"
android:theme="@style/Theme.Material3.DayNight.NoActionBar"
android:parentActivityName=".activities.DevicesActivity" />
<activity
android:name=".activities.DeviceInfoActivity"
android:label="@string/device_config_info"
android:launchMode="singleTop"
android:parentActivityName=".activities.EditDeviceActivity" />
<activity
android:name=".activities.ControlInfoActivity"
android:launchMode="singleTop"
android:theme="@style/Theme.Material3.DayNight.NoActionBar" />
<activity
android:name=".activities.ShortcutDeviceActivity"
android:label="@string/pref_add_name_empty"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>
<activity
android:name=".activities.ShortcutHueRoomActivity"
android:label="@string/hue_room"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>
<activity
android:name=".activities.ShortcutHueSceneActivity"
android:label="@string/hue_scene_shortcut"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>
<activity
android:name=".activities.ShortcutHueSceneActionActivity"
android:taskAffinity=""
android:theme="@android:style/Theme.NoDisplay"
android:exported="true" />
<activity
android:name=".activities.ShortcutTasmotaActivity"
android:label="@string/tasmota"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>
<activity
android:name=".activities.ShortcutTasmotaActionActivity"
android:taskAffinity=""
android:theme="@android:style/Theme.NoDisplay"
android:exported="true" />
<service
android:name=".services.ControlService"
android:permission="android.permission.BIND_CONTROLS"
android:exported="true"
tools:targetApi="30">
<intent-filter>
<action android:name="android.service.controls.ControlsProviderService" />
</intent-filter>
</service>
</application>
</manifest>

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

View file

@ -0,0 +1,110 @@
package com.rine.upnpdiscovery
import android.util.Log
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
import org.xmlpull.v1.XmlPullParserFactory
import java.io.ByteArrayInputStream
class UPnPDevice internal constructor(val hostAddress: String, header: String) {
internal val location: String
val server: String
// XML content
private var descriptionXML: String = ""
// From description XML
var friendlyName: String = ""
private var deviceType: String = ""
private var presentationURL: String = ""
private var serialNumber: String = ""
private var modelName: String = ""
private var modelNumber: String = ""
private var modelURL: String = ""
private var manufacturer: String = ""
private var manufacturerURL: String = ""
private var udn: String = ""
private var urlBase: String = ""
init {
location = parseHeader(header, "LOCATION: ")
server = parseHeader(header, "SERVER: ")
}
internal fun update(xml: String) {
descriptionXML = xml
try {
xmlParse(xml)
} catch (e: XmlPullParserException) {
Log.w(UPnPDiscovery.TAG, e.toString())
}
}
override fun toString(): String =
"FriendlyName: " + friendlyName + LINE_END +
"ModelName: " + modelName + LINE_END +
"HostAddress: " + hostAddress + LINE_END +
"Location: " + location + LINE_END +
"DeviceType: " + deviceType + LINE_END +
"PresentationURL: " + presentationURL + LINE_END +
"SerialNumber: " + serialNumber + LINE_END +
"ModelURL: " + modelURL + LINE_END +
"ModelNumber: " + modelNumber + LINE_END +
"Manufacturer: " + manufacturer + LINE_END +
"ManufacturerURL: " + manufacturerURL + LINE_END +
"UDN: " + udn + LINE_END +
"URLBase: " + urlBase
private fun parseHeader(
mSearchAnswer: String,
whatSearch: String,
): String {
var result = ""
var searchLinePos = mSearchAnswer.indexOf(whatSearch)
if (searchLinePos != -1) {
searchLinePos += whatSearch.length
val locColon = mSearchAnswer.indexOf(LINE_END, searchLinePos)
result = mSearchAnswer.substring(searchLinePos, locColon)
}
return result
}
private fun readText(parser: XmlPullParser): String {
var result = ""
if (parser.next() == XmlPullParser.TEXT) {
result = parser.text
parser.nextTag()
}
return result
}
private fun xmlParse(xml: String) {
val xmlFactoryObject = XmlPullParserFactory.newInstance()
val parser = xmlFactoryObject.newPullParser()
parser.setInput(ByteArrayInputStream(xml.toByteArray(Charsets.UTF_8)), null)
var event = parser.eventType
while (event != XmlPullParser.END_DOCUMENT) {
val name = parser.name
if (event == XmlPullParser.START_TAG) {
when (name) {
"friendlyName" -> friendlyName = readText(parser)
"deviceType" -> deviceType = readText(parser)
"presentationURL" -> presentationURL = readText(parser)
"serialNumber" -> serialNumber = readText(parser)
"modelName" -> modelName = readText(parser)
"modelNumber" -> modelNumber = readText(parser)
"modelURL" -> modelURL = readText(parser)
"manufacturer" -> manufacturer = readText(parser)
"manufacturerURL" -> manufacturerURL = readText(parser)
"UDN" -> udn = readText(parser)
"URLBase" -> urlBase = readText(parser)
}
}
event = parser.next()
}
}
companion object {
private const val LINE_END = "\r\n"
}
}

View file

@ -0,0 +1,178 @@
package com.rine.upnpdiscovery
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.net.wifi.WifiManager
import android.os.AsyncTask
import android.util.Log
import com.android.volley.Request
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import java.io.IOException
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress
import java.net.InetSocketAddress
@Suppress("MagicNumber")
class UPnPDiscovery : AsyncTask<Activity, UPnPDiscovery.OnDiscoveryListener, Void> {
private val devices = HashSet<UPnPDevice>()
@SuppressLint("StaticFieldLeak")
private val mContext: Context
private var mThreadsCount: Int = 0
private val mCustomQuery: String
private val mInternetAddress: String
private val mPort: Int
private val mListener: OnDiscoveryListener
interface OnDiscoveryListener {
fun onStart()
fun onFoundNewDevice(device: UPnPDevice)
fun onFinish(devices: HashSet<UPnPDevice>)
fun onError(e: Exception)
}
private constructor(activity: Activity, listener: OnDiscoveryListener) {
mContext = activity.applicationContext
mListener = listener
mThreadsCount = 0
mCustomQuery = DEFAULT_QUERY
mInternetAddress = DEFAULT_ADDRESS
mPort = 1900
}
private constructor(
activity: Activity,
listener: OnDiscoveryListener,
customQuery: String,
address: String,
port: Int,
) {
mContext = activity.applicationContext
mListener = listener
mThreadsCount = 0
mCustomQuery = customQuery
mInternetAddress = address
mPort = port
}
@Deprecated("Deprecated in Java")
override fun doInBackground(vararg p0: Activity?): Void? {
mListener.onStart()
val wifi = mContext.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val lock = wifi.createMulticastLock("The Lock")
lock.acquire()
var socket: DatagramSocket? = null
try {
val group = InetAddress.getByName(mInternetAddress)
val port = mPort
val query = mCustomQuery
socket = DatagramSocket(null)
socket.reuseAddress = true
socket.broadcast = true
socket.bind(InetSocketAddress(port))
val datagramPacketRequest = DatagramPacket(query.toByteArray(), query.length, group, port)
socket.send(datagramPacketRequest)
val time = System.currentTimeMillis()
var curTime = System.currentTimeMillis()
while (curTime - time < 1000) {
val datagramPacket = DatagramPacket(ByteArray(1024), 1024)
socket.receive(datagramPacket)
val response = String(datagramPacket.data, 0, datagramPacket.length)
if (response.substring(0, 12).uppercase() == "HTTP/1.1 200") {
val device = UPnPDevice(datagramPacket.address.hostAddress ?: continue, response)
mThreadsCount++
getData(device.location, device)
}
curTime = System.currentTimeMillis()
}
} catch (e: IOException) {
mListener.onError(e)
} finally {
socket?.close()
}
lock.release()
return null
}
private fun getData(
url: String,
device: UPnPDevice,
) {
val stringRequest =
StringRequest(
Request.Method.GET,
url,
{ response ->
device.update(response)
mListener.onFoundNewDevice(device)
devices.add(device)
mThreadsCount--
if (mThreadsCount == 0) {
mListener.onFinish(devices)
}
},
{
mThreadsCount--
Log.e(TAG, "URL: $url get content error!")
},
)
stringRequest.tag = TAG + "SSDP description request"
Volley.newRequestQueue(mContext).add(stringRequest)
}
companion object {
internal val TAG: String = UPnPDiscovery::class.java.simpleName
private const val DISCOVER_TIMEOUT = 1500
private const val LINE_END = "\r\n"
private const val DEFAULT_QUERY =
"M-SEARCH * HTTP/1.1" + LINE_END +
"HOST: 239.255.255.250:1900" + LINE_END +
"MAN: \"ssdp:discover\"" + LINE_END +
"MX: 1" + LINE_END +
"ST: ssdp:all" + LINE_END +
LINE_END
private const val DEFAULT_ADDRESS = "239.255.255.250"
fun discoveryDevices(
activity: Activity,
listener: OnDiscoveryListener,
): Boolean {
val discover = UPnPDiscovery(activity, listener)
discover.execute()
return try {
Thread.sleep(DISCOVER_TIMEOUT.toLong())
true
} catch (e: InterruptedException) {
false
}
}
fun discoveryDevices(
activity: Activity,
listener: OnDiscoveryListener,
customQuery: String,
address: String,
port: Int,
): Boolean {
val discover = UPnPDiscovery(activity, listener, customQuery, address, port)
discover.execute()
return try {
Thread.sleep(DISCOVER_TIMEOUT.toLong())
true
} catch (e: InterruptedException) {
false
}
}
}
}

View file

@ -0,0 +1,10 @@
package io.github.domi04151309.home
import com.google.android.material.color.DynamicColors
class Application : android.app.Application() {
override fun onCreate() {
super.onCreate()
DynamicColors.applyToActivitiesIfAvailable(this)
}
}

View file

@ -0,0 +1,112 @@
package io.github.domi04151309.home.activities
import android.content.Intent
import android.os.Bundle
import androidx.core.net.toUri
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.github.domi04151309.home.BuildConfig
import io.github.domi04151309.home.R
class AboutActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, GeneralPreferenceFragment())
.commit()
}
class GeneralPreferenceFragment : PreferenceFragmentCompat() {
@Suppress("SameReturnValue")
private fun onIconsClicked(): Boolean {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.about_icons)
.setItems(resources.getStringArray(R.array.about_icons_array)) { _, which ->
startActivity(
Intent(
Intent.ACTION_VIEW,
when (which) {
0 -> "https://icons8.com/"
1 -> "https://fonts.google.com/icons?selected=Material+Icons"
else -> "about:blank"
}.toUri(),
),
)
}
.show()
return true
}
@Suppress("SameReturnValue")
private fun onExternalClicked(link: String): Boolean {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.about_privacy)
.setMessage(R.string.about_privacy_desc)
.setPositiveButton(android.R.string.ok) { _, _ ->
startActivity(
Intent(
Intent.ACTION_VIEW,
link.toUri(),
),
)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.setNeutralButton(R.string.about_privacy_policy) { _, _ ->
startActivity(
Intent(
Intent.ACTION_VIEW,
"https://docs.github.com/en/github/site-policy/github-privacy-statement".toUri(),
),
)
}
.show()
return true
}
override fun onCreatePreferences(
savedInstanceState: Bundle?,
rootKey: String?,
) {
addPreferencesFromResource(R.xml.pref_about)
findPreference<Preference>("app_version")?.apply {
summary =
requireContext().getString(
R.string.about_app_version_desc,
BuildConfig.VERSION_NAME,
BuildConfig.VERSION_CODE,
)
setOnPreferenceClickListener {
onExternalClicked("$REPOSITORY_URL/releases")
}
}
findPreference<Preference>("github")?.apply {
summary = REPOSITORY_URL
setOnPreferenceClickListener {
onExternalClicked(REPOSITORY_URL)
}
}
findPreference<Preference>("license")?.setOnPreferenceClickListener {
onExternalClicked("$REPOSITORY_URL/blob/$BRANCH/LICENSE")
}
findPreference<Preference>("icons")?.setOnPreferenceClickListener {
onIconsClicked()
}
findPreference<Preference>("contributors")?.setOnPreferenceClickListener {
onExternalClicked("$REPOSITORY_URL/graphs/contributors")
}
findPreference<Preference>("libraries")?.setOnPreferenceClickListener {
startActivity(Intent(requireContext(), LibraryActivity::class.java))
true
}
}
}
companion object {
private const val REPOSITORY: String = "Domi04151309/HomeApp"
private const val BRANCH: String = "main"
private const val REPOSITORY_URL: String = "https://github.com/$REPOSITORY"
}
}

View file

@ -0,0 +1,22 @@
package io.github.domi04151309.home.activities
import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.elevation.SurfaceColors
import io.github.domi04151309.home.R
abstract class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (resources.configuration.uiMode.and(
Configuration.UI_MODE_NIGHT_MASK,
) != Configuration.UI_MODE_NIGHT_YES
) {
setTheme(R.style.LightStatusBarOverlay)
}
val color = SurfaceColors.SURFACE_2.getColor(this)
window.statusBarColor = color
window.navigationBarColor = color
}
}

View file

@ -0,0 +1,70 @@
package io.github.domi04151309.home.activities
import android.os.Bundle
import com.google.android.material.elevation.SurfaceColors
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.fragments.ControlInfoFragment
import io.github.domi04151309.home.fragments.HueColorFragment
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.interfaces.HueRoomInterface
class ControlInfoActivity : BaseActivity() {
private var hueRoom: ControlInfoActivityHueRoom? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
window.statusBarColor = SurfaceColors.SURFACE_0.getColor(this)
val id = intent.getStringExtra(EXTRA_ID)
if (id === null) {
return
}
val device = Devices(this).getDeviceById(id.substring(0, id.indexOf('@')))
if (device.mode == Global.HUE_API) {
showHueFragment(id, device)
return
}
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, ControlInfoFragment(device, intent.getStringExtra(EXTRA_TITLE) ?: ""))
.commit()
}
override fun onStart() {
super.onStart()
hueRoom?.onStart()
}
override fun onStop() {
super.onStop()
hueRoom?.onStop()
}
override fun onDestroy() {
super.onDestroy()
hueRoom?.onDestroy()
}
private fun showHueFragment(
id: String,
device: DeviceItem,
) {
hueRoom = ControlInfoActivityHueRoom(this, device, id.substring(id.indexOf('@') + 1))
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, HueColorFragment(hueRoom as HueRoomInterface))
.commit()
}
companion object {
const val EXTRA_ID: String = "EXTRA_ID"
const val EXTRA_TITLE: String = "EXTRA_TITLE"
}
}

View file

@ -0,0 +1,97 @@
package io.github.domi04151309.home.activities
import android.content.Context
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.LightStates
import io.github.domi04151309.home.helpers.HueLightListener
import io.github.domi04151309.home.helpers.HueUtils.MIN_COLOR_TEMPERATURE
import io.github.domi04151309.home.helpers.UpdateHandler
import io.github.domi04151309.home.interfaces.HueRoomInterface
import org.json.JSONArray
class ControlInfoActivityHueRoom : HueRoomInterface {
override var lights: JSONArray?
override var lampData: HueLightListener
override var id: String
override var device: DeviceItem
override var addressPrefix: String
override var canReceiveRequest: Boolean
private var updateDataRequest: JsonObjectRequest? = null
private var updateHandler: UpdateHandler = UpdateHandler()
constructor(context: Context, device: DeviceItem, id: String) {
val hueApi = HueAPI(context, device.id)
val queue = Volley.newRequestQueue(context)
this.lights = null
this.lampData = HueLightListener()
this.id = id
this.device = device
this.addressPrefix = device.address + "api/" + hueApi.getUsername()
this.canReceiveRequest = false
updateDataRequest = getUpdateRequest()
updateHandler.setUpdateFunction {
if (canReceiveRequest && hueApi.readyForRequest) {
queue.add(updateDataRequest)
}
}
onStart()
}
fun onStart() {
canReceiveRequest = true
}
fun onStop() {
canReceiveRequest = false
}
fun onDestroy() {
updateHandler.stop()
}
override fun onColorChanged(color: Int) {
// Do nothing.
}
private fun getUpdateRequest() =
JsonObjectRequest(
Request.Method.GET,
"$addressPrefix/groups/$id",
null,
{ response ->
lights = response.getJSONArray("lights")
val action = response.getJSONObject("action")
val light = LightStates.Light()
light.ct =
if (action.has("ct")) {
action.getInt("ct") - MIN_COLOR_TEMPERATURE
} else {
-1
}
if (action.has("hue") && action.has("sat")) {
light.hue = action.getInt("hue")
light.sat = action.getInt("sat")
} else {
light.hue = -1
light.sat = -1
}
light.on = response.getJSONObject("state").getBoolean("any_on")
lampData.state = light
},
{
canReceiveRequest = false
},
)
}

View file

@ -0,0 +1,238 @@
package io.github.domi04151309.home.activities
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.SimpleListAdapter
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.api.HueAPIParser
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.SimpleListItem
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
import org.json.JSONObject
import java.util.Locale
import java.util.concurrent.TimeUnit
@Suppress("TooManyFunctions")
class DeviceInfoActivity : BaseActivity(), RecyclerViewHelperInterface {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_devices)
val devices = Devices(this)
val id = intent.getStringExtra(Devices.INTENT_EXTRA_DEVICE) ?: ""
if (!devices.idExists(id)) {
finish()
return
}
val device = devices.getDeviceById(id)
val queue = Volley.newRequestQueue(this)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
val items = mutableListOf<SimpleListItem>()
recyclerView.layoutManager = LinearLayoutManager(this)
items.add(
SimpleListItem(
device.name,
device.address,
icon = device.iconId,
),
)
when (device.mode) {
Global.HUE_API -> showHueInfo(device, queue, items, recyclerView)
Global.SHELLY_GEN_2 -> showShelly2Info(device, queue, items, recyclerView)
Global.SHELLY_GEN_3 -> showShelly2Info(device, queue, items, recyclerView)
}
}
override fun onItemClicked(
view: View,
position: Int,
) {
// Do nothing.
}
private fun boolToString(bool: Boolean): String =
resources.getString(
if (bool) R.string.str_on else R.string.str_off,
)
@Suppress("MagicNumber")
private fun rssiToPercent(rssi: Int): Int =
if (rssi <= -100) {
0
} else if (rssi >= -50) {
100
} else {
2 * (rssi + 100)
}
private fun formatUptime(uptime: Long) =
String.format(
Locale.getDefault(),
"%02d:%02d:%02d",
TimeUnit.SECONDS.toHours(uptime),
TimeUnit.SECONDS.toMinutes(uptime) -
TimeUnit.HOURS.toMinutes(
TimeUnit.SECONDS.toHours(
uptime,
),
),
TimeUnit.SECONDS.toSeconds(uptime) -
TimeUnit.MINUTES.toSeconds(
TimeUnit.SECONDS.toMinutes(
uptime,
),
),
)
private fun showHueInfo(
device: DeviceItem,
queue: RequestQueue,
items: MutableList<SimpleListItem>,
recyclerView: RecyclerView,
) {
val hueAPI = HueAPI(this, device.id)
val addressPrefix = device.address + "api/" + hueAPI.getUsername()
queue.add(
JsonObjectRequest(
Request.Method.GET,
"$addressPrefix/config",
null,
{ response ->
items.addAll(HueAPIParser.parseHueConfig(resources, response))
queue.add(
JsonObjectRequest(
Request.Method.GET,
"$addressPrefix/sensors",
null,
{ innerResponse ->
items.addAll(HueAPIParser.parseHueSensors(resources, innerResponse))
queue.add(
JsonObjectRequest(
Request.Method.GET,
"$addressPrefix/lights",
null,
{ innerInnerResponse ->
items.addAll(HueAPIParser.parseHueLights(resources, innerInnerResponse))
recyclerView.adapter = SimpleListAdapter(items, this)
},
{ },
),
)
},
{ },
),
)
},
{ },
),
)
}
@Suppress("LongMethod")
private fun parseShelly2Info(response: JSONObject) =
listOf(
SimpleListItem(summary = resources.getString(R.string.device_config_info_status)),
SimpleListItem(
(response.optJSONObject("wifi") ?: JSONObject()).run {
optString("ssid") + " (" + rssiToPercent(optInt("rssi")) + " %)"
},
resources.getString(R.string.shelly_wifi),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
boolToString(
(
response.optJSONObject("mqtt")
?: JSONObject()
).optBoolean("connected"),
),
resources.getString(R.string.shelly_mqtt),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
boolToString(
(
response.optJSONObject("cloud")
?: JSONObject()
).optBoolean("connected"),
),
resources.getString(R.string.shelly_cloud),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
formatUptime((response.optJSONObject("sys") ?: JSONObject()).optLong("uptime")),
resources.getString(R.string.shelly_uptime),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
(response.optJSONObject("sys") ?: JSONObject()).run {
"${(optInt("fs_free") / optInt("fs_size").toFloat() * TO_PERCENT).toInt()} %"
},
resources.getString(R.string.shelly_storage),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
(response.optJSONObject("sys") ?: JSONObject()).run {
"${(optInt("ram_free") / optInt("ram_size").toFloat() * TO_PERCENT).toInt()} %"
},
resources.getString(R.string.shelly_ram),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
resources.getString(
if ((
(
response.optJSONObject("sys")
?: JSONObject()
).optJSONObject("available_updates")
?: JSONObject()
).has("stable")
) {
R.string.str_yes
} else {
R.string.str_no
},
),
resources.getString(R.string.shelly_update),
icon = R.drawable.ic_about_info,
),
)
private fun showShelly2Info(
device: DeviceItem,
queue: RequestQueue,
items: MutableList<SimpleListItem>,
recyclerView: RecyclerView,
) {
queue.add(
JsonObjectRequest(
Request.Method.GET,
device.address + "rpc/Shelly.GetStatus",
null,
{ response ->
items.addAll(parseShelly2Info(response))
recyclerView.adapter = SimpleListAdapter(items, this)
},
{ },
),
)
}
companion object {
private const val TO_PERCENT = 100
}
}

View file

@ -0,0 +1,152 @@
package io.github.domi04151309.home.activities
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.DeviceListAdapter
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.SimpleListItem
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterfaceAdvanced
class DevicesActivity : BaseActivity(), RecyclerViewHelperInterfaceAdvanced {
private var reset = true
private lateinit var devices: Devices
private lateinit var recyclerView: RecyclerView
private lateinit var itemTouchHelper: ItemTouchHelper
private val itemTouchHelperCallback =
object : ItemTouchHelper.Callback() {
override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
): Int =
if (
viewHolder.adapterPosition == (recyclerView.adapter?.itemCount ?: -1) - 1
) {
makeMovementFlags(0, 0)
} else {
makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0)
}
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder,
): Boolean {
val adapter = recyclerView.adapter ?: return false
return if (target.adapterPosition == adapter.itemCount - 1) {
false
} else {
recyclerView.adapter?.notifyItemMoved(
viewHolder.adapterPosition,
target.adapterPosition,
)
devices.moveDevice(viewHolder.adapterPosition, target.adapterPosition)
true
}
}
override fun isLongPressDragEnabled(): Boolean = true
override fun onSwiped(
viewHolder: RecyclerView.ViewHolder,
direction: Int,
) {
// Do nothing.
}
override fun clearView(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
) {
super.clearView(recyclerView, viewHolder)
devices.saveChanges()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_devices)
devices = Devices(this)
recyclerView = findViewById(R.id.recyclerView)
itemTouchHelper = ItemTouchHelper(itemTouchHelperCallback)
itemTouchHelper.attachToRecyclerView(recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
}
private fun loadDevices() {
val listItems: ArrayList<SimpleListItem> = ArrayList(devices.length)
var currentDevice: DeviceItem
for (i in 0 until devices.length) {
currentDevice = devices.getDeviceByIndex(i)
listItems +=
SimpleListItem(
title = currentDevice.name,
summary =
if (currentDevice.hide) {
resources.getString(R.string.device_config_hidden) + " · " + currentDevice.address
} else {
currentDevice.address
},
hidden = "edit#${currentDevice.id}",
icon = currentDevice.iconId,
)
}
listItems +=
SimpleListItem(
title = resources.getString(R.string.pref_add),
summary = resources.getString(R.string.pref_add_summary),
hidden = "add",
icon = R.drawable.ic_add,
)
recyclerView.adapter = DeviceListAdapter(listItems, this)
}
override fun onItemClicked(
view: View,
position: Int,
) {
val action = view.findViewById<TextView>(R.id.hidden).text
if (action.contains("edit")) {
reset = true
startActivity(
Intent(this, EditDeviceActivity::class.java)
.putExtra("deviceId", action.substring(action.indexOf('#') + 1)),
)
} else if (action == "add") {
reset = true
MaterialAlertDialogBuilder(this)
.setTitle(R.string.pref_add_method)
.setItems(resources.getStringArray(R.array.pref_add_method_array)) { _, which ->
if (which == 0) {
startActivity(Intent(this, EditDeviceActivity::class.java))
} else if (which == 1) {
startActivity(Intent(this, SearchDevicesActivity::class.java))
}
}
.show()
}
}
override fun onItemHandleTouched(viewHolder: RecyclerView.ViewHolder) {
itemTouchHelper.startDrag(viewHolder)
}
override fun onStart() {
super.onStart()
if (reset) {
reset = false
loadDevices()
}
}
}

View file

@ -0,0 +1,387 @@
package io.github.domi04151309.home.activities
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Button
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.core.net.toUri
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.elevation.SurfaceColors
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.textfield.TextInputLayout
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.IconSpinnerAdapter
import io.github.domi04151309.home.custom.TextWatcher
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.helpers.DeviceSecrets
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
class EditDeviceActivity : BaseActivity() {
private lateinit var devices: Devices
private lateinit var deviceId: String
private lateinit var deviceSecrets: DeviceSecrets
private lateinit var deviceIcon: ImageView
private lateinit var nameText: TextView
private lateinit var nameBox: TextInputLayout
private lateinit var addressBox: TextInputLayout
private lateinit var iconSpinner: AutoCompleteTextView
private lateinit var modeSpinner: AutoCompleteTextView
private lateinit var specialDivider: View
private lateinit var specialSection: LinearLayout
private lateinit var usernameBox: TextInputLayout
private lateinit var passwordBox: TextInputLayout
private lateinit var configHide: CheckBox
private lateinit var configDirectView: CheckBox
private lateinit var configButton: Button
private lateinit var infoButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_edit_device)
window.statusBarColor = SurfaceColors.SURFACE_0.getColor(this)
devices = Devices(this)
var deviceId = intent.getStringExtra("deviceId")
val editing =
if (deviceId == null) {
deviceId = devices.generateNewId()
false
} else {
true
}
this.deviceId = deviceId
deviceSecrets = DeviceSecrets(this, deviceId)
deviceIcon = findViewById(R.id.deviceIcn)
nameText = findViewById(R.id.nameTxt)
nameBox = findViewById(R.id.nameBox)
addressBox = findViewById(R.id.addressBox)
iconSpinner = findViewById<TextInputLayout>(R.id.iconSpinner).editText as AutoCompleteTextView
modeSpinner = findViewById<TextInputLayout>(R.id.modeSpinner).editText as AutoCompleteTextView
specialDivider = findViewById(R.id.specialDivider)
specialSection = findViewById(R.id.specialSection)
usernameBox = findViewById(R.id.usernameBox)
passwordBox = findViewById(R.id.passwordBox)
configHide = findViewById(R.id.configHide)
configDirectView = findViewById(R.id.configDirectView)
configButton = findViewById(R.id.configBtn)
infoButton = findViewById(R.id.infoBtn)
findViewById<TextView>(R.id.idTxt).text = resources.getString(R.string.pref_add_id, deviceId)
iconSpinner.addTextChangedListener(getIconTextWatcher())
modeSpinner.addTextChangedListener(getModeTextWatcher(editing))
nameBox.editText?.addTextChangedListener(getNameTextWatcher())
if (editing) {
onEditDevice()
} else {
onCreateDevice()
}
iconSpinner.setAdapter(IconSpinnerAdapter(resources.getStringArray(R.array.pref_icons)))
modeSpinner.setAdapter(
ArrayAdapter(
this,
R.layout.dropdown_item,
resources.getStringArray(R.array.pref_add_mode_array),
),
)
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener {
onFloatingActionButtonClicked()
}
findViewById<MaterialToolbar>(R.id.toolbar).apply {
setNavigationIcon(R.drawable.ic_arrow_back)
setNavigationOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
}
}
private fun getIconTextWatcher() =
TextWatcher {
deviceIcon.setImageResource(Global.getIcon(it))
}
private fun showExternalInfoBasedOnMode(mode: String) {
configButton.visibility =
if (HAS_CONFIG.contains(mode)) {
View.VISIBLE
} else {
View.GONE
}
infoButton.visibility =
if (HAS_INFO.contains(mode)) {
View.VISIBLE
} else {
View.GONE
}
}
@Suppress("ComplexCondition")
private fun getModeTextWatcher(editing: Boolean) =
TextWatcher {
val specialVisibility =
if (
it == Global.FRITZ_AUTO_LOGIN ||
it == Global.GRAFANA_AUTO_LOGIN ||
it == Global.PI_HOLE_AUTO_LOGIN ||
it == Global.SHELLY_GEN_1
) {
View.VISIBLE
} else {
View.GONE
}
val usernameVisibility =
if (
it == Global.GRAFANA_AUTO_LOGIN ||
it == Global.SHELLY_GEN_1
) {
View.VISIBLE
} else {
View.GONE
}
specialDivider.visibility = specialVisibility
specialSection.visibility = specialVisibility
usernameBox.visibility = usernameVisibility
if (SUPPORTS_DIRECT_VIEW.contains(it)) {
configDirectView.isEnabled = true
} else {
configDirectView.isEnabled = false
configDirectView.isChecked = false
}
if (editing) {
showExternalInfoBasedOnMode(it)
}
}
private fun getNameTextWatcher() =
TextWatcher {
if (it == "") {
nameText.text = resources.getString(R.string.pref_add_name_empty)
} else {
nameText.text = it
}
}
private fun onEditDevice() {
val device = devices.getDeviceById(deviceId)
nameBox.editText?.setText(device.name)
addressBox.editText?.setText(device.address)
iconSpinner.setText(device.iconName)
modeSpinner.setText(device.mode)
usernameBox.editText?.setText(deviceSecrets.username)
passwordBox.editText?.setText(deviceSecrets.password)
configHide.isChecked = device.hide
configDirectView.isChecked = device.directView
configButton.setOnClickListener {
onConfigButtonClicked()
}
infoButton.setOnClickListener {
startActivity(Intent(this, DeviceInfoActivity::class.java).putExtra(Devices.INTENT_EXTRA_DEVICE, deviceId))
}
findViewById<Button>(R.id.shortcutBtn).setOnClickListener {
onShortcutButtonClicked(device)
}
findViewById<Button>(R.id.deleteBtn).setOnClickListener {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.str_delete)
.setMessage(R.string.pref_delete_device_question)
.setPositiveButton(R.string.str_delete) { _, _ ->
devices.deleteDevice(deviceId)
finish()
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
}
}
private fun onConfigButtonClicked() {
when (modeSpinner.text.toString()) {
Global.ESP_EASY, Global.SHELLY_GEN_1, Global.SHELLY_GEN_2, Global.SHELLY_GEN_3 -> {
startActivity(
Intent(this, WebActivity::class.java)
.putExtra("URI", addressBox.editText?.text.toString())
.putExtra("title", resources.getString(R.string.pref_device_config)),
)
}
Global.NODE_RED -> {
startActivity(
Intent(this, WebActivity::class.java)
.putExtra("URI", formatNodeREDAddress(addressBox.editText?.text.toString()))
.putExtra("title", resources.getString(R.string.pref_device_config)),
)
}
Global.HUE_API -> {
val huePackageName = "com.philips.lighting.hue2"
val launchIntent = packageManager.getLaunchIntentForPackage(huePackageName)
if (launchIntent == null) {
try {
startActivity(
Intent(
Intent.ACTION_VIEW,
"market://details?id=$huePackageName".toUri(),
),
)
} catch (e: ActivityNotFoundException) {
Log.w(EditDeviceActivity::class.simpleName, e)
startActivity(
Intent(
Intent.ACTION_VIEW,
"https://play.google.com/store/apps/details?id=$huePackageName".toUri(),
),
)
}
} else {
startActivity(launchIntent)
}
}
}
}
private fun onShortcutButtonClicked(device: DeviceItem) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val shortcutManager = this.getSystemService(ShortcutManager::class.java)
if (shortcutManager != null) {
if (shortcutManager.isRequestPinShortcutSupported) {
val shortcut =
ShortcutInfo.Builder(this, deviceId)
.setShortLabel(
device.name.ifEmpty {
resources.getString(R.string.pref_add_name_empty)
},
)
.setLongLabel(
device.name.ifEmpty {
resources.getString(R.string.pref_add_name_empty)
},
)
.setIcon(Icon.createWithResource(this, device.iconId))
.setIntent(
Intent(this, MainActivity::class.java)
.putExtra(Devices.INTENT_EXTRA_DEVICE, deviceId)
.setAction(Intent.ACTION_MAIN)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK),
)
.build()
shortcutManager.requestPinShortcut(shortcut, null)
} else {
Toast.makeText(this, R.string.pref_add_shortcut_failed, Toast.LENGTH_LONG).show()
}
}
} else {
Toast.makeText(this, R.string.pref_add_shortcut_failed, Toast.LENGTH_LONG).show()
}
}
private fun onCreateDevice() {
iconSpinner.setText(resources.getStringArray(R.array.pref_icons)[0])
modeSpinner.setText(resources.getStringArray(R.array.pref_add_mode_array)[0])
findViewById<View>(R.id.editDivider).visibility = View.GONE
findViewById<LinearLayout>(R.id.editSection).visibility = View.GONE
}
private fun onFloatingActionButtonClicked() {
val name = nameBox.editText?.text.toString()
if (name == "") {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.err_missing_name)
.setMessage(R.string.err_missing_name_summary)
.setPositiveButton(android.R.string.ok) { _, _ -> }
.show()
return
} else if (addressBox.editText?.text.toString() == "") {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.err_missing_address)
.setMessage(R.string.err_missing_address_summary)
.setPositiveButton(android.R.string.ok) { _, _ -> }
.show()
return
}
val tempAddress =
if (modeSpinner.text.toString() == Global.NODE_RED) {
formatNodeREDAddress(addressBox.editText?.text.toString())
} else {
addressBox.editText?.text.toString()
}
val newItem =
DeviceItem(
deviceId,
name,
modeSpinner.text.toString(),
iconSpinner.text.toString(),
configHide.isChecked,
configDirectView.isChecked,
)
newItem.address = tempAddress
devices.addDevice(newItem)
deviceSecrets.username = usernameBox.editText?.text.toString()
deviceSecrets.password = passwordBox.editText?.text.toString()
deviceSecrets.updateDeviceSecrets()
finish()
}
private fun formatNodeREDAddress(url: String): String {
var result = url
if (!result.contains(":1880")) {
if (result.endsWith('/')) result = result.dropLast(1)
result += ":1880/"
}
return result
}
companion object {
private val SUPPORTS_DIRECT_VIEW =
arrayOf(
Global.ESP_EASY,
Global.HUE_API,
Global.SHELLY_GEN_1,
Global.SHELLY_GEN_2,
Global.SHELLY_GEN_3,
Global.SIMPLE_HOME_API,
Global.TASMOTA,
)
private val HAS_CONFIG =
arrayOf(
Global.HUE_API,
Global.ESP_EASY,
Global.NODE_RED,
Global.SHELLY_GEN_1,
Global.SHELLY_GEN_2,
Global.SHELLY_GEN_3,
)
private val HAS_INFO =
arrayOf(
Global.HUE_API,
Global.SHELLY_GEN_2,
Global.SHELLY_GEN_3,
)
}
}

View file

@ -0,0 +1,76 @@
package io.github.domi04151309.home.activities
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.Volley
import io.github.domi04151309.home.R
import io.github.domi04151309.home.custom.CustomJsonArrayRequest
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.UpdateHandler
import org.json.JSONObject
class HueConnectActivity : BaseActivity() {
private val updateHandler = UpdateHandler()
private var success = false
private lateinit var queue: RequestQueue
private lateinit var requestToRegisterUser: CustomJsonArrayRequest
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_hue_connect)
queue = Volley.newRequestQueue(this)
val deviceId = intent.getStringExtra("deviceId") ?: ""
val jsonRequestObject = JSONObject("""{ "devicetype": "Home App#${android.os.Build.PRODUCT}" }""")
requestToRegisterUser =
CustomJsonArrayRequest(
Request.Method.POST, Devices(this).getDeviceById(deviceId).address + "api", jsonRequestObject,
{ response ->
val responseObject = response.getJSONObject(0)
if (responseObject.has("success") && !success) {
success = true
val username = responseObject.getJSONObject("success").getString("username")
PreferenceManager.getDefaultSharedPreferences(this).edit {
putString(
deviceId,
username,
)
}
startActivity(
Intent(this, MainActivity::class.java)
.putExtra(Devices.INTENT_EXTRA_DEVICE, deviceId)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK),
)
}
},
{ error ->
Toast.makeText(this, R.string.err, Toast.LENGTH_LONG).show()
Log.e(Global.LOG_TAG, error.toString())
},
)
findViewById<Button>(R.id.cancel_btn).setOnClickListener {
finish()
}
}
override fun onStart() {
super.onStart()
updateHandler.setUpdateFunction {
if (!success) queue.add(requestToRegisterUser)
}
}
override fun onStop() {
super.onStop()
updateHandler.stop()
}
}

View file

@ -0,0 +1,266 @@
package io.github.domi04151309.home.activities
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Icon
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.core.content.res.ResourcesCompat
import androidx.core.widget.ImageViewCompat
import androidx.viewpager2.widget.ViewPager2
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.elevation.SurfaceColors
import com.google.android.material.slider.Slider
import com.google.android.material.tabs.TabLayoutMediator
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.HueDetailsTabAdapter
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.LightStates
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.HueLightListener
import io.github.domi04151309.home.helpers.HueUtils
import io.github.domi04151309.home.helpers.HueUtils.MIN_COLOR_TEMPERATURE
import io.github.domi04151309.home.helpers.SliderUtils
import io.github.domi04151309.home.helpers.UpdateHandler
import io.github.domi04151309.home.interfaces.HueRoomInterface
import org.json.JSONArray
class HueLampActivity : BaseActivity(), HueRoomInterface, Toolbar.OnMenuItemClickListener {
override var addressPrefix: String = ""
override var id: String = ""
override var lights: JSONArray? = null
override var canReceiveRequest: Boolean = false
override var lampData: HueLightListener = HueLightListener()
override lateinit var device: DeviceItem
private var lampName: String = ""
private var updateDataRequest: JsonObjectRequest? = null
private var updateHandler: UpdateHandler = UpdateHandler()
private lateinit var hueAPI: HueAPI
private lateinit var queue: RequestQueue
private lateinit var lampIcon: ImageView
private lateinit var nameText: TextView
private lateinit var brightnessText: TextView
private lateinit var brightnessBar: Slider
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_hue_lamp)
window.statusBarColor = SurfaceColors.SURFACE_0.getColor(this)
id = intent.getStringExtra("id") ?: "0"
if (intent.hasExtra(Devices.INTENT_EXTRA_DEVICE)) {
val extraDevice = intent.getStringExtra(Devices.INTENT_EXTRA_DEVICE) ?: ""
val devices = Devices(this)
if (devices.idExists(extraDevice)) {
device = devices.getDeviceById(extraDevice)
} else {
Toast.makeText(this, R.string.main_device_nonexistent, Toast.LENGTH_LONG).show()
finish()
return
}
}
hueAPI = HueAPI(this, device.id)
addressPrefix = device.address + "api/" + hueAPI.getUsername()
queue = Volley.newRequestQueue(this)
lampIcon = findViewById(R.id.lampIcon)
nameText = findViewById(R.id.nameTxt)
brightnessText = findViewById(R.id.briTxt)
brightnessBar = findViewById(R.id.briBar)
setupViews()
val viewPager = findViewById<ViewPager2>(R.id.viewPager)
viewPager.isUserInputEnabled = false
viewPager.adapter = HueDetailsTabAdapter(this, this)
viewPager.setCurrentItem(1, false)
val tabIcons =
arrayOf(
ResourcesCompat.getDrawable(resources, R.drawable.ic_color_palette, theme),
ResourcesCompat.getDrawable(resources, R.drawable.ic_scene_white, theme),
ResourcesCompat.getDrawable(resources, R.drawable.ic_device_lamp, theme),
)
TabLayoutMediator(findViewById(R.id.tabBar), viewPager) { tab, position ->
tab.icon = tabIcons[position]
}.attach()
updateDataRequest = getUpdateRequest()
updateHandler.setUpdateFunction {
if (canReceiveRequest && hueAPI.readyForRequest) {
queue.add(updateDataRequest)
}
}
findViewById<MaterialToolbar>(R.id.toolbar).apply {
setNavigationIcon(R.drawable.ic_arrow_back)
setNavigationOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
inflateMenu(R.menu.activity_hue_lamp_actions)
setOnMenuItemClickListener(this@HueLampActivity)
}
}
private fun setupViews() {
// Slider labels
brightnessBar.setLabelFormatter { value: Float ->
HueUtils.briToPercent(value.toInt())
}
// Lamp tint
ImageViewCompat.setImageTintList(
lampIcon,
ColorStateList.valueOf(Color.WHITE),
)
lampData.addOnDataChangedListener {
ImageViewCompat.setImageTintList(
lampIcon,
ColorStateList.valueOf(
if (it.hue != -1 && it.sat != -1) {
HueUtils.hueSatToRGB(it.hue, it.sat)
} else if (it.ct != -1) {
HueUtils.ctToRGB(it.ct + MIN_COLOR_TEMPERATURE)
} else {
Color.WHITE
},
),
)
}
findViewById<Button>(R.id.onBtn).setOnClickListener {
hueAPI.switchGroupById(id, true)
}
findViewById<Button>(R.id.offBtn).setOnClickListener {
hueAPI.switchGroupById(id, false)
}
brightnessBar.addOnSliderTouchListener(
object : Slider.OnSliderTouchListener {
override fun onStartTrackingTouch(slider: Slider) {
canReceiveRequest = false
}
override fun onStopTrackingTouch(slider: Slider) {
hueAPI.changeBrightnessOfGroup(id, slider.value.toInt())
canReceiveRequest = true
}
},
)
}
private fun getUpdateRequest() =
JsonObjectRequest(
Request.Method.GET,
"$addressPrefix/groups/$id",
null,
{ response ->
lights = response.getJSONArray("lights")
lampName = response.getString("name")
nameText.text = lampName
val action = response.getJSONObject("action")
val light = LightStates.Light()
if (action.has("bri")) {
SliderUtils.setProgress(brightnessBar, action.getInt("bri"))
} else {
brightnessText.visibility = View.GONE
brightnessBar.visibility = View.GONE
}
light.ct =
if (action.has("ct")) {
action.getInt("ct") - MIN_COLOR_TEMPERATURE
} else {
-1
}
if (action.has("hue") && action.has("sat")) {
light.hue = action.getInt("hue")
light.sat = action.getInt("sat")
} else {
light.hue = -1
light.sat = -1
}
light.on = response.getJSONObject("state").getBoolean("any_on")
brightnessBar.isEnabled = light.on
lampData.state = light
},
{
canReceiveRequest = false
updateHandler.stop()
finish()
},
)
override fun onStart() {
super.onStart()
canReceiveRequest = true
}
override fun onStop() {
super.onStop()
canReceiveRequest = false
}
override fun onDestroy() {
super.onDestroy()
updateHandler.stop()
}
override fun onColorChanged(color: Int) {
ImageViewCompat.setImageTintList(
lampIcon,
ColorStateList.valueOf(color),
)
}
@Suppress("ReturnCount")
override fun onMenuItemClick(item: MenuItem): Boolean {
if (item.itemId != R.id.action_add_shortcut) return super.onOptionsItemSelected(item)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val shortcutManager = getSystemService(ShortcutManager::class.java) ?: return true
if (!shortcutManager.isRequestPinShortcutSupported) {
Toast.makeText(this, R.string.pref_add_shortcut_failed, Toast.LENGTH_LONG).show()
return true
}
val shortcut =
ShortcutInfo.Builder(this, device.id + lampName)
.setShortLabel(lampName)
.setLongLabel(lampName)
.setIcon(Icon.createWithResource(this, device.iconId))
.setIntent(
Intent(this, HueLampActivity::class.java)
.putExtra("id", id)
.putExtra(Devices.INTENT_EXTRA_DEVICE, device.id)
.setAction(Intent.ACTION_MAIN)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK),
)
.build()
shortcutManager.requestPinShortcut(shortcut, null)
} else {
Toast.makeText(this, R.string.pref_add_shortcut_failed, Toast.LENGTH_LONG).show()
}
return true
}
}

View file

@ -0,0 +1,362 @@
package io.github.domi04151309.home.activities
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.VolleyError
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.elevation.SurfaceColors
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.slider.Slider
import com.google.android.material.textfield.TextInputLayout
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.HueSceneLampListAdapter
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.custom.CustomJsonArrayRequest
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.LightStates
import io.github.domi04151309.home.data.SceneListItem
import io.github.domi04151309.home.fragments.HueColorSheet
import io.github.domi04151309.home.fragments.HueScenesFragment
import io.github.domi04151309.home.helpers.ColorUtils
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.HueUtils
import io.github.domi04151309.home.helpers.HueUtils.MAX_BRIGHTNESS
import io.github.domi04151309.home.helpers.SliderUtils
import io.github.domi04151309.home.interfaces.HueAdvancedLampInterface
import io.github.domi04151309.home.interfaces.SceneRecyclerViewHelperInterface
import org.json.JSONArray
import org.json.JSONObject
@Suppress("TooManyFunctions")
class HueSceneActivity :
BaseActivity(),
SceneRecyclerViewHelperInterface,
HueAdvancedLampInterface,
Response.Listener<JSONArray>,
Response.ErrorListener {
private var editing = false
private val lightStates = LightStates()
private val listItems = mutableListOf<SceneListItem>()
private var groupId = "0"
private var sceneId = ""
private var defaultText = ""
private lateinit var hueAPI: HueAPI
private lateinit var adapter: HueSceneLampListAdapter
private lateinit var queue: RequestQueue
private lateinit var nameBox: TextInputLayout
private lateinit var briBar: Slider
override var id: String = ""
override var canReceiveRequest: Boolean = true
override lateinit var device: DeviceItem
override lateinit var addressPrefix: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_hue_scene)
window.statusBarColor = SurfaceColors.SURFACE_0.getColor(this)
val nameTxt = findViewById<TextView>(R.id.nameTxt)
device = Devices(this).getDeviceById(intent.getStringExtra("deviceId") ?: "")
hueAPI = HueAPI(this, device.id)
addressPrefix = device.address +
"api/" + hueAPI.getUsername()
queue = Volley.newRequestQueue(this)
nameBox = findViewById(R.id.nameBox)
briBar = findViewById(R.id.briBar)
editing = intent.hasExtra("scene")
adapter = HueSceneLampListAdapter(listItems, this)
groupId = intent.getStringExtra("room") ?: "0"
sceneId = intent.getStringExtra("scene") ?: ""
findViewById<RecyclerView>(R.id.recyclerView).apply {
layoutManager = LinearLayoutManager(this@HueSceneActivity)
adapter = this@HueSceneActivity.adapter
}
briBar.setLabelFormatter { value: Float ->
HueUtils.briToPercent(value.toInt())
}
if (editing) {
onEditScene()
} else {
onCreateScene()
}
nameBox.editText?.addTextChangedListener(
object : TextWatcher {
override fun afterTextChanged(s: Editable) {
// Do nothing.
}
override fun beforeTextChanged(
s: CharSequence,
start: Int,
count: Int,
after: Int,
) {
// Do nothing.
}
override fun onTextChanged(
s: CharSequence,
start: Int,
before: Int,
count: Int,
) {
val string = s.toString()
if (string == "") {
nameTxt.text = defaultText
} else {
nameTxt.text = string
}
}
},
)
briBar.addOnSliderTouchListener(
object : Slider.OnSliderTouchListener {
override fun onStartTrackingTouch(slider: Slider) {
// Do nothing.
}
override fun onStopTrackingTouch(slider: Slider) {
hueAPI.changeBrightnessOfGroup(groupId, slider.value.toInt())
adapter.changeSceneBrightness(HueUtils.briToPercent(slider.value.toInt()))
lightStates.setSceneBrightness(slider.value.toInt())
}
},
)
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener {
onFloatingActionButtonClicked()
}
findViewById<MaterialToolbar>(R.id.toolbar).apply {
setNavigationIcon(R.drawable.ic_arrow_back)
setNavigationOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
}
}
private fun onEditScene() {
defaultText = resources.getString(R.string.hue_scene)
hueAPI.activateSceneOfGroup(groupId, sceneId)
queue.add(
JsonObjectRequest(
Request.Method.GET,
"$addressPrefix/scenes/$sceneId",
null,
{ response ->
queue.add(
JsonObjectRequest(
Request.Method.GET,
"$addressPrefix/lights",
null,
{ secondResponse ->
nameBox.editText?.setText(response.optString("name"))
val lights =
response.optJSONObject("lightstates") ?: JSONObject()
var lightObj: JSONObject
val brightness = Array(2) { 0 }
for (i in lights.keys()) {
lightObj = lights.getJSONObject(i)
lightStates.addLight(i, lightObj)
listItems +=
generateListItem(
i,
(secondResponse.optJSONObject(i) ?: JSONObject())
.optString("name"),
lightObj,
)
if (lightObj.has("bri")) {
brightness[0] += lightObj.getInt("bri")
brightness[1]++
}
}
SliderUtils.setProgress(
briBar,
if (brightness[1] > 0) brightness[0] / brightness[1] else 0,
)
listItems.sortBy { it.title }
adapter.notifyDataSetChanged()
},
this,
),
)
},
this,
),
)
}
private fun onCreateScene() {
defaultText = resources.getString(R.string.hue_new_scene)
queue.add(
JsonObjectRequest(
Request.Method.GET,
"$addressPrefix/groups/$groupId",
null,
{ response ->
val lightIds = response.getJSONArray("lights")
SliderUtils.setProgress(
briBar,
(response.optJSONObject("action") ?: JSONObject()).optInt("bri"),
)
queue.add(
JsonObjectRequest(
Request.Method.GET,
"$addressPrefix/lights",
null,
{ secondResponse ->
var lightObj: JSONObject
for (i in 0 until lightIds.length()) {
lightObj =
secondResponse.getJSONObject(lightIds.getString(i))
val state = lightObj.getJSONObject("state")
listItems +=
generateListItem(
lightIds.getString(i),
lightObj.getString("name"),
state,
)
}
listItems.sortBy { it.title }
adapter.notifyDataSetChanged()
},
this,
),
)
},
this,
),
)
}
private fun generateListItem(
id: String,
title: String,
state: JSONObject,
): SceneListItem =
SceneListItem(
title,
id,
state.optBoolean("on"),
HueUtils.briToPercent(state.optInt("bri", MAX_BRIGHTNESS)),
if (state.has("xy")) {
val xyArray = state.getJSONArray("xy")
ColorUtils.xyToRGB(
xyArray.getDouble(0),
xyArray.getDouble(1),
)
} else if (state.has("hue") && state.has("sat")) {
HueUtils.hueSatToRGB(state.getInt("hue"), state.getInt("sat"))
} else if (state.has("ct")) {
HueUtils.ctToRGB(state.getInt("ct"))
} else {
"#FFFFFF".toColorInt()
},
)
private fun onFloatingActionButtonClicked() {
val name = nameBox.editText?.text.toString()
if (name == "") {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.err_missing_name)
.setMessage(R.string.err_missing_name_summary)
.setPositiveButton(android.R.string.ok) { _, _ -> }
.show()
return
}
queue.add(
if (editing) {
CustomJsonArrayRequest(
Request.Method.PUT,
"$addressPrefix/scenes/$sceneId",
JSONObject("""{ "name": "$name", "lightstates": $lightStates }"""),
this,
this,
)
} else {
CustomJsonArrayRequest(
Request.Method.POST,
"$addressPrefix/scenes",
JSONObject(
"""{ "name": "$name", "recycle": false, "group": "$groupId", "type": "GroupScene" }""",
),
this,
this,
)
},
)
}
override fun onResponse(response: JSONArray) {
HueScenesFragment.scenesChanged = true
finish()
}
override fun onErrorResponse(error: VolleyError) {
Toast.makeText(this, Global.volleyError(this, error), Toast.LENGTH_LONG).show()
Log.e(Global.LOG_TAG, error.toString())
}
override fun onItemClicked(
view: View,
data: SceneListItem,
) {
id = data.hidden
HueColorSheet(this).show(supportFragmentManager, HueColorSheet::class.simpleName)
}
override fun onStateChanged(
view: View,
data: SceneListItem,
state: Boolean,
) {
hueAPI.switchLightById(data.hidden, state)
lightStates.switchLight(data.hidden, state)
}
override fun onColorChanged(color: Int) {
adapter.updateColor(id, color)
}
override fun onBrightnessChanged(brightness: Int) {
lightStates.setLightBrightness(id, brightness)
adapter.updateBrightness(id, HueUtils.briToPercent(brightness))
}
override fun onHueSatChanged(
hue: Int,
sat: Int,
) {
lightStates.setLightHue(id, hue)
lightStates.setLightSat(id, sat)
}
override fun onCtChanged(ct: Int) {
lightStates.setLightCt(id, ct)
}
}

View file

@ -0,0 +1,45 @@
package io.github.domi04151309.home.activities
import android.os.Bundle
import androidx.core.content.res.ResourcesCompat
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import io.github.domi04151309.home.R
class LibraryActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, GeneralPreferenceFragment())
.commit()
}
class GeneralPreferenceFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(
savedInstanceState: Bundle?,
rootKey: String?,
) {
addPreferencesFromResource(R.xml.pref_about_list)
preferenceScreen.removeAll()
val libraries = resources.getStringArray(R.array.about_libraries)
val licenses = resources.getStringArray(R.array.about_libraries_licenses)
if (libraries.size != licenses.size) error("Library array size does not match license array size.")
for (index in libraries.indices) {
preferenceScreen.addPreference(
Preference(requireContext()).apply {
icon =
ResourcesCompat.getDrawable(
requireContext().resources,
R.drawable.ic_about_library,
requireContext().theme,
)
title = libraries[index]
summary = licenses[index]
},
)
}
}
}
}

View file

@ -0,0 +1,616 @@
package io.github.domi04151309.home.activities
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.DisplayMetrics
import android.view.ContextMenu
import android.view.Gravity
import android.view.MenuItem
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.FrameLayout
import android.widget.ImageSwitcher
import android.widget.ImageView
import android.widget.TextSwitcher
import android.widget.TextView
import android.widget.Toast
import androidx.activity.addCallback
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.elevation.SurfaceColors
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.MainListAdapter
import io.github.domi04151309.home.api.UnifiedAPI
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.Global.checkNetwork
import io.github.domi04151309.home.helpers.P
import io.github.domi04151309.home.helpers.TasmotaHelper
import io.github.domi04151309.home.helpers.UpdateHandler
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
import kotlin.math.max
import kotlin.math.min
@Suppress("TooManyFunctions")
class MainActivity : BaseActivity() {
private var tasmotaPosition: Int = 0
private var shouldReset: Boolean = false
private val updateHandler = UpdateHandler()
private var isDeviceSelected = false
private var canReceiveRequest = false
private var currentView: View? = null
internal lateinit var devices: Devices
internal lateinit var adapter: MainListAdapter
private lateinit var deviceIcon: ImageSwitcher
private lateinit var deviceName: TextSwitcher
private lateinit var fab: FloatingActionButton
private var columns: Int? = null
/*
* Unified callbacks
*/
private var unified: UnifiedAPI? = null
private val unifiedRequestCallback =
object : UnifiedAPI.CallbackInterface {
override fun onItemsLoaded(
holder: UnifiedRequestCallback,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) {
if (holder.response != null) {
val device = devices.getDeviceById(holder.deviceId)
deviceIcon.setImageResource(device.iconId)
deviceName.setText(device.name)
adapter.updateData(holder.response, recyclerViewInterface)
fab.hide()
isDeviceSelected = true
} else {
if (currentView == null) {
loadDeviceList()
Toast.makeText(this@MainActivity, holder.errorMessage, Toast.LENGTH_LONG).show()
} else {
currentView?.findViewById<TextView>(R.id.summary)?.text = holder.errorMessage
}
}
}
override fun onExecuted(
result: String,
shouldRefresh: Boolean,
) {
showExecutionResult(result)
if (shouldRefresh) unified?.loadList(this)
}
}
private val unifiedHelperInterface =
object : HomeRecyclerViewHelperInterface {
override fun onStateChanged(
view: View,
data: ListViewItem,
state: Boolean,
) {
if (data.hidden.isEmpty()) return
unified?.changeSwitchState(data.hidden, state)
}
override fun onItemClicked(
view: View,
data: ListViewItem,
) {
unified?.execute(data.hidden, unifiedRequestCallback)
}
}
private val unifiedRealTimeStatesCallback =
object : UnifiedAPI.RealTimeStatesCallback {
override fun onStatesLoaded(
states: List<ListViewItem>,
offset: Int,
) {
for (i in states.indices) {
adapter.updateItem(
i + offset,
states[i],
)
}
}
}
/*
* Things related to Tasmota
*/
private val tasmotaHelperInterface =
object : HomeRecyclerViewHelperInterface {
override fun onStateChanged(
view: View,
data: ListViewItem,
state: Boolean,
) {
// Do nothing.
}
override fun onItemClicked(
view: View,
data: ListViewItem,
) {
val helper = TasmotaHelper(this@MainActivity, unified ?: return)
when (data.hidden) {
"add" -> helper.addToList(unifiedRequestCallback)
"execute_once" -> helper.executeOnce(unifiedRequestCallback)
else ->
unified?.execute(
view.findViewById<TextView>(R.id.summary).text.toString(),
unifiedRequestCallback,
)
}
}
}
/*
* Things related to the main menu
*/
private val mainHelperInterface =
object : HomeRecyclerViewHelperInterface {
override fun onStateChanged(
view: View,
data: ListViewItem,
state: Boolean,
) {
if (data.hidden.isEmpty()) return
val deviceId = data.hidden.substring(0, data.hidden.indexOf('@'))
Global.getCorrectAPI(
this@MainActivity,
devices.getDeviceById(deviceId).mode,
deviceId,
).changeSwitchState(data.hidden.substring(deviceId.length + 1), state)
}
override fun onItemClicked(
view: View,
data: ListViewItem,
) {
currentView = view
if (data.title == resources.getString(R.string.main_no_devices)) {
startActivityAndReset(Intent(this@MainActivity, DevicesActivity::class.java))
} else if (data.title == resources.getString(R.string.err_wrong_format)) {
startActivityAndReset(Intent(this@MainActivity, SettingsActivity::class.java))
} else if (data.hidden.contains('@')) {
val deviceId = data.hidden.substring(0, data.hidden.indexOf('@'))
val api = Global.getCorrectAPI(this@MainActivity, devices.getDeviceById(deviceId).mode, deviceId)
api.execute(
data.hidden.substring(deviceId.length + 1),
object : UnifiedAPI.CallbackInterface {
override fun onItemsLoaded(
holder: UnifiedRequestCallback,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) {
// Do nothing.
}
override fun onExecuted(
result: String,
shouldRefresh: Boolean,
) {
showExecutionResult(result)
if (shouldRefresh) {
api.loadList(
object : UnifiedAPI.CallbackInterface {
override fun onItemsLoaded(
holder: UnifiedRequestCallback,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) {
adapter.updateDirectView(
deviceId,
holder.response ?: listOf(),
adapter.getDirectViewPos(deviceId),
)
}
override fun onExecuted(
result: String,
shouldRefresh: Boolean,
) {
// Do nothing.
}
},
)
}
}
},
)
} else {
if (checkNetwork(this@MainActivity)) {
view.findViewById<TextView>(R.id.summary).text =
resources.getString(R.string.main_connecting)
selectDevice(data.hidden)
} else {
view.findViewById<TextView>(R.id.summary).text =
resources.getString(R.string.main_network_not_secure)
}
}
}
}
private fun getColumns(): Int? =
(
PreferenceManager.getDefaultSharedPreferences(this)
.getString(P.PREF_COLUMNS, P.PREF_COLUMNS_DEFAULT)
?: P.PREF_COLUMNS_DEFAULT
).toIntOrNull()
/*
* Activity methods
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
window.statusBarColor = SurfaceColors.SURFACE_0.getColor(this)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
devices = Devices(this)
deviceIcon = findViewById(R.id.deviceIcon)
deviceName = findViewById(R.id.deviceName)
fab = findViewById(R.id.fab)
columns = getColumns()
setupHeader()
adapter = MainListAdapter(recyclerView)
recyclerView.layoutManager = GridLayoutManager(this, numberOfRows())
recyclerView.adapter = adapter
fab.setOnClickListener {
startActivityAndReset(Intent(this, DevicesActivity::class.java))
}
findViewById<MaterialToolbar>(R.id.toolbar).setOnMenuItemClickListener {
startActivity(
Intent(
this@MainActivity,
SettingsActivity::class.java,
),
)
true
}
// Handle shortcut
if (intent.hasExtra(Devices.INTENT_EXTRA_DEVICE)) {
val deviceId = intent.getStringExtra(Devices.INTENT_EXTRA_DEVICE) ?: ""
if (devices.idExists(deviceId)) {
if (checkNetwork(this)) {
val device = devices.getDeviceById(deviceId)
deviceIcon.setImageResource(device.iconId)
deviceName.setText(device.name)
selectDevice(deviceId)
} else {
loadDeviceList()
Toast.makeText(this, R.string.main_network_not_secure, Toast.LENGTH_LONG)
.show()
}
} else {
loadDeviceList()
Toast.makeText(this, R.string.main_device_nonexistent, Toast.LENGTH_LONG).show()
}
} else {
loadDeviceList()
}
onBackPressedDispatcher.addCallback {
if (isDeviceSelected) {
loadDeviceList()
} else {
finish()
}
}
}
private fun setupHeader() {
deviceIcon.setFactory {
val view = ImageView(this@MainActivity)
view.layoutParams =
FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT,
)
view
}
deviceName.setFactory {
val view = TextView(this@MainActivity)
view.layoutParams =
FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT,
)
view.setTextAppearance(androidx.appcompat.R.style.TextAppearance_AppCompat_Large)
view.setTextColor(ContextCompat.getColor(this, android.R.color.white))
view.gravity = Gravity.CENTER_VERTICAL
view.ellipsize = TextUtils.TruncateAt.END
view.maxLines = 1
view
}
val inAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_in)
val outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_out)
inAnimation.duration /= 2
outAnimation.duration /= 2
deviceIcon.inAnimation = inAnimation
deviceIcon.outAnimation = outAnimation
deviceName.inAnimation = inAnimation
deviceName.outAnimation = outAnimation
}
override fun onCreateContextMenu(
menu: ContextMenu?,
v: View?,
menuInfo: ContextMenu.ContextMenuInfo?,
) {
super.onCreateContextMenu(menu, v, menuInfo)
val hidden = v?.findViewById<TextView>(R.id.hidden)?.text ?: return
if (hidden.contains("tasmota_command")) {
tasmotaPosition = hidden.substring(hidden.lastIndexOf('#') + 1).toInt()
menuInflater.inflate(R.menu.activity_main_tasmota_context, menu)
}
}
override fun onContextItemSelected(item: MenuItem): Boolean {
val helper = TasmotaHelper(this, unified ?: return super.onContextItemSelected(item))
return when (item.title) {
resources.getString(R.string.str_edit) -> {
helper.updateItem(unifiedRequestCallback, tasmotaPosition)
true
}
resources.getString(R.string.str_delete) -> {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.str_delete)
.setMessage(R.string.tasmota_delete_command)
.setPositiveButton(R.string.str_delete) { _, _ ->
helper.removeFromList(unifiedRequestCallback, tasmotaPosition)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
true
}
else -> {
super.onContextItemSelected(item)
}
}
}
override fun onStart() {
super.onStart()
if (getColumns() != columns) {
columns = getColumns()
recreate()
}
if (shouldReset) {
loadDeviceList()
shouldReset = false
}
canReceiveRequest = true
}
override fun onStop() {
super.onStop()
canReceiveRequest = false
}
override fun onDestroy() {
super.onDestroy()
updateHandler.stop()
}
private fun numberOfRows(): Int {
if (columns != null) return columns ?: 1
val displayMetrics: DisplayMetrics = resources.displayMetrics
val horizontal: Int =
(
displayMetrics.widthPixels / displayMetrics.density / COLUMN_COUNT_FRACTION
).toInt()
val vertical: Int =
(
displayMetrics.heightPixels / displayMetrics.density / COLUMN_COUNT_FRACTION
).toInt()
return max(1, min(horizontal, vertical))
}
internal fun selectDevice(deviceId: String) {
val deviceObj = devices.getDeviceById(deviceId)
when {
WEB_MODES.contains(deviceObj.mode) -> {
val intent =
Intent(this, WebActivity::class.java)
.putExtra("title", deviceObj.name)
when (deviceObj.mode) {
Global.FRITZ_AUTO_LOGIN -> {
intent.putExtra("URI", deviceObj.address)
intent.putExtra("fritz_auto_login", deviceObj.id)
}
Global.GRAFANA_AUTO_LOGIN -> {
intent.putExtra("URI", deviceObj.address)
intent.putExtra("grafana_auto_login", deviceObj.id)
}
Global.PI_HOLE_AUTO_LOGIN -> {
intent.putExtra("URI", deviceObj.address)
intent.putExtra("pi_hole_auto_login", deviceObj.id)
}
Global.NODE_RED -> {
intent.putExtra("URI", deviceObj.address + "ui/")
}
Global.WEBSITE -> {
intent.putExtra("URI", deviceObj.address)
}
}
startActivityAndReset(intent)
}
Global.UNIFIED_MODES.contains(deviceObj.mode) -> {
unified =
Global.getCorrectAPI(
this,
deviceObj.mode,
deviceId,
unifiedHelperInterface,
tasmotaHelperInterface,
)
unified?.loadList(unifiedRequestCallback, true)
updateHandler.setUpdateFunction {
if (canReceiveRequest && unified?.needsRealTimeData == true) {
unified?.loadStates(unifiedRealTimeStatesCallback, 0)
}
}
}
else -> {
Toast.makeText(this, R.string.main_unknown_mode, Toast.LENGTH_LONG).show()
}
}
}
private fun onDirectView(
currentDevice: DeviceItem,
position: Int,
registeredForUpdates: HashMap<Int, UnifiedAPI?>,
) {
val api = Global.getCorrectAPI(this, currentDevice.mode, currentDevice.id)
api.loadList(
object : UnifiedAPI.CallbackInterface {
override fun onItemsLoaded(
holder: UnifiedRequestCallback,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) {
if (holder.response != null) {
Thread {
while (!updateHandler.running) Thread.sleep(TINY_DELAY)
runOnUiThread {
adapter.updateDirectView(
currentDevice.id,
holder.response,
position,
)
}
registeredForUpdates[position] = api
}.start()
}
}
override fun onExecuted(
result: String,
shouldRefresh: Boolean,
) {
// Do nothing.
}
},
)
}
private fun getDeviceItem(device: DeviceItem) =
ListViewItem(
title = device.name,
summary = resources.getString(R.string.main_tap_to_connect),
hidden = device.id,
icon = device.iconId,
)
private fun updateStates(registeredForUpdates: HashMap<Int, UnifiedAPI?>) {
if (canReceiveRequest) {
for (i in registeredForUpdates.keys) {
if (registeredForUpdates[i]?.needsRealTimeData == true) {
registeredForUpdates[i]?.loadStates(
unifiedRealTimeStatesCallback,
adapter.getOffset(i),
)
}
}
}
}
internal fun loadDeviceList() {
updateHandler.stop()
val registeredForUpdates: HashMap<Int, UnifiedAPI?> = hashMapOf()
val listItems: ArrayList<ListViewItem> = ArrayList(devices.length)
if (devices.length == 0) {
listItems +=
ListViewItem(
title = resources.getString(R.string.main_no_devices),
summary = resources.getString(R.string.main_no_devices_summary),
icon = R.drawable.ic_info,
)
}
var actualPosition = 0
for (i in 0 until devices.length) {
val currentDevice = devices.getDeviceByIndex(i)
if (!currentDevice.hide) {
if (
currentDevice.directView &&
Global.UNIFIED_MODES.contains(currentDevice.mode) &&
checkNetwork(this)
) {
onDirectView(currentDevice, actualPosition, registeredForUpdates)
}
listItems += getDeviceItem(currentDevice)
actualPosition++
}
}
adapter.updateData(listItems, mainHelperInterface)
deviceIcon.setImageResource(R.drawable.ic_home_white)
deviceName.setText(resources.getString(R.string.main_device_name))
fab.show()
isDeviceSelected = false
updateHandler.setUpdateFunction {
updateStates(registeredForUpdates)
}
unified = null
}
internal fun startActivityAndReset(intent: Intent) {
shouldReset = true
startActivity(intent)
}
internal fun showExecutionResult(result: String) {
if (result.length < MAX_RESPONSE_LENGTH) {
Toast.makeText(this, result, Toast.LENGTH_LONG).show()
} else {
Snackbar
.make(
findViewById(android.R.id.content),
R.string.main_execution_completed,
Snackbar.LENGTH_LONG,
)
.setAction(R.string.str_show) {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.main_execution_completed)
.setMessage(result)
.setPositiveButton(android.R.string.ok) { _, _ -> }
.show()
}
.show()
}
}
companion object {
private val WEB_MODES =
arrayOf(
Global.FRITZ_AUTO_LOGIN,
Global.GRAFANA_AUTO_LOGIN,
Global.PI_HOLE_AUTO_LOGIN,
Global.NODE_RED,
Global.WEBSITE,
)
private const val TINY_DELAY = 100L
private const val COLUMN_COUNT_FRACTION = 240
private const val MAX_RESPONSE_LENGTH = 64
}
}

View file

@ -0,0 +1,118 @@
package io.github.domi04151309.home.activities
import android.net.nsd.NsdManager
import android.net.wifi.WifiManager
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.rine.upnpdiscovery.UPnPDiscovery
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.DeviceDiscoveryListAdapter
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.discovery.NetworkServiceDiscoveryListener
import io.github.domi04151309.home.discovery.NetworkServiceResolveListener
import io.github.domi04151309.home.discovery.UPnPListener
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
class SearchDevicesActivity : BaseActivity(), RecyclerViewHelperInterface {
private lateinit var adapter: DeviceDiscoveryListAdapter
private lateinit var devices: Devices
private lateinit var nsdManager: NsdManager
private lateinit var discoveryListenerHttp: NsdManager.DiscoveryListener
private lateinit var discoveryListenerSimpleHome: NsdManager.DiscoveryListener
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_devices)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
adapter =
DeviceDiscoveryListAdapter(
mutableListOf(
ListViewItem(
title = resources.getString(R.string.pref_add_search),
summary = resources.getString(R.string.pref_add_search_summary),
icon = R.drawable.ic_search,
),
),
this,
)
devices = Devices(this)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = adapter
// Device variables
val manager = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
val routerIp = intToIp(manager.dhcpInfo.gateway)
Thread {
// Add Router
adapter.add(
ListViewItem(
title = resources.getString(R.string.pref_device_router),
summary = routerIp,
hidden = "Website#Router",
icon = R.drawable.ic_device_router,
state = devices.addressExists(routerIp),
),
)
// Get compatible devices
UPnPDiscovery.discoveryDevices(
this,
UPnPListener(this, adapter),
)
}.start()
val resolveListener = NetworkServiceResolveListener(this, adapter)
nsdManager = getSystemService(NSD_SERVICE) as NsdManager
discoveryListenerHttp = NetworkServiceDiscoveryListener(this, resolveListener)
discoveryListenerSimpleHome = NetworkServiceDiscoveryListener(this, resolveListener)
nsdManager.discoverServices("_http._tcp", NsdManager.PROTOCOL_DNS_SD, discoveryListenerHttp)
nsdManager.discoverServices("_simplehome._tcp", NsdManager.PROTOCOL_DNS_SD, discoveryListenerSimpleHome)
}
@Suppress("MagicNumber")
private fun intToIp(address: Int): String =
(address and 0xFF).toString() + "." + (address shr 8 and 0xFF) + "." +
(address shr 16 and 0xFF) + "." + (address shr 24 and 0xFF)
override fun onItemClicked(
view: View,
position: Int,
) {
val name = view.findViewById<TextView>(R.id.title).text.toString()
val hidden = view.findViewById<TextView>(R.id.hidden).text.toString()
if (hidden != "") {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.pref_add_dialog)
.setMessage(resources.getString(R.string.pref_add_dialog_message, name))
.setPositiveButton(R.string.str_add) { _, _ ->
val newItem =
DeviceItem(
devices.generateNewId(),
name,
hidden.substring(0, hidden.indexOf('#')),
hidden.substring(hidden.lastIndexOf('#') + 1),
)
newItem.address = view.findViewById<TextView>(R.id.summary).text.toString()
devices.addDevice(newItem)
adapter.changeState(position, true)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
}
}
override fun onDestroy() {
super.onDestroy()
nsdManager.stopServiceDiscovery(discoveryListenerHttp)
nsdManager.stopServiceDiscovery(discoveryListenerSimpleHome)
}
}

View file

@ -0,0 +1,82 @@
package io.github.domi04151309.home.activities
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.core.content.edit
import androidx.core.net.toUri
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.github.domi04151309.home.R
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.P
class SettingsActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, GeneralPreferenceFragment())
.commit()
}
class GeneralPreferenceFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(
savedInstanceState: Bundle?,
rootKey: String?,
) {
addPreferencesFromResource(R.xml.pref_general)
findPreference<Preference>(P.PREF_CONTROLS_AUTH)?.isVisible =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
findPreference<Preference>("devices")?.setOnPreferenceClickListener {
startActivity(Intent(context, DevicesActivity::class.java))
true
}
findPreference<Preference>("devices_json")?.setOnPreferenceClickListener {
Devices.reloadFromPreferences()
true
}
findPreference<Preference>("reset_json")?.setOnPreferenceClickListener {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.pref_reset)
.setMessage(R.string.pref_reset_question)
.setPositiveButton(R.string.str_delete) { _, _ ->
PreferenceManager.getDefaultSharedPreferences(requireContext()).edit {
putString("devices_json", Global.DEFAULT_JSON)
}
Toast.makeText(context, R.string.pref_reset_toast, Toast.LENGTH_LONG).show()
Devices.reloadFromPreferences()
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
true
}
findPreference<Preference>("about")?.setOnPreferenceClickListener {
startActivity(Intent(context, AboutActivity::class.java))
true
}
findPreference<Preference>("wiki")?.setOnPreferenceClickListener {
val uri = "https://github.com/Domi04151309/HomeApp/wiki"
startActivity(
Intent(context, WebActivity::class.java).putExtra("URI", uri)
.putExtra("title", resources.getString(R.string.pref_info_wiki)),
)
true
}
findPreference<Preference>("header")?.setOnPreferenceClickListener {
startActivity(
Intent(
Intent.ACTION_VIEW,
"https://unsplash.com/photos/mx4mSkK9zeo".toUri(),
),
)
true
}
}
}
}

View file

@ -0,0 +1,82 @@
package io.github.domi04151309.home.activities
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.SimpleListAdapter
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.SimpleListItem
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
class ShortcutDeviceActivity : BaseActivity(), RecyclerViewHelperInterface {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_devices)
val devices = Devices(this)
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
val listItems: ArrayList<SimpleListItem> = ArrayList(devices.length)
var currentDevice: DeviceItem
for (i in 0 until devices.length) {
currentDevice = devices.getDeviceByIndex(i)
listItems +=
SimpleListItem(
title = currentDevice.name,
summary = currentDevice.address,
hidden = currentDevice.id,
icon = currentDevice.iconId,
)
}
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = SimpleListAdapter(listItems, this)
}
override fun onItemClicked(
view: View,
position: Int,
) {
val device = Devices(this).getDeviceByIndex(position)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val shortcutManager = this.getSystemService(ShortcutManager::class.java)
if (shortcutManager != null) {
setResult(
RESULT_OK,
shortcutManager.createShortcutResultIntent(
ShortcutInfo.Builder(this, device.id)
.setShortLabel(
device.name.ifEmpty {
resources.getString(R.string.pref_add_name_empty)
},
)
.setLongLabel(
device.name.ifEmpty {
resources.getString(R.string.pref_add_name_empty)
},
)
.setIcon(Icon.createWithResource(this, device.iconId))
.setIntent(
Intent(this, MainActivity::class.java)
.putExtra(Devices.INTENT_EXTRA_DEVICE, device.id)
.setAction(Intent.ACTION_MAIN)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK),
)
.build(),
),
)
finish()
}
} else {
Toast.makeText(this, R.string.pref_add_shortcut_failed, Toast.LENGTH_LONG).show()
}
}
}

View file

@ -0,0 +1,122 @@
package io.github.domi04151309.home.activities
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.SimpleListAdapter
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.api.UnifiedAPI
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.SimpleListItem
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
class ShortcutHueRoomActivity : BaseActivity(), RecyclerViewHelperInterface {
private var deviceId: String? = null
private lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_devices)
recyclerView = findViewById(R.id.recyclerView)
val devices = Devices(this)
val listItems: ArrayList<SimpleListItem> = ArrayList(devices.length)
var currentDevice: DeviceItem
for (i in 0 until devices.length) {
currentDevice = devices.getDeviceByIndex(i)
if (currentDevice.mode == Global.HUE_API) {
listItems +=
SimpleListItem(
title = currentDevice.name,
summary = currentDevice.address,
hidden = currentDevice.id,
icon = currentDevice.iconId,
)
}
}
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = SimpleListAdapter(listItems, this)
}
override fun onItemClicked(
view: View,
position: Int,
) {
if (deviceId == null) {
deviceId = view.findViewById<TextView>(R.id.hidden).text.toString()
HueAPI(this, deviceId ?: error("Impossible state.")).loadList(
object : UnifiedAPI.CallbackInterface {
override fun onItemsLoaded(
holder: UnifiedRequestCallback,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) {
if (holder.response != null) {
recyclerView.adapter =
SimpleListAdapter(
holder.response as List<SimpleListItem>,
this@ShortcutHueRoomActivity,
)
} else {
deviceId = null
Toast.makeText(
this@ShortcutHueRoomActivity,
holder.errorMessage,
Toast.LENGTH_LONG,
).show()
}
}
override fun onExecuted(
result: String,
shouldRefresh: Boolean,
) {
// Do nothing.
}
},
)
} else {
val device = Devices(this).getDeviceById(deviceId ?: error("Impossible state."))
val lampName = view.findViewById<TextView>(R.id.title).text
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val shortcutManager = this.getSystemService(ShortcutManager::class.java)
if (shortcutManager != null) {
setResult(
RESULT_OK,
shortcutManager.createShortcutResultIntent(
ShortcutInfo.Builder(this, device.id + lampName)
.setShortLabel(lampName)
.setLongLabel(lampName)
.setIcon(Icon.createWithResource(this, device.iconId))
.setIntent(
Intent(this, HueLampActivity::class.java)
.putExtra("id", view.findViewById<TextView>(R.id.hidden).text)
.putExtra(Devices.INTENT_EXTRA_DEVICE, device.id)
.setAction(Intent.ACTION_MAIN)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK),
)
.build(),
),
)
finish()
}
} else {
Toast.makeText(this, R.string.pref_add_shortcut_failed, Toast.LENGTH_LONG).show()
}
}
}
}

View file

@ -0,0 +1,26 @@
package io.github.domi04151309.home.activities
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.helpers.Devices
class ShortcutHueSceneActionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent.hasExtra("scene") && intent.hasExtra("group") && intent.hasExtra(Devices.INTENT_EXTRA_DEVICE)) {
HueAPI(
this,
intent.getStringExtra(Devices.INTENT_EXTRA_DEVICE) ?: error(IMPOSSIBLE_STATE),
).activateSceneOfGroup(
intent.getStringExtra("group") ?: error(IMPOSSIBLE_STATE),
intent.getStringExtra("scene") ?: error(IMPOSSIBLE_STATE),
)
}
finish()
}
companion object {
private const val IMPOSSIBLE_STATE = "Impossible state."
}
}

View file

@ -0,0 +1,181 @@
package io.github.domi04151309.home.activities
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.SimpleListAdapter
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.api.UnifiedAPI
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.SimpleListItem
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
import org.json.JSONObject
class ShortcutHueSceneActivity : BaseActivity(), RecyclerViewHelperInterface {
private var deviceId: String? = null
private var group: String? = null
private lateinit var recyclerView: RecyclerView
private val device: DeviceItem
get() = Devices(this).getDeviceById(deviceId ?: error("Device ID is null."))
private val api: HueAPI
get() = HueAPI(this, deviceId ?: error("Device ID is null."))
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_devices)
recyclerView = findViewById(R.id.recyclerView)
val devices = Devices(this)
val listItems: ArrayList<SimpleListItem> = ArrayList(devices.length)
var currentDevice: DeviceItem
for (i in 0 until devices.length) {
currentDevice = devices.getDeviceByIndex(i)
if (currentDevice.mode == Global.HUE_API) {
listItems +=
SimpleListItem(
title = currentDevice.name,
summary = currentDevice.address,
hidden = currentDevice.id,
icon = currentDevice.iconId,
)
}
}
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = SimpleListAdapter(listItems, this)
}
private fun loadDevice(view: View) {
deviceId = view.findViewById<TextView>(R.id.hidden).text.toString()
api.loadList(
object : UnifiedAPI.CallbackInterface {
override fun onItemsLoaded(
holder: UnifiedRequestCallback,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) {
if (holder.response != null) {
recyclerView.adapter =
SimpleListAdapter(
holder.response as List<SimpleListItem>,
this@ShortcutHueSceneActivity,
)
} else {
deviceId = null
Toast.makeText(
this@ShortcutHueSceneActivity,
holder.errorMessage,
Toast.LENGTH_LONG,
).show()
}
}
override fun onExecuted(
result: String,
shouldRefresh: Boolean,
) {
// Do nothing.
}
},
)
}
private fun loadGroup(view: View) {
group = view.findViewById<TextView>(R.id.hidden).text.toString()
Volley.newRequestQueue(this).add(
JsonObjectRequest(
Request.Method.GET,
device.address + "api/" + api.getUsername() + "/scenes/",
null,
{ response ->
val listItems: ArrayList<SimpleListItem> = ArrayList(response.length() / 2)
var currentObject: JSONObject
for (i in response.keys()) {
currentObject = response.getJSONObject(i)
if (currentObject.optString("group") == group) {
listItems.add(
SimpleListItem(
currentObject.optString("name"),
resources.getString(R.string.hue_tap),
i,
R.drawable.ic_scene,
),
)
}
}
listItems.sortBy { it.title }
recyclerView.adapter = SimpleListAdapter(listItems, this)
},
{ error ->
group = null
Toast.makeText(this, Global.volleyError(this, error), Toast.LENGTH_LONG)
.show()
},
),
)
}
private fun createShortcut(view: View) {
val lampName = view.findViewById<TextView>(R.id.title).text
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val shortcutManager = this.getSystemService(ShortcutManager::class.java)
if (shortcutManager != null) {
setResult(
RESULT_OK,
shortcutManager.createShortcutResultIntent(
ShortcutInfo.Builder(this, device.id + lampName)
.setShortLabel(view.findViewById<TextView>(R.id.title).text)
.setLongLabel(view.findViewById<TextView>(R.id.title).text)
.setIcon(Icon.createWithResource(this, device.iconId))
.setIntent(
Intent(this, ShortcutHueSceneActionActivity::class.java)
.putExtra(
"scene",
view.findViewById<TextView>(R.id.hidden).text,
)
.putExtra("group", group)
.putExtra(Devices.INTENT_EXTRA_DEVICE, device.id)
.setAction(Intent.ACTION_MAIN)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK),
)
.build(),
),
)
finish()
}
} else {
Toast.makeText(this, R.string.pref_add_shortcut_failed, Toast.LENGTH_LONG).show()
}
}
override fun onItemClicked(
view: View,
position: Int,
) {
if (deviceId == null) {
loadDevice(view)
} else if (group == null) {
loadGroup(view)
} else {
createShortcut(view)
}
}
}

View file

@ -0,0 +1,45 @@
package io.github.domi04151309.home.activities
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import io.github.domi04151309.home.api.Tasmota
import io.github.domi04151309.home.api.UnifiedAPI
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
class ShortcutTasmotaActionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent.hasExtra("command") && intent.hasExtra(Devices.INTENT_EXTRA_DEVICE)) {
Tasmota(
this,
intent.getStringExtra(Devices.INTENT_EXTRA_DEVICE) ?: error("Impossible state."),
null,
).execute(
intent.getStringExtra("command") ?: error("Impossible state."),
object : UnifiedAPI.CallbackInterface {
override fun onItemsLoaded(
holder: UnifiedRequestCallback,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) {
// Do nothing.
}
override fun onExecuted(
result: String,
shouldRefresh: Boolean,
) {
Toast.makeText(
this@ShortcutTasmotaActionActivity,
result,
Toast.LENGTH_LONG,
).show()
}
},
)
}
finish()
}
}

View file

@ -0,0 +1,125 @@
package io.github.domi04151309.home.activities
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.SimpleListAdapter
import io.github.domi04151309.home.api.Tasmota
import io.github.domi04151309.home.api.UnifiedAPI
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.SimpleListItem
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
class ShortcutTasmotaActivity : BaseActivity(), RecyclerViewHelperInterface {
private var deviceId: String? = null
private lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_devices)
recyclerView = findViewById(R.id.recyclerView)
val devices = Devices(this)
val listItems: ArrayList<SimpleListItem> = ArrayList(devices.length)
var currentDevice: DeviceItem
for (i in 0 until devices.length) {
currentDevice = devices.getDeviceByIndex(i)
if (currentDevice.mode == Global.TASMOTA) {
listItems +=
SimpleListItem(
title = currentDevice.name,
summary = currentDevice.address,
hidden = currentDevice.id,
icon = currentDevice.iconId,
)
}
}
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = SimpleListAdapter(listItems, this)
}
override fun onItemClicked(
view: View,
position: Int,
) {
if (deviceId == null) {
deviceId = view.findViewById<TextView>(R.id.hidden).text.toString()
Tasmota(this, deviceId ?: error("Impossible state."), null).loadList(
object : UnifiedAPI.CallbackInterface {
override fun onItemsLoaded(
holder: UnifiedRequestCallback,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) {
if (holder.response != null) {
recyclerView.adapter =
SimpleListAdapter(
holder.response as List<SimpleListItem>,
this@ShortcutTasmotaActivity,
)
} else {
deviceId = null
Toast.makeText(
this@ShortcutTasmotaActivity,
holder.errorMessage,
Toast.LENGTH_LONG,
).show()
}
}
override fun onExecuted(
result: String,
shouldRefresh: Boolean,
) {
// Do nothing.
}
},
)
} else {
val device = Devices(this).getDeviceById(deviceId ?: error("Impossible state."))
val lampName = view.findViewById<TextView>(R.id.title).text
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val shortcutManager = this.getSystemService(ShortcutManager::class.java)
if (shortcutManager != null) {
setResult(
RESULT_OK,
shortcutManager.createShortcutResultIntent(
ShortcutInfo.Builder(this, device.id + lampName)
.setShortLabel(lampName)
.setLongLabel(lampName)
.setIcon(Icon.createWithResource(this, device.iconId))
.setIntent(
Intent(this, ShortcutTasmotaActionActivity::class.java)
.putExtra(
"command",
view.findViewById<TextView>(R.id.summary).text,
)
.putExtra(Devices.INTENT_EXTRA_DEVICE, device.id)
.setAction(Intent.ACTION_MAIN)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK),
)
.build(),
),
)
finish()
}
} else {
Toast.makeText(this, R.string.pref_add_shortcut_failed, Toast.LENGTH_LONG).show()
}
}
}
}

View file

@ -0,0 +1,154 @@
package io.github.domi04151309.home.activities
import android.Manifest
import android.annotation.SuppressLint
import android.app.DownloadManager
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.KeyEvent
import android.view.Menu
import android.view.MenuItem
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.widget.ProgressBar
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.net.toUri
import io.github.domi04151309.home.R
class WebActivity : BaseActivity() {
private var valueCallback: ValueCallback<Array<Uri>>? = null
private lateinit var webView: WebView
private lateinit var webViewClient: WebActivityWebViewClient
private lateinit var resultLauncher: ActivityResultLauncher<Intent>
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web)
webViewClient =
WebActivityWebViewClient(
this,
intent,
findViewById<ProgressBar>(R.id.progressBar),
findViewById<ProgressBar>(R.id.error),
)
webView = findViewById(R.id.webView)
webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
webView.webViewClient = webViewClient
resultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val path = result.data?.data
valueCallback?.onReceiveValue(
if (path == null) {
arrayOf()
} else {
arrayOf(path)
},
)
}
}
webView.webChromeClient =
object : WebChromeClient() {
override fun onShowFileChooser(
webView: WebView?,
filePathCallback: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?,
): Boolean = showFileChooser(filePathCallback)
}
webView.setDownloadListener { url, _, _, _, _ ->
onDownload(url)
}
webView.loadUrl(intent.getStringExtra("URI") ?: ABOUT_BLANK)
title = intent.getStringExtra("title")
}
internal fun showFileChooser(filePathCallback: ValueCallback<Array<Uri>>?): Boolean {
valueCallback = filePathCallback
resultLauncher.launch(
Intent(Intent.ACTION_CHOOSER)
.putExtra(
Intent.EXTRA_INTENT,
Intent(Intent.ACTION_GET_CONTENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
},
)
.putExtra(Intent.EXTRA_TITLE, "Image Chooser"),
)
return true
}
private fun onDownload(url: String) {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE,
) != PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
),
1,
)
}
val uri = url.toUri()
(getSystemService(DOWNLOAD_SERVICE) as DownloadManager).enqueue(
DownloadManager.Request(uri).apply {
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
uri.lastPathSegment,
)
},
)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.activity_web_actions, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.action_open) {
startActivity(Intent(Intent.ACTION_VIEW, webView.url?.toUri()))
return true
}
return super.onOptionsItemSelected(item)
}
override fun onKeyDown(
keyCode: Int,
event: KeyEvent,
): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack() && !webViewClient.hasError) {
webView.goBack()
return true
}
return super.onKeyDown(keyCode, event)
}
companion object {
private const val ABOUT_BLANK = "about:blank"
}
}

View file

@ -0,0 +1,191 @@
package io.github.domi04151309.home.activities
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.http.SslError
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.HttpAuthHandler
import android.webkit.SslErrorHandler
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.EditText
import android.widget.Toast
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.github.domi04151309.home.R
import io.github.domi04151309.home.helpers.DeviceSecrets
class WebActivityWebViewClient(
private val context: Context,
private val intent: Intent,
private val progressView: View,
private val errorView: View,
) :
WebViewClient() {
var hasError: Boolean = false
private set
private var isFirstLoad: Boolean = true
private val nullParent: ViewGroup? = null
override fun onPageFinished(
view: WebView,
url: String,
) {
if (url == ABOUT_BLANK) {
view.visibility = View.GONE
return
}
if (isFirstLoad) {
if (intent.hasExtra("fritz_auto_login")) {
injectFritzLogin(
view,
DeviceSecrets(
context,
intent.getStringExtra("fritz_auto_login") ?: "",
).password,
)
} else if (intent.hasExtra("grafana_auto_login")) {
val secrets =
DeviceSecrets(
context,
intent.getStringExtra("grafana_auto_login") ?: "",
)
injectGrafanaLogin(
view,
secrets.username,
secrets.password,
)
} else if (intent.hasExtra("pi_hole_auto_login")) {
injectPiHoleLogin(
view,
DeviceSecrets(
context,
intent.getStringExtra("pi_hole_auto_login") ?: "",
).password,
)
}
isFirstLoad = false
}
progressView.visibility = View.GONE
view.visibility = View.VISIBLE
super.onPageFinished(view, url)
}
override fun onReceivedHttpAuthRequest(
view: WebView,
handler: HttpAuthHandler,
host: String,
realm: String,
) {
val dialogView =
LayoutInflater.from(context)
.inflate(R.layout.dialog_web_authentication, nullParent, false)
MaterialAlertDialogBuilder(context)
.setTitle(R.string.webView_authentication)
.setView(dialogView)
.setPositiveButton(android.R.string.ok) { _, _ ->
handler.proceed(
dialogView.findViewById<EditText>(R.id.username).text.toString(),
dialogView.findViewById<EditText>(R.id.password).text.toString(),
)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
}
@SuppressLint("WebViewClientOnReceivedSslError")
override fun onReceivedSslError(
view: WebView,
handler: SslErrorHandler,
error: SslError,
) {
Toast.makeText(context, R.string.webView_ssl_error, Toast.LENGTH_LONG)
.show()
handler.proceed()
}
override fun onReceivedError(
view: WebView,
request: WebResourceRequest,
error: WebResourceError,
) {
view.loadUrl(ABOUT_BLANK)
hasError = true
progressView.visibility = View.GONE
errorView.visibility = View.VISIBLE
}
private fun injectFritzLogin(
webView: WebView,
password: String,
) {
webView.evaluateJavascript(
"""
document.getElementById('uiPass').value = '$password';
document.getElementById('submitLoginBtn').click();
""",
) {}
}
private fun injectGrafanaLogin(
webView: WebView,
username: String,
password: String,
) {
webView.evaluateJavascript(
"""
function setNativeValue(element, value) {
const proto = Object.getPrototypeOf(element);
const descriptor = Object.getOwnPropertyDescriptor(proto, 'value');
const setter = descriptor && descriptor.set;
if (setter) {
setter.call(element, value);
} else {
element.value = value;
}
}
const check = setInterval(() => {
const username = document.querySelector('input[name=user]');
const password = document.querySelector('input[name=password]');
if (username && password) {
clearInterval(check);
setNativeValue(username, '$username');
setNativeValue(password, '$password');
['input', 'change'].forEach(eventName => {
username.dispatchEvent(new Event(eventName, { bubbles: true }));
password.dispatchEvent(new Event(eventName, { bubbles: true }));
});
document.querySelector('button[type=submit]').click();
}
}, 100);
""",
) {}
}
private fun injectPiHoleLogin(
webView: WebView,
password: String,
) {
webView.evaluateJavascript(
"""
document.getElementById('current-password').value = '$password';
document.querySelector('button[type=submit]').click();
""",
) {}
}
companion object {
private const val ABOUT_BLANK = "about:blank"
}
}

View file

@ -0,0 +1,76 @@
package io.github.domi04151309.home.adapters
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
class DeviceDiscoveryListAdapter(
private val items: MutableList<ListViewItem>,
private val helperInterface: RecyclerViewHelperInterface,
) : RecyclerView.Adapter<DeviceDiscoveryListAdapter.ViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ViewHolder =
ViewHolder(
LayoutInflater
.from(parent.context)
.inflate(R.layout.list_item_device_discovery, parent, false),
)
override fun onBindViewHolder(
holder: ViewHolder,
position: Int,
) {
holder.drawable.setImageResource(items[position].icon)
holder.title.text = items[position].title
holder.summary.text = items[position].summary
holder.hidden.text = items[position].hidden
holder.stateDrawable.setImageResource(
if (items[position].state == true) {
R.drawable.ic_done
} else {
android.R.color.transparent
},
)
holder.itemView.setOnClickListener { helperInterface.onItemClicked(holder.itemView, position) }
}
override fun getItemCount(): Int = items.size
fun add(item: ListViewItem): Int {
items.add(item)
notifyItemInserted(items.size - 1)
return items.size - 1
}
fun changeState(
i: Int,
state: Boolean,
) {
items[i].state = state
notifyItemChanged(i)
}
fun changeTitle(
i: Int,
title: String,
) {
items[i].title = title
notifyItemChanged(i)
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val drawable: ImageView = view.findViewById(R.id.drawable)
val title: TextView = view.findViewById(R.id.title)
val summary: TextView = view.findViewById(R.id.summary)
val hidden: TextView = view.findViewById(R.id.hidden)
val stateDrawable: ImageView = view.findViewById(R.id.state)
}
}

View file

@ -0,0 +1,60 @@
package io.github.domi04151309.home.adapters
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.SimpleListItem
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterfaceAdvanced
class DeviceListAdapter(
private val items: List<SimpleListItem>,
private val helperInterface: RecyclerViewHelperInterfaceAdvanced,
) : RecyclerView.Adapter<DeviceListAdapter.ViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ViewHolder =
ViewHolder(
LayoutInflater
.from(parent.context)
.inflate(R.layout.list_item_devices, parent, false),
)
@SuppressLint("ClickableViewAccessibility")
override fun onBindViewHolder(
holder: ViewHolder,
position: Int,
) {
holder.drawable.setImageResource(items[position].icon)
holder.title.text = items[position].title
holder.summary.text = items[position].summary
holder.hidden.text = items[position].hidden
holder.itemView.setOnClickListener { helperInterface.onItemClicked(holder.itemView, position) }
if (position == itemCount - 1) {
holder.handle.visibility = View.GONE
} else {
holder.handle.setOnTouchListener { view, event ->
if (event.actionMasked == MotionEvent.ACTION_DOWN) {
helperInterface.onItemHandleTouched(holder)
}
view.performClick()
}
}
}
override fun getItemCount(): Int = items.size
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val drawable: ImageView = view.findViewById(R.id.drawable)
val title: TextView = view.findViewById(R.id.title)
val summary: TextView = view.findViewById(R.id.summary)
val hidden: TextView = view.findViewById(R.id.hidden)
val handle: ImageView = view.findViewById(R.id.handle)
}
}

View file

@ -0,0 +1,24 @@
package io.github.domi04151309.home.adapters
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import io.github.domi04151309.home.fragments.HueColorFragment
import io.github.domi04151309.home.fragments.HueLampsFragment
import io.github.domi04151309.home.fragments.HueScenesFragment
import io.github.domi04151309.home.interfaces.HueRoomInterface
class HueDetailsTabAdapter(
activity: FragmentActivity,
private val lampInterface: HueRoomInterface,
) : FragmentStateAdapter(activity) {
override fun createFragment(position: Int): Fragment =
when (position) {
0 -> HueColorFragment(lampInterface)
1 -> HueScenesFragment(lampInterface)
2 -> HueLampsFragment(lampInterface)
else -> Fragment()
}
override fun getItemCount(): Int = 3
}

View file

@ -0,0 +1,110 @@
package io.github.domi04151309.home.adapters
import android.annotation.SuppressLint
import android.graphics.drawable.LayerDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.materialswitch.MaterialSwitch
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
class HueLampListAdapter(
private val stateListener: CompoundButton.OnCheckedChangeListener,
private val helperInterface: RecyclerViewHelperInterface,
) : RecyclerView.Adapter<HueLampListAdapter.ViewHolder>() {
private var items: List<ListViewItem> = mutableListOf()
private var colors: List<Int> = mutableListOf()
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ViewHolder =
ViewHolder(
LayoutInflater
.from(parent.context)
.inflate(R.layout.list_item, parent, false),
)
override fun onBindViewHolder(
holder: ViewHolder,
position: Int,
) {
val context = holder.itemView.context
val finalDrawable =
LayerDrawable(
arrayOf(
ContextCompat.getDrawable(context, R.drawable.ic_hue_lamp_base),
ContextCompat.getDrawable(context, R.drawable.ic_hue_lamp_color),
),
)
finalDrawable.getDrawable(1).setTint(colors[position])
holder.drawable.setImageDrawable(finalDrawable)
holder.title.text = items[position].title
holder.summary.text = items[position].summary
holder.hidden.text = items[position].hidden
holder.stateSwitch.isChecked = items[position].state == true
holder.stateSwitch.setOnCheckedChangeListener(stateListener)
holder.itemView.setOnClickListener { helperInterface.onItemClicked(holder.itemView, position) }
}
override fun getItemCount(): Int = items.size
@SuppressLint("NotifyDataSetChanged")
fun updateData(
recyclerView: RecyclerView,
newItems: List<ListViewItem>,
newColors: List<Int>,
) {
if (newItems.size != items.size) {
items = newItems
colors = newColors
notifyDataSetChanged()
return
}
val changed = mutableListOf<Int>()
for (i in items.indices) {
if (items[i].hidden != newItems[i].hidden) {
changed.add(i)
} else {
val holder = (recyclerView.findViewHolderForAdapterPosition(i) ?: return) as ViewHolder
if (items[i].summary != newItems[i].summary) {
holder.summary.text = newItems[i].summary
}
if (items[i].state != newItems[i].state) {
holder.stateSwitch.isChecked = newItems[i].state == true
}
if (colors[i] != newColors[i]) {
val context = holder.itemView.context
val finalDrawable =
LayerDrawable(
arrayOf(
ContextCompat.getDrawable(context, R.drawable.ic_hue_lamp_base),
ContextCompat.getDrawable(context, R.drawable.ic_hue_lamp_color),
),
)
finalDrawable.getDrawable(1).setTint(newColors[i])
holder.drawable.setImageDrawable(finalDrawable)
}
}
}
items = newItems
colors = newColors
changed.forEach(::notifyItemChanged)
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val drawable: ImageView = view.findViewById(R.id.drawable)
val title: TextView = view.findViewById(R.id.title)
val summary: TextView = view.findViewById(R.id.summary)
val hidden: TextView = view.findViewById(R.id.hidden)
val stateSwitch: MaterialSwitch = view.findViewById(R.id.state)
}
}

View file

@ -0,0 +1,79 @@
package io.github.domi04151309.home.adapters
import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.LayerDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.SceneGridItem
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
class HueSceneGridAdapter(
private val contextMenuListener: View.OnCreateContextMenuListener,
private val helperInterface: RecyclerViewHelperInterface,
) : RecyclerView.Adapter<HueSceneGridAdapter.ViewHolder>() {
private var items: MutableList<SceneGridItem> = mutableListOf()
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ViewHolder =
ViewHolder(
LayoutInflater
.from(parent.context)
.inflate(R.layout.grid_item, parent, false),
)
override fun onBindViewHolder(
holder: ViewHolder,
position: Int,
) {
val context = holder.itemView.context
holder.title.text = items[position].name
holder.hidden.text = items[position].hidden
if (items[position].color == null) {
holder.drawable.setImageResource(R.drawable.ic_hue_scene_add)
} else {
val finalDrawable =
LayerDrawable(
arrayOf(
ContextCompat.getDrawable(context, R.drawable.ic_hue_scene_base),
ContextCompat.getDrawable(context, R.drawable.ic_hue_scene_color),
),
)
finalDrawable.getDrawable(1).setTint(items[position].color ?: Color.WHITE)
holder.drawable.setImageDrawable(finalDrawable)
}
holder.itemView.setOnClickListener { helperInterface.onItemClicked(holder.itemView, position) }
holder.itemView.setOnCreateContextMenuListener(contextMenuListener)
}
override fun getItemCount(): Int = items.size
@SuppressLint("NotifyDataSetChanged")
fun updateData(newItems: MutableList<SceneGridItem>) {
if (newItems.size != items.size) {
items = newItems
notifyDataSetChanged()
} else {
val changed = mutableListOf<Int>()
for (i in items.indices) {
if (items[i] != newItems[i]) changed.add(i)
}
items = newItems
changed.forEach(::notifyItemChanged)
}
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val drawable: ImageView = view.findViewById(R.id.drawable)
val title: TextView = view.findViewById(R.id.title)
val hidden: TextView = view.findViewById(R.id.hidden)
}
}

View file

@ -0,0 +1,115 @@
package io.github.domi04151309.home.adapters
import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.content.res.Resources
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.widget.ImageViewCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.materialswitch.MaterialSwitch
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.SceneListItem
import io.github.domi04151309.home.interfaces.SceneRecyclerViewHelperInterface
class HueSceneLampListAdapter(
private var items: List<SceneListItem>,
private var helperInterface: SceneRecyclerViewHelperInterface,
) : RecyclerView.Adapter<HueSceneLampListAdapter.ViewHolder>() {
init {
setHasStableIds(true)
}
override fun getItemId(position: Int): Long =
(position.toString() + '#' + items[position].hidden)
.hashCode()
.toLong()
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ViewHolder =
ViewHolder(
LayoutInflater
.from(parent.context)
.inflate(R.layout.list_item, parent, false),
)
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(
holder: ViewHolder,
position: Int,
) {
val id = getItemId(position)
holder.drawable.setImageResource(R.drawable.ic_circle)
holder.title.text = items[position].title
holder.summary.text = generateSummary(holder.itemView.resources, items[position])
holder.hidden.text = items[position].hidden
holder.stateSwitch.isChecked = items[position].state
holder.stateSwitch.setOnCheckedChangeListener { compoundButton, b ->
items[getPosFromId(id)].state = b
holder.summary.text = generateSummary(holder.itemView.resources, items[getPosFromId(id)])
if (compoundButton.isPressed) {
helperInterface.onStateChanged(
holder.itemView,
items[getPosFromId(id)],
b,
)
}
}
ImageViewCompat.setImageTintList(
holder.drawable,
ColorStateList.valueOf(items[position].color),
)
holder.itemView.setOnClickListener {
helperInterface.onItemClicked(holder.itemView, items[getPosFromId(id)])
}
}
override fun getItemCount(): Int = items.size
fun changeSceneBrightness(brightness: String) {
for (i in items.indices) {
items[i].brightness = brightness
if (items[i].state) notifyItemChanged(i)
}
}
fun updateBrightness(
id: String,
brightness: String,
) {
val i = items.indexOfFirst { it.hidden == id }
items[i].brightness = brightness
if (items[i].state) notifyItemChanged(i)
}
fun updateColor(
id: String,
color: Int,
) {
val i = items.indexOfFirst { it.hidden == id }
items[i].color = color
notifyItemChanged(i)
}
private fun generateSummary(
resources: Resources,
item: SceneListItem,
): String =
resources.getString(R.string.hue_brightness) +
": " + if (item.state) item.brightness else "0 %"
private fun getPosFromId(id: Long): Int = items.indices.indexOfFirst { getItemId(it) == id }
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val drawable: ImageView = view.findViewById(R.id.drawable)
val title: TextView = view.findViewById(R.id.title)
val summary: TextView = view.findViewById(R.id.summary)
val hidden: TextView = view.findViewById(R.id.hidden)
val stateSwitch: MaterialSwitch = view.findViewById(R.id.state)
}
}

View file

@ -0,0 +1,62 @@
package io.github.domi04151309.home.adapters
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.Filter
import android.widget.Filterable
import android.widget.ImageView
import android.widget.TextView
import io.github.domi04151309.home.R
import io.github.domi04151309.home.helpers.Global
internal class IconSpinnerAdapter(
private var itemArray: Array<String>,
) : BaseAdapter(), Filterable {
override fun getCount(): Int = itemArray.size
override fun getItem(position: Int): String = itemArray[position]
override fun getItemId(position: Int): Long = position.toLong()
override fun getFilter(): Filter = ItemFilter()
override fun getView(
position: Int,
convertView: View?,
parent: ViewGroup,
): View {
val vi: View =
convertView
?: LayoutInflater.from(parent.context).inflate(R.layout.icon_dropdown_item, parent, false)
vi.findViewById<ImageView>(R.id.drawable).setImageResource(Global.getIcon(itemArray[position]))
vi.findViewById<TextView>(R.id.title).text = itemArray[position]
return vi
}
inner class ItemFilter : Filter() {
override fun performFiltering(constraint: CharSequence): FilterResults {
val results = FilterResults()
val search = constraint.toString().lowercase()
val items: ArrayList<String> = ArrayList(itemArray.size)
for (i in itemArray.indices) {
if (itemArray[i].lowercase().contains(search)) items.add(itemArray[i])
}
results.values = items.toArray()
results.count = items.size
return results
}
override fun publishResults(
constraint: CharSequence,
results: FilterResults,
) {
itemArray = (results.values as Array<*>).filterIsInstance<String>().toTypedArray()
notifyDataSetChanged()
}
}
}

View file

@ -0,0 +1,209 @@
package io.github.domi04151309.home.adapters
import android.annotation.SuppressLint
import android.app.Activity
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.TranslateAnimation
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.materialswitch.MaterialSwitch
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
@Suppress("TooManyFunctions")
class MainListAdapter(private var attachedTo: RecyclerView) : RecyclerView.Adapter<MainListAdapter.ViewHolder>() {
private var items: MutableList<ListViewItem> = mutableListOf()
private var helperInterface: HomeRecyclerViewHelperInterface? = null
private var animate: Boolean = true
private var offsets: IntArray = intArrayOf()
init {
setHasStableIds(true)
}
override fun getItemId(position: Int): Long =
(position.toString() + '#' + items[position].hidden)
.hashCode()
.toLong()
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ViewHolder =
ViewHolder(
LayoutInflater
.from(parent.context)
.inflate(R.layout.list_item, parent, false),
)
override fun onBindViewHolder(
holder: ViewHolder,
position: Int,
) {
holder.drawable.setImageResource(items[position].icon)
holder.title.text = items[position].title
holder.summary.text = items[position].summary
holder.hidden.text = items[position].hidden
val id = getItemId(position)
if (items[position].state != null) {
holder.stateSwitch.isChecked = items[position].state == true
holder.stateSwitch.setOnCheckedChangeListener { compoundButton, b ->
if (compoundButton.isPressed) {
helperInterface?.onStateChanged(
holder.itemView,
items[getPosFromId(id)],
b,
)
}
}
} else {
holder.stateSwitch.visibility = View.GONE
}
holder.itemView.setOnClickListener {
helperInterface?.onItemClicked(holder.itemView, items[getPosFromId(id)])
}
holder.itemView.setOnCreateContextMenuListener(holder.itemView.context as Activity)
if (animate) playAnimation(holder.itemView)
}
override fun onViewRecycled(holder: ViewHolder) {
holder.stateSwitch.visibility = View.VISIBLE
super.onViewRecycled(holder)
}
override fun getItemCount(): Int = items.size
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
attachedTo = recyclerView
}
@SuppressLint("NotifyDataSetChanged")
fun updateData(
newItems: List<ListViewItem>,
newHelperInterface: HomeRecyclerViewHelperInterface? = null,
preferredAnimationState: Boolean? = null,
) {
offsets = IntArray(newItems.size) { 1 }
if (newHelperInterface != null) {
attachedTo.layoutManager?.scrollToPosition(0)
animate = preferredAnimationState != false
items.clear()
items.addAll(newItems)
helperInterface = newHelperInterface
notifyDataSetChanged()
} else {
animate = preferredAnimationState == true
val changed = mutableListOf<Int>()
items.clear()
for (i in 0 until items.size) {
if (items[i] != newItems[i]) changed.add(i)
items.add(newItems[i])
}
changed.forEach(::notifyItemChanged)
}
}
fun updateItem(
position: Int,
item: ListViewItem,
) {
if (position > items.size - 1) {
Log.w(Global.LOG_TAG, "The position $position is larger than the item count")
return
}
if (items[position].summary == item.summary && items[position].state == item.state) {
return
}
items[position].summary = item.summary
items[position].state = item.state
val viewHolder = attachedTo.findViewHolderForAdapterPosition(position) as? ViewHolder
if (viewHolder == null) {
notifyItemChanged(position)
} else {
viewHolder.summary.text = item.summary
viewHolder.stateSwitch.isChecked = item.state == true
}
}
fun getOffset(pos: Int): Int = offsets.copyOfRange(0, pos).sum()
fun updateDirectView(
id: String,
newItems: List<ListViewItem>,
directViewPos: Int,
) {
newItems.forEach { it.hidden = id + '@' + it.hidden }
val correctOffset = getOffset(directViewPos)
val directViewSize = offsets[directViewPos]
for (i in 0 until directViewSize) {
items.removeAt(correctOffset)
notifyItemRemoved(correctOffset)
}
offsets[directViewPos] = newItems.size
items.addAll(correctOffset, newItems)
notifyItemRangeInserted(correctOffset, newItems.size)
}
private fun getPosFromId(id: Long): Int = items.indices.indexOfFirst { getItemId(it) == id }
fun getDirectViewPos(deviceId: String): Int {
var currentPos = 0
for (i in offsets.indices) {
if (items[currentPos].hidden.contains(deviceId)) return i
currentPos += offsets[i]
}
return -1
}
private fun playAnimation(v: View) {
val set = AnimationSet(true)
val firstAnimation: Animation = AlphaAnimation(0.0f, 1.0f)
firstAnimation.duration = ANIMATION_DURATION
set.addAnimation(firstAnimation)
val secondAnimation =
TranslateAnimation(
Animation.RELATIVE_TO_SELF,
-1.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
)
secondAnimation.duration = ANIMATION_DURATION
set.addAnimation(secondAnimation)
v.startAnimation(set)
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val drawable: ImageView = view.findViewById(R.id.drawable)
val title: TextView = view.findViewById(R.id.title)
val summary: TextView = view.findViewById(R.id.summary)
val hidden: TextView = view.findViewById(R.id.hidden)
val stateSwitch: MaterialSwitch = view.findViewById(R.id.state)
}
companion object {
private const val ANIMATION_DURATION = 300L
}
}

View file

@ -0,0 +1,48 @@
package io.github.domi04151309.home.adapters
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.SimpleListItem
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
class SimpleListAdapter(
private val items: List<SimpleListItem>,
private val helperInterface: RecyclerViewHelperInterface,
) : RecyclerView.Adapter<SimpleListAdapter.ViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ViewHolder =
ViewHolder(
LayoutInflater
.from(parent.context)
.inflate(R.layout.list_item_simple, parent, false),
)
@SuppressLint("ClickableViewAccessibility")
override fun onBindViewHolder(
holder: ViewHolder,
position: Int,
) {
holder.drawable.setImageResource(items[position].icon)
holder.title.text = items[position].title
holder.summary.text = items[position].summary
holder.hidden.text = items[position].hidden
holder.itemView.setOnClickListener { helperInterface.onItemClicked(holder.itemView, position) }
}
override fun getItemCount(): Int = items.size
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val drawable: ImageView = view.findViewById(R.id.drawable)
val title: TextView = view.findViewById(R.id.title)
val summary: TextView = view.findViewById(R.id.summary)
val hidden: TextView = view.findViewById(R.id.hidden)
}
}

View file

@ -0,0 +1,86 @@
package io.github.domi04151309.home.api
import android.content.Context
import android.util.Log
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
class EspEasyAPI(
c: Context,
deviceId: String,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) : UnifiedAPI(c, deviceId, recyclerViewInterface) {
private val parser = EspEasyAPIParser(c.resources, this)
override fun loadList(
callback: CallbackInterface,
extended: Boolean,
) {
super.loadList(callback, extended)
val jsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url + "json",
null,
{ infoResponse ->
val listItems = parser.parseResponse(infoResponse)
updateCache(listItems)
callback.onItemsLoaded(
UnifiedRequestCallback(
listItems,
deviceId,
),
recyclerViewInterface,
)
},
{ error ->
callback.onItemsLoaded(
UnifiedRequestCallback(
null,
deviceId,
Global.volleyError(c, error),
),
null,
)
},
)
queue.add(jsonObjectRequest)
}
override fun loadStates(
callback: RealTimeStatesCallback,
offset: Int,
) {
val jsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url + "json",
null,
{ infoResponse ->
callback.onStatesLoaded(
parser.parseResponse(infoResponse),
offset,
)
},
{ },
)
queue.add(jsonObjectRequest)
}
override fun changeSwitchState(
id: String,
state: Boolean,
) {
val switchUrl = url + "control?cmd=GPIO," + id + "," + if (state) "1" else "0"
val jsonObjectRequest =
JsonObjectRequest(
switchUrl,
{ },
{ e -> Log.e(Global.LOG_TAG, e.toString()) },
)
queue.add(jsonObjectRequest)
}
}

View file

@ -0,0 +1,118 @@
package io.github.domi04151309.home.api
import android.content.res.Resources
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.ListViewItem
import org.json.JSONArray
import org.json.JSONObject
class EspEasyAPIParser(resources: Resources, api: UnifiedAPI?) : UnifiedAPI.Parser(resources, api) {
override fun parseResponse(response: JSONObject): List<ListViewItem> {
val listItems = mutableListOf<ListViewItem>()
// sensors
val sensors = response.optJSONArray("Sensors") ?: JSONArray()
for (sensorId in 0 until sensors.length()) {
val currentSensor = sensors.getJSONObject(sensorId)
if (currentSensor.optString("TaskEnabled", FALSE).equals(FALSE)) {
continue
}
val type = currentSensor.optString("Type")
if (type.startsWith("Environment")) {
listItems.addAll(parseEnvironment(type, currentSensor))
} else if (type.startsWith("Switch")) {
listItems.addAll(parseSwitch(type, currentSensor))
}
}
return listItems
}
private fun parseEnvironment(
type: String,
currentSensor: JSONObject,
): List<ListViewItem> {
val listItems = mutableListOf<ListViewItem>()
var taskIcons = intArrayOf()
when (type) {
"Environment - BMx280" -> {
taskIcons += R.drawable.ic_device_thermometer
taskIcons += R.drawable.ic_device_hygrometer
taskIcons += R.drawable.ic_device_gauge
}
"Environment - DHT11/12/22 SONOFF2301/7021" -> {
taskIcons += R.drawable.ic_device_thermometer
taskIcons += R.drawable.ic_device_hygrometer
}
"Environment - DS18b20" -> {
taskIcons += R.drawable.ic_device_thermometer
}
}
val taskName = currentSensor.getString("TaskName")
for (taskId in taskIcons.indices) {
val currentTask = currentSensor.getJSONArray(TASK_VALUES).getJSONObject(taskId)
val currentValue = currentTask.getString(VALUE)
if (!currentValue.equals("nan")) {
val suffix =
when (taskIcons[taskId]) {
R.drawable.ic_device_thermometer -> " °C"
R.drawable.ic_device_hygrometer -> " %"
R.drawable.ic_device_gauge -> " hPa"
else -> ""
}
listItems +=
ListViewItem(
title = currentValue + suffix,
summary = taskName + ": " + currentTask.getString("Name"),
icon = taskIcons[taskId],
)
}
}
return listItems
}
private fun parseSwitch(
type: String,
currentSensor: JSONObject,
): List<ListViewItem> {
val listItems = mutableListOf<ListViewItem>()
when (type) {
"Switch input - Switch" -> {
val currentState = currentSensor.getJSONArray(TASK_VALUES).getJSONObject(0).getInt(VALUE) > 0
var taskName = currentSensor.getString("TaskName")
var gpioId = ""
val gpioFinder = Regex("~GPIO~([0-9]+)$")
val matchResult = gpioFinder.find(taskName)
if (matchResult != null && matchResult.groupValues.size > 1) {
gpioId = matchResult.groupValues[1]
taskName = taskName.replace("~GPIO~$gpioId", "")
}
listItems +=
ListViewItem(
title = taskName,
summary =
resources.getString(
if (currentState) {
R.string.switch_summary_on
} else {
R.string.switch_summary_off
},
),
hidden = gpioId,
state = currentState,
icon = R.drawable.ic_do,
)
api?.needsRealTimeData = true
}
}
return listItems
}
companion object {
private const val FALSE = "false"
private const val TASK_VALUES = "TaskValues"
private const val VALUE = "Value"
}
}

View file

@ -0,0 +1,275 @@
package io.github.domi04151309.home.api
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.preference.PreferenceManager
import com.android.volley.ParseError
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import io.github.domi04151309.home.activities.HueConnectActivity
import io.github.domi04151309.home.activities.HueLampActivity
import io.github.domi04151309.home.custom.CustomJsonArrayRequest
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.Global.volleyError
import io.github.domi04151309.home.helpers.HueUtils
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
import org.json.JSONArray
import org.json.JSONObject
@Suppress("TooManyFunctions")
class HueAPI(
c: Context,
deviceId: String,
recyclerViewInterface: HomeRecyclerViewHelperInterface? = null,
) : UnifiedAPI(c, deviceId, recyclerViewInterface) {
private val parser = HueAPIParser(c.resources)
var readyForRequest: Boolean = true
init {
needsRealTimeData = true
}
interface RequestCallback {
fun onLightsLoaded(response: JSONObject?)
}
fun getUsername(): String =
PreferenceManager.getDefaultSharedPreferences(c)
.getString(deviceId, "")
?: ""
// For unified API
override fun loadList(
callback: CallbackInterface,
extended: Boolean,
) {
super.loadList(callback, extended)
val jsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url + "api/${getUsername()}/groups",
null,
{ response ->
val listItems = parser.parseResponse(response)
updateCache(listItems)
callback.onItemsLoaded(
UnifiedRequestCallback(
listItems,
deviceId,
),
recyclerViewInterface,
)
},
{ error ->
callback.onItemsLoaded(
UnifiedRequestCallback(
null,
deviceId,
volleyError(c, error),
),
null,
)
if (error is ParseError) {
c.startActivity(
Intent(
c,
HueConnectActivity::class.java,
).putExtra("deviceId", deviceId),
)
}
},
)
queue.add(jsonObjectRequest)
}
override fun loadStates(
callback: RealTimeStatesCallback,
offset: Int,
) {
if (!readyForRequest) return
val jsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url + "api/${getUsername()}/groups",
null,
{ response ->
callback.onStatesLoaded(parser.parseResponse(response), offset)
},
{ },
)
queue.add(jsonObjectRequest)
}
override fun execute(
path: String,
callback: CallbackInterface,
) {
c.startActivity(
Intent(c, HueLampActivity::class.java)
.putExtra("id", path)
.putExtra(Devices.INTENT_EXTRA_DEVICE, deviceId),
)
}
override fun changeSwitchState(
id: String,
state: Boolean,
) {
switchGroupById(id, state)
}
override fun changePercentage(
id: String,
percentage: Float,
) {
changeBrightnessOfGroup(id, (percentage / MAX_PERCENTAGE * HueUtils.MAX_BRIGHTNESS).toInt())
}
fun loadLightsByIds(
lightIds: JSONArray,
callback: RequestCallback,
) {
val jsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url + "api/${getUsername()}/lights",
null,
{ response ->
val returnObject = JSONObject()
var lightId: String
for (i in 0 until lightIds.length()) {
lightId = lightIds.getString(i)
returnObject.put(lightId, response.getJSONObject(lightId))
}
callback.onLightsLoaded(returnObject)
},
{ callback.onLightsLoaded(null) },
)
queue.add(jsonObjectRequest)
}
fun switchLightById(
lightId: String,
on: Boolean,
) {
putObject(getLightPath(lightId), "{\"on\":$on}")
}
fun changeBrightness(
lightId: String,
bri: Int,
) {
putObject(getLightPath(lightId), "{\"bri\":$bri}")
}
fun changeColorTemperature(
lightId: String,
ct: Int,
) {
putObject(getLightPath(lightId), "{\"ct\":$ct}")
}
fun changeHue(
lightId: String,
hue: Int,
) {
putObject(getLightPath(lightId), "{\"hue\":$hue}")
}
fun changeSaturation(
lightId: String,
sat: Int,
) {
putObject(getLightPath(lightId), "{\"sat\":$sat}")
}
fun changeHueSat(
lightId: String,
hue: Int,
sat: Int,
) {
putObject(getLightPath(lightId), """{ "hue": $hue, "sat": $sat }""")
}
fun switchGroupById(
groupId: String,
on: Boolean,
) {
putObject(getGroupPath(groupId), "{\"on\":$on}")
}
fun changeBrightnessOfGroup(
groupId: String,
bri: Int,
) {
putObject(getGroupPath(groupId), "{\"bri\":$bri}")
}
fun changeColorTemperatureOfGroup(
groupId: String,
ct: Int,
) {
putObject(getGroupPath(groupId), "{\"ct\":$ct}")
}
fun changeHueOfGroup(
groupId: String,
hue: Int,
) {
putObject(getGroupPath(groupId), "{\"hue\":$hue}")
}
fun changeSaturationOfGroup(
groupId: String,
sat: Int,
) {
putObject(getGroupPath(groupId), "{\"sat\":$sat}")
}
fun changeHueSatOfGroup(
groupId: String,
hue: Int,
sat: Int,
) {
putObject(getGroupPath(groupId), """{ "hue": $hue, "sat": $sat }""")
}
fun activateSceneOfGroup(
groupId: String,
scene: String,
) {
putObject(getGroupPath(groupId), """{ "scene": $scene }""")
}
private fun getLightPath(lightId: String) = "/lights/$lightId/state"
private fun getGroupPath(groupId: String) = "/groups/$groupId/action"
private fun putObject(
address: String,
requestObject: String,
) {
val request =
CustomJsonArrayRequest(
Request.Method.PUT,
url + "api/${getUsername()}$address",
JSONObject(requestObject),
{ },
{ e -> Log.e(Global.LOG_TAG, e.toString()) },
)
if (readyForRequest) {
readyForRequest = false
queue.add(request)
Handler(Looper.getMainLooper()).postDelayed({ readyForRequest = true }, UPDATE_DELAY)
}
}
companion object {
private const val UPDATE_DELAY = 100L
}
}

View file

@ -0,0 +1,174 @@
package io.github.domi04151309.home.api
import android.content.res.Resources
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.data.SimpleListItem
import io.github.domi04151309.home.helpers.HueUtils
import org.json.JSONObject
import java.util.TreeMap
import kotlin.collections.ArrayList
class HueAPIParser(resources: Resources) : UnifiedAPI.Parser(resources) {
override fun parseResponse(response: JSONObject): List<ListViewItem> {
val listItems: ArrayList<ListViewItem> = ArrayList(response.length())
val rooms: TreeMap<String, Pair<String, JSONObject>> = TreeMap()
val zones: TreeMap<String, Pair<String, JSONObject>> = TreeMap()
var currentObject: JSONObject
for (i in response.keys()) {
currentObject = response.getJSONObject(i)
when (currentObject.getString("type")) {
"Room" -> rooms[currentObject.getString("name")] = Pair(i, currentObject)
"Zone" -> zones[currentObject.getString("name")] = Pair(i, currentObject)
}
}
for (i in rooms.keys) listItems.add(
parseGroupObj(
rooms[i] ?: error("Room $i does not exist."),
false,
),
)
for (i in zones.keys) listItems.add(
parseGroupObj(
zones[i] ?: error("Zone $i does not exist."),
true,
),
)
return listItems
}
private fun parseGroupObj(
pair: Pair<String, JSONObject>,
isZone: Boolean,
): ListViewItem {
val state = pair.second.optJSONObject(STATE)?.optBoolean(ANY_ON)
val value =
pair.second.optJSONObject(ACTION)?.optInt(
BRI,
HueUtils.MAX_BRIGHTNESS,
) ?: HueUtils.MAX_BRIGHTNESS
return ListViewItem(
title = pair.second.getString("name"),
summary =
resources.getString(R.string.hue_brightness) +
": " + if (state == true) HueUtils.briToPercent(value) else "0 %",
hidden = pair.first,
icon = if (isZone) R.drawable.ic_zone else R.drawable.ic_room,
state = state,
percentage = (value / HueUtils.MAX_BRIGHTNESS.toFloat() * 100).toInt(),
)
}
companion object {
private const val STATE = "state"
private const val ANY_ON = "any_on"
private const val ACTION = "action"
private const val BRI = "bri"
fun parseHueConfig(
resources: Resources,
response: JSONObject,
): List<SimpleListItem> =
listOf(
SimpleListItem(summary = resources.getString(R.string.hue_bridge)),
SimpleListItem(
response.optString("name"),
resources.getString(R.string.hue_bridge_name),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
response.optString("modelid"),
resources.getString(R.string.hue_bridge_model),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
response.optString("bridgeid"),
resources.getString(R.string.hue_bridge_id),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
response.optString("swversion"),
resources.getString(R.string.hue_bridge_software),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
response.optString("zigbeechannel"),
resources.getString(R.string.hue_bridge_zigbee),
icon = R.drawable.ic_about_info,
),
SimpleListItem(
response.optString("timezone"),
resources.getString(R.string.hue_bridge_time_zone),
icon = R.drawable.ic_about_info,
),
)
fun parseHueSensors(
resources: Resources,
response: JSONObject,
): List<SimpleListItem> {
val sensorItems = mutableListOf<SimpleListItem>()
for (i in response.keys()) {
val current = response.optJSONObject(i) ?: JSONObject()
val config = current.optJSONObject("config") ?: JSONObject()
if (config.has("battery")) {
sensorItems.add(
SimpleListItem(
current.optString("name"),
config.optString("battery") + "%",
icon =
if (config.optBoolean("reachable")) {
R.drawable.ic_device_raspberry_pi
} else {
R.drawable.ic_warning
},
),
)
}
}
val items = mutableListOf(SimpleListItem(summary = resources.getString(R.string.hue_controls)))
items.addAll(sensorItems.sortedBy { it.title })
return items
}
fun parseHueLights(
resources: Resources,
response: JSONObject,
): List<SimpleListItem> {
val lightItems = mutableListOf<SimpleListItem>()
for (i in response.keys()) {
val current =
response.optJSONObject(i)
?: JSONObject()
val state =
current.optJSONObject(STATE) ?: JSONObject()
lightItems.add(
SimpleListItem(
current.optString("name"),
(
if (state.optBoolean("on")) {
resources.getString(
R.string.str_on,
)
} else {
resources.getString(R.string.str_off)
}
) +
" · " +
current.optString("productname"),
icon =
if (state.optBoolean("reachable")) {
R.drawable.ic_device_lamp
} else {
R.drawable.ic_warning
},
),
)
}
val items = mutableListOf(SimpleListItem(summary = resources.getString(R.string.hue_lights)))
items.addAll(lightItems.sortedBy { it.title })
return items
}
}
}

View file

@ -0,0 +1,244 @@
package io.github.domi04151309.home.api
import android.content.Context
import android.util.Log
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import io.github.domi04151309.home.custom.JsonObjectRequestAuth
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.DeviceSecrets
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
class ShellyAPI(
c: Context,
deviceId: String,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
private val version: Int,
) : UnifiedAPI(c, deviceId, recyclerViewInterface) {
private val secrets = DeviceSecrets(c, deviceId)
private val parser = ShellyAPIParser(c.resources, version)
init {
needsRealTimeData = true
}
override fun loadList(
callback: CallbackInterface,
extended: Boolean,
) {
super.loadList(callback, extended)
val jsonObjectRequest =
when (version) {
1 -> listRequestV1(callback)
2 -> listRequestV2(callback)
else -> null
}
queue.add(jsonObjectRequest)
}
private fun listRequestV1(callback: CallbackInterface) =
JsonObjectRequestAuth(
Request.Method.GET,
url + SETTINGS,
secrets,
null,
{ settingsResponse ->
queue.add(
JsonObjectRequestAuth(
Request.Method.GET,
url + "status",
secrets,
null,
{ statusResponse ->
val listItems = parser.parseResponse(settingsResponse, statusResponse)
updateCache(listItems)
callback.onItemsLoaded(
UnifiedRequestCallback(
listItems,
deviceId,
),
recyclerViewInterface,
)
},
{ error ->
callback.onItemsLoaded(
UnifiedRequestCallback(
null,
deviceId,
Global.volleyError(c, error),
),
null,
)
},
),
)
},
{ error ->
callback.onItemsLoaded(
UnifiedRequestCallback(
null,
deviceId,
Global.volleyError(c, error),
),
null,
)
},
)
private fun listRequestV2(callback: CallbackInterface) =
JsonObjectRequest(
Request.Method.GET,
url + "rpc/Shelly.GetConfig",
null,
{ configResponse ->
queue.add(
JsonObjectRequest(
Request.Method.GET,
url + "rpc/Shelly.GetStatus",
null,
{ statusResponse ->
val listItems = parser.parseResponse(configResponse, statusResponse)
updateCache(listItems)
callback.onItemsLoaded(
UnifiedRequestCallback(
listItems,
deviceId,
),
recyclerViewInterface,
)
},
{ error ->
callback.onItemsLoaded(
UnifiedRequestCallback(
null,
deviceId,
Global.volleyError(c, error),
),
null,
)
},
),
)
},
{ error ->
callback.onItemsLoaded(
UnifiedRequestCallback(
null,
deviceId,
Global.volleyError(c, error),
),
null,
)
},
)
override fun loadStates(
callback: RealTimeStatesCallback,
offset: Int,
) {
val jsonObjectRequest =
when (version) {
1 ->
JsonObjectRequestAuth(
Request.Method.GET,
url + SETTINGS,
secrets,
null,
{ settingsResponse ->
queue.add(
JsonObjectRequestAuth(
Request.Method.GET,
url + "status",
secrets,
null,
{ statusResponse ->
callback.onStatesLoaded(
parser.parseResponse(settingsResponse, statusResponse),
offset,
)
},
{ },
),
)
},
{ },
)
2 ->
JsonObjectRequest(
Request.Method.GET,
url + "rpc/Shelly.GetConfig",
null,
{ configResponse ->
queue.add(
JsonObjectRequest(
Request.Method.GET,
url + "rpc/Shelly.GetStatus",
null,
{ statusResponse ->
callback.onStatesLoaded(
parser.parseResponse(configResponse, statusResponse),
offset,
)
},
{ },
),
)
},
{ },
)
else -> null
}
queue.add(jsonObjectRequest)
}
override fun changeSwitchState(
id: String,
state: Boolean,
) {
val requestUrl = url + "relay/$id?turn=" + if (state) "on" else "off"
val jsonObjectRequest =
when (version) {
1 ->
JsonObjectRequestAuth(
Request.Method.GET,
requestUrl,
secrets,
null,
{ },
{ e -> Log.e(Global.LOG_TAG, e.toString()) },
)
2 ->
JsonObjectRequest(
Request.Method.GET,
requestUrl,
null,
{ },
{ e -> Log.e(Global.LOG_TAG, e.toString()) },
)
else -> null
}
queue.add(jsonObjectRequest)
}
companion object {
private const val SETTINGS = "settings"
/**
* Detect the name of the shelly device during discovery.
*/
fun loadName(
url: String,
version: Int,
listener: Response.Listener<String>,
): JsonObjectRequest =
JsonObjectRequest(
url + if (version == 1) SETTINGS else "shelly",
{ statusResponse ->
listener.onResponse(if (statusResponse.isNull("name")) "" else statusResponse.optString("name"))
},
{},
)
}
}

View file

@ -0,0 +1,233 @@
package io.github.domi04151309.home.api
import android.content.res.Resources
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.helpers.Global
import org.json.JSONArray
import org.json.JSONObject
import java.text.DecimalFormat
class ShellyAPIParser(resources: Resources, private val version: Int) :
UnifiedAPI.Parser(resources) {
fun parseResponse(
config: JSONObject,
status: JSONObject,
): List<ListViewItem> =
if (version == 1) {
parseResponseV1(config, status)
} else {
parseResponseV2(config, status)
}
private fun parseResponseV1(
settings: JSONObject,
status: JSONObject,
): List<ListViewItem> {
val listItems = mutableListOf<ListViewItem>()
listItems.addAll(parseSwitchesAndMetersV1(settings, status))
listItems.addAll(parseTemperatureSensorsV1(status))
listItems.addAll(parseHumiditySensorsV1(status))
return listItems
}
private fun parseSwitchesAndMetersV1(
settings: JSONObject,
status: JSONObject,
): List<ListViewItem> {
val listItems = mutableListOf<ListViewItem>()
// switches
val relays = settings.optJSONArray("relays") ?: JSONArray()
var currentRelay: JSONObject
var currentState: Boolean
var hideMeters = false
for (relayId in 0 until relays.length()) {
currentRelay = relays.getJSONObject(relayId)
currentState = currentRelay.getBoolean("ison")
listItems +=
ListViewItem(
title =
nameOrDefault(
if (currentRelay.isNull("name")) "" else currentRelay.optString("name"),
relayId,
),
summary =
resources.getString(
if (currentState) {
R.string.switch_summary_on
} else {
R.string.switch_summary_off
},
),
hidden = relayId.toString(),
state = currentState,
icon = Global.getIcon(currentRelay.optString("appliance_type"), R.drawable.ic_do),
)
// Shelly1 has the "user power constant" setting, but no actual meter
hideMeters = currentRelay.has("power")
}
// power meters
val meters = if (hideMeters) JSONArray() else status.optJSONArray("meters") ?: JSONArray()
var currentMeter: JSONObject
for (meterId in 0 until meters.length()) {
currentMeter = meters.getJSONObject(meterId)
listItems +=
ListViewItem(
title = "${currentMeter.getDouble("power")} W",
summary = resources.getString(R.string.shelly_powermeter_summary),
icon = R.drawable.ic_device_electricity,
)
}
return listItems
}
private fun parseTemperatureSensorsV1(status: JSONObject): List<ListViewItem> {
val listItems = mutableListOf<ListViewItem>()
val tempSensors = status.optJSONObject("ext_temperature") ?: JSONObject()
for (sensorId in tempSensors.keys()) {
val currentSensor = tempSensors.getJSONObject(sensorId)
listItems +=
ListViewItem(
title = "${currentSensor.getDouble("tC")} °C",
summary = resources.getString(R.string.shelly_temperature_sensor_summary),
icon = R.drawable.ic_device_thermometer,
)
}
return listItems
}
private fun parseHumiditySensorsV1(status: JSONObject): List<ListViewItem> {
val listItems = mutableListOf<ListViewItem>()
val humSensors = status.optJSONObject("ext_humidity") ?: JSONObject()
for (sensorId in humSensors.keys()) {
val currentSensor = humSensors.getJSONObject(sensorId)
listItems +=
ListViewItem(
title = "${currentSensor.getDouble("hum")}%",
summary = resources.getString(R.string.shelly_humidity_sensor_summary),
icon = R.drawable.ic_device_hygrometer,
)
}
return listItems
}
private fun parseResponseV2(
config: JSONObject,
status: JSONObject,
): List<ListViewItem> {
val listItems = mutableListOf<ListViewItem>()
for (switchKey in config.keys()) {
if (switchKey.startsWith("switch:")) {
listItems.addAll(
parseSwitchV2(
config.getJSONObject(switchKey),
status.getJSONObject(switchKey),
config,
),
)
} else if (switchKey.startsWith("pm1:")) {
listItems.addAll(parsePowermeter1V2(config.getJSONObject(switchKey), status.getJSONObject(switchKey)))
}
}
return listItems
}
private fun parsePowermeter1V2(
pm1Config: JSONObject,
pm1Status: JSONObject,
): List<ListViewItem> {
val listItems = mutableListOf<ListViewItem>()
val currentId = pm1Config.getInt("id").toString()
val format = DecimalFormat("#.###")
listItems +=
ListViewItem(
title = "${format.format(pm1Status.getDouble("apower"))} W",
summary = resources.getString(R.string.shelly_powermeter_summary),
hidden = currentId,
icon = R.drawable.ic_device_electricity,
)
listItems +=
ListViewItem(
title = "${format.format(pm1Status.getDouble("current"))} A",
summary = resources.getString(R.string.shelly_powermeter_current),
hidden = currentId + "c",
)
listItems +=
ListViewItem(
title = "${format.format(pm1Status.getDouble("voltage"))} V",
summary = resources.getString(R.string.shelly_powermeter_voltage),
hidden = currentId + "v",
)
listItems +=
ListViewItem(
title = "${format.format(pm1Status.getJSONObject("aenergy").getDouble("total") / KILO)} kWh",
summary = resources.getString(R.string.shelly_powermeter_energy),
hidden = currentId + "e",
)
listItems +=
ListViewItem(
title = "${format.format(pm1Status.getJSONObject("ret_aenergy").getDouble("total") / KILO)} kWh",
summary = resources.getString(R.string.shelly_powermeter_return_energy),
hidden = currentId + "rete",
)
return listItems
}
private fun parseSwitchV2(
switchConfig: JSONObject,
switchStatus: JSONObject,
config: JSONObject,
): List<ListViewItem> {
val listItems = mutableListOf<ListViewItem>()
val currentId = switchConfig.getInt("id")
val currentState = switchStatus.getBoolean("output")
listItems +=
ListViewItem(
title =
nameOrDefault(
if (switchConfig.isNull("name")) "" else switchConfig.getString("name"),
currentId,
),
summary =
resources.getString(
if (currentState) {
R.string.switch_summary_on
} else {
R.string.switch_summary_off
},
),
hidden = currentId.toString(),
state = currentState,
icon =
Global.getIcon(
config.optJSONObject("sys")?.optJSONObject("ui_data")
?.optJSONArray("consumption_types")
?.getString(currentId)
?: "",
R.drawable.ic_do,
),
)
return listItems
}
private fun nameOrDefault(
name: String,
id: Int,
): String =
if (name.trim().isEmpty()) {
resources.getString(R.string.shelly_switch_title, id + 1)
} else {
name
}
companion object {
private const val KILO = 1000
}
}

View file

@ -0,0 +1,137 @@
package io.github.domi04151309.home.api
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.Global.volleyError
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
import java.net.URLEncoder
class SimpleHomeAPI(
c: Context,
deviceId: String,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) : UnifiedAPI(c, deviceId, recyclerViewInterface) {
private val parser = SimpleHomeAPIParser(c.resources, this)
override fun loadList(
callback: CallbackInterface,
extended: Boolean,
) {
super.loadList(callback, extended)
val jsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url + "commands",
null,
{ response ->
val listItems = parser.parseResponse(response)
updateCache(listItems)
callback.onItemsLoaded(
UnifiedRequestCallback(
listItems,
deviceId,
),
recyclerViewInterface,
)
},
{ error ->
callback.onItemsLoaded(UnifiedRequestCallback(null, deviceId, volleyError(c, error)), null)
},
)
queue.add(jsonObjectRequest)
}
override fun loadStates(
callback: RealTimeStatesCallback,
offset: Int,
) {
val jsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url + "commands",
null,
{ infoResponse ->
callback.onStatesLoaded(
parser.parseResponse(infoResponse),
offset,
)
},
{ },
)
queue.add(jsonObjectRequest)
}
override fun execute(
path: String,
callback: CallbackInterface,
) {
val splitCharPos = path.lastIndexOf('@')
val realPath = path.substring(splitCharPos + 1)
when (path.substring(0, splitCharPos)) {
"none", "switch" -> { }
"input" -> {
val nullParent: ViewGroup? = null
val view = LayoutInflater.from(c).inflate(R.layout.dialog_input, nullParent, false)
val input = view.findViewById<EditText>(R.id.input)
MaterialAlertDialogBuilder(c)
.setTitle(R.string.input_title)
.setView(view)
.setPositiveButton(R.string.str_send) { _, _ ->
val jsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url + realPath + "?input=" + URLEncoder.encode(input.text.toString(), "utf-8"),
null,
{ },
{ e -> Log.e(Global.LOG_TAG, e.toString()) },
)
queue.add(jsonObjectRequest)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
}
else -> {
val jsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url + realPath,
null,
{ response ->
callback.onExecuted(
response.optString("toast", c.resources.getString(R.string.main_execution_completed)),
response.optBoolean("refresh", false),
)
},
{ error ->
callback.onExecuted(volleyError(c, error))
},
)
queue.add(jsonObjectRequest)
}
}
}
override fun changeSwitchState(
id: String,
state: Boolean,
) {
val jsonObjectRequest =
JsonObjectRequest(
Request.Method.GET,
url + id.substring(id.lastIndexOf('@') + 1) + "?input=" + if (state) 1 else 0,
null,
{ },
{ e -> Log.e(Global.LOG_TAG, e.toString()) },
)
queue.add(jsonObjectRequest)
}
}

View file

@ -0,0 +1,34 @@
package io.github.domi04151309.home.api
import android.content.res.Resources
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.helpers.Global
import org.json.JSONObject
class SimpleHomeAPIParser(resources: Resources, api: UnifiedAPI?) : UnifiedAPI.Parser(resources, api) {
override fun parseResponse(response: JSONObject): List<ListViewItem> {
val listItems: ArrayList<ListViewItem> = ArrayList(response.length())
val commands = response.optJSONObject("commands") ?: return listItems
var currentObject: JSONObject
var currentMode: String
for (i in commands.keys()) {
currentObject = commands.getJSONObject(i)
currentMode = currentObject.optString("mode", "action")
listItems +=
ListViewItem(
title = currentObject.optString("title"),
summary = currentObject.optString("summary"),
hidden = "$currentMode@$i",
icon = Global.getIcon(currentObject.optString("icon"), R.drawable.ic_do),
state = if (currentMode == SWITCH) currentObject.optBoolean("data", false) else null,
)
if (currentMode == SWITCH) api?.needsRealTimeData = true
}
return listItems
}
companion object {
private const val SWITCH = "switch"
}
}

View file

@ -0,0 +1,97 @@
package io.github.domi04151309.home.api
import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.preference.PreferenceManager
import com.android.volley.Request
import com.android.volley.toolbox.StringRequest
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.TasmotaHelper
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
class Tasmota(
c: Context,
deviceId: String,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) : UnifiedAPI(c, deviceId, recyclerViewInterface) {
private val prefs = PreferenceManager.getDefaultSharedPreferences(c)
override fun loadList(
callback: CallbackInterface,
extended: Boolean,
) {
super.loadList(callback, extended)
val list = JSONArray(prefs.getString(deviceId, TasmotaHelper.EMPTY_ARRAY))
val listItems: ArrayList<ListViewItem> = ArrayList(list.length())
if (list.length() == 0) {
listItems +=
ListViewItem(
title = c.resources.getString(R.string.tasmota_empty_list),
summary = c.resources.getString(R.string.tasmota_empty_list_summary),
icon = R.drawable.ic_warning,
)
} else {
var currentItem: JSONObject
for (i in 0 until list.length()) {
try {
currentItem = list.optJSONObject(i) ?: JSONObject()
listItems +=
ListViewItem(
title = currentItem.optString("title"),
summary = currentItem.optString("command"),
hidden = "tasmota_command#$i",
icon = R.drawable.ic_do,
)
} catch (e: JSONException) {
Log.e(Global.LOG_TAG, e.toString())
}
}
}
if (extended) {
listItems +=
ListViewItem(
title = c.resources.getString(R.string.tasmota_add_command),
summary = c.resources.getString(R.string.tasmota_add_command_summary),
icon = R.drawable.ic_add,
hidden = "add",
)
listItems +=
ListViewItem(
title = c.resources.getString(R.string.tasmota_execute_once),
summary = c.resources.getString(R.string.tasmota_execute_once_summary),
icon = R.drawable.ic_edit,
hidden = "execute_once",
)
}
updateCache(listItems)
callback.onItemsLoaded(UnifiedRequestCallback(listItems, deviceId), recyclerViewInterface)
}
override fun execute(
path: String,
callback: CallbackInterface,
) {
val request =
StringRequest(
Request.Method.GET,
url + path,
{ response ->
callback.onExecuted(response)
},
{ error ->
Toast.makeText(c, Global.volleyError(c, error), Toast.LENGTH_LONG).show()
},
)
queue.add(request)
}
}

View file

@ -0,0 +1,89 @@
package io.github.domi04151309.home.api
import android.content.Context
import android.content.res.Resources
import com.android.volley.RequestQueue
import com.android.volley.toolbox.Volley
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.data.UnifiedRequestCallback
import io.github.domi04151309.home.helpers.Devices
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
import org.json.JSONObject
open class UnifiedAPI(
protected val c: Context,
val deviceId: String,
protected val recyclerViewInterface: HomeRecyclerViewHelperInterface?,
) {
interface CallbackInterface {
fun onItemsLoaded(
holder: UnifiedRequestCallback,
recyclerViewInterface: HomeRecyclerViewHelperInterface?,
)
fun onExecuted(
result: String,
shouldRefresh: Boolean = false,
)
}
interface RealTimeStatesCallback {
fun onStatesLoaded(
states: List<ListViewItem>,
offset: Int,
)
}
var needsRealTimeData: Boolean = false
protected val url: String = Devices(c).getDeviceById(deviceId).address
protected val queue: RequestQueue = Volley.newRequestQueue(c)
protected fun updateCache(items: List<ListViewItem>) {
listCache[deviceId] = Pair(System.currentTimeMillis(), items)
}
open fun loadList(
callback: CallbackInterface,
extended: Boolean = false,
) {
if (System.currentTimeMillis() - (listCache[deviceId]?.first ?: 0) < LIST_REQUEST_TIMEOUT) {
callback.onItemsLoaded(
UnifiedRequestCallback(listCache[deviceId]?.second, deviceId),
recyclerViewInterface,
)
return
}
}
open fun loadStates(
callback: RealTimeStatesCallback,
offset: Int,
) {}
open fun execute(
path: String,
callback: CallbackInterface,
) {}
open fun changeSwitchState(
id: String,
state: Boolean,
) {}
open fun changePercentage(
id: String,
percentage: Float,
) {}
open class Parser(protected val resources: Resources, protected val api: UnifiedAPI? = null) {
open fun parseResponse(response: JSONObject): List<ListViewItem> = listOf()
}
companion object {
private const val LIST_REQUEST_TIMEOUT = 1000
private val listCache: MutableMap<String, Pair<Long, List<ListViewItem>>> = mutableMapOf()
protected const val MAX_PERCENTAGE = 100f
}
}

View file

@ -0,0 +1,33 @@
package io.github.domi04151309.home.custom
import com.android.volley.NetworkResponse
import com.android.volley.ParseError
import com.android.volley.Response
import com.android.volley.toolbox.HttpHeaderParser
import com.android.volley.toolbox.JsonRequest
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.io.UnsupportedEncodingException
import java.nio.charset.Charset
class CustomJsonArrayRequest(
method: Int,
url: String,
jsonRequest: JSONObject?,
listener: Response.Listener<JSONArray>,
errorListener: Response.ErrorListener,
) : JsonRequest<JSONArray>(method, url, jsonRequest?.toString(), listener, errorListener) {
override fun parseNetworkResponse(response: NetworkResponse): Response<JSONArray> =
try {
val jsonString = String(response.data, Charset.forName(HttpHeaderParser.parseCharset(response.headers)))
Response.success(
JSONArray(jsonString),
HttpHeaderParser.parseCacheHeaders(response),
)
} catch (e: UnsupportedEncodingException) {
Response.error(ParseError(e))
} catch (e: JSONException) {
Response.error(ParseError(e))
}
}

View file

@ -0,0 +1,26 @@
package io.github.domi04151309.home.custom
import android.util.Base64
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import io.github.domi04151309.home.helpers.DeviceSecrets
import org.json.JSONObject
class JsonObjectRequestAuth(
method: Int,
url: String,
private val secrets: DeviceSecrets,
jsonRequest: JSONObject?,
listener: Response.Listener<JSONObject>,
errorListener: Response.ErrorListener,
) : JsonObjectRequest(method, url, jsonRequest, listener, errorListener) {
override fun getHeaders(): MutableMap<String, String> {
val params = HashMap<String, String>()
params["Authorization"] = "Basic " +
Base64.encodeToString(
"${secrets.username}:${secrets.password}".toByteArray(),
Base64.NO_WRAP,
)
return params
}
}

View file

@ -0,0 +1,28 @@
package io.github.domi04151309.home.custom
import android.text.Editable
import android.text.TextWatcher
class TextWatcher(private val lambda: (text: String) -> Unit) : TextWatcher {
override fun beforeTextChanged(
p0: CharSequence,
p1: Int,
p2: Int,
p3: Int,
) {
// Do nothing.
}
override fun onTextChanged(
p0: CharSequence,
p1: Int,
p2: Int,
p3: Int,
) {
// Do nothing.
}
override fun afterTextChanged(text: Editable) {
lambda(text.toString())
}
}

View file

@ -0,0 +1,31 @@
package io.github.domi04151309.home.data
import io.github.domi04151309.home.helpers.Global
class DeviceItem(
val id: String,
val name: String = "Device",
val mode: String = "Default",
val iconName: String = "Lamp",
val hide: Boolean = false,
val directView: Boolean = false,
) {
var address: String = "http://127.0.0.1/"
set(value) {
field = formatAddress(value)
}
val iconId: Int get() = Global.getIcon(iconName)
companion object {
fun formatAddress(address: String): String {
var url = address
if (!(url.startsWith("https://") || url.startsWith("http://"))) {
url = "http://$url"
}
if (!url.endsWith("/")) {
url += "/"
}
return url
}
}
}

View file

@ -0,0 +1,92 @@
package io.github.domi04151309.home.data
import org.json.JSONArray
import org.json.JSONObject
class LightStates {
private val lights: MutableMap<String, Light> = mutableMapOf()
fun addLight(
id: String,
state: JSONObject,
) {
lights[id] =
Light(
state.optBoolean("on"),
if (state.has("bri")) state.getInt("bri") else -1,
if (state.has("xy")) state.getJSONArray("xy") else null,
ct = if (state.has("ct")) state.getInt("ct") else -1,
)
}
fun setSceneBrightness(bri: Int) {
for (i in lights) {
i.value.bri = bri
}
}
fun setLightBrightness(
id: String,
bri: Int,
) {
lights[id]?.bri = bri
}
fun setLightHue(
id: String,
hue: Int,
) {
lights[id]?.xy = null
lights[id]?.hue = hue
}
fun setLightSat(
id: String,
sat: Int,
) {
lights[id]?.xy = null
lights[id]?.sat = sat
}
fun setLightCt(
id: String,
ct: Int,
) {
lights[id]?.xy = null
lights[id]?.ct = ct
}
fun switchLight(
id: String,
on: Boolean,
) {
lights[id]?.on = on
}
override fun toString(): String {
val json = JSONObject()
for ((key, value) in lights) {
val light = JSONObject()
light.put("on", value.on)
if (value.bri != -1) light.put("bri", value.bri)
if (value.xy != null) light.put("xy", value.xy)
if (value.hue != -1 && value.sat != -1) {
light.put("hue", value.hue)
light.put("sat", value.sat)
} else if (value.ct != -1) {
light.put("ct", value.ct)
}
json.put(key, light)
}
return json.toString()
}
class Light(
var on: Boolean = false,
var bri: Int = -1,
var xy: JSONArray? = null,
var hue: Int = -1,
var sat: Int = -1,
var ct: Int = -1,
)
}

View file

@ -0,0 +1,19 @@
package io.github.domi04151309.home.data
class ListViewItem(
title: String = "",
summary: String = "",
hidden: String = "",
icon: Int = 0,
var state: Boolean? = null,
var percentage: Int? = null,
) : SimpleListItem(title, summary, hidden, icon) {
override fun toString(): String =
"""
title: $title,
summary: $summary,
hidden: $hidden,
state: $state,
percentage: $percentage
""".trimIndent()
}

View file

@ -0,0 +1,7 @@
package io.github.domi04151309.home.data
data class SceneGridItem(
val name: String,
val hidden: String = "",
val color: Int? = null,
)

View file

@ -0,0 +1,9 @@
package io.github.domi04151309.home.data
class SceneListItem(
val title: String = "",
val hidden: String = "",
var state: Boolean = false,
var brightness: String = "",
var color: Int = 0,
)

View file

@ -0,0 +1,8 @@
package io.github.domi04151309.home.data
open class SimpleListItem(
var title: String = "",
var summary: String = "",
var hidden: String = "",
var icon: Int = 0,
)

View file

@ -0,0 +1,7 @@
package io.github.domi04151309.home.data
data class UnifiedRequestCallback(
val response: List<ListViewItem>?,
val deviceId: String,
val errorMessage: String = "",
)

View file

@ -0,0 +1,57 @@
package io.github.domi04151309.home.discovery
import android.content.Context
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import androidx.appcompat.app.AppCompatActivity
class NetworkServiceDiscoveryListener(
context: Context,
private val resolveListener: NetworkServiceResolveListener,
) : NsdManager.DiscoveryListener {
private val nsdManager = context.getSystemService(AppCompatActivity.NSD_SERVICE) as NsdManager
override fun onStartDiscoveryFailed(
p0: String?,
p1: Int,
) {
nsdManager.stopServiceDiscovery(this)
}
override fun onStopDiscoveryFailed(
p0: String?,
p1: Int,
) {
nsdManager.stopServiceDiscovery(this)
}
override fun onDiscoveryStarted(p0: String?) {
// Do nothing.
}
override fun onDiscoveryStopped(p0: String?) {
// Do nothing.
}
override fun onServiceFound(service: NsdServiceInfo) {
val serviceName = service.serviceName.lowercase()
if ((serviceName.startsWith("shelly") && !serviceName.startsWith("shellybutton1")) ||
service.serviceType.equals("_simplehome._tcp.")
) {
if (resolveListener.isBusy.compareAndSet(false, true)) {
nsdManager.resolveService(service, resolveListener)
} else {
resolveListener.pendingServices.add(service)
}
}
}
override fun onServiceLost(service: NsdServiceInfo) {
val iterator = resolveListener.pendingServices.iterator()
while (iterator.hasNext()) {
if (iterator.next().serviceName == service.serviceName) {
iterator.remove()
}
}
}
}

View file

@ -0,0 +1,84 @@
package io.github.domi04151309.home.discovery
import android.app.Activity
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import androidx.appcompat.app.AppCompatActivity
import com.android.volley.toolbox.Volley
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.DeviceDiscoveryListAdapter
import io.github.domi04151309.home.api.ShellyAPI
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.helpers.Devices
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicBoolean
class NetworkServiceResolveListener(
private val activity: Activity,
private val adapter: DeviceDiscoveryListAdapter,
) : NsdManager.ResolveListener {
private val devices = Devices(activity)
private val queue = Volley.newRequestQueue(activity)
private val nsdManager = activity.getSystemService(AppCompatActivity.NSD_SERVICE) as NsdManager
var isBusy: AtomicBoolean = AtomicBoolean(false)
var pendingServices: ConcurrentLinkedQueue<NsdServiceInfo> = ConcurrentLinkedQueue<NsdServiceInfo>()
override fun onResolveFailed(
service: NsdServiceInfo,
p1: Int,
) {
resolveNextInQueue()
}
override fun onServiceResolved(service: NsdServiceInfo) {
activity.runOnUiThread {
if (service.serviceType.equals("._simplehome._tcp")) {
val url =
service.attributes["url"]?.decodeToString()
?: service.host.hostAddress
adapter.add(
ListViewItem(
title = service.serviceName,
summary = url,
hidden = "SimpleHome API#Raspberry Pi",
icon = R.drawable.ic_device_raspberry_pi,
state = devices.addressExists(url),
),
)
} else {
val pos =
adapter.add(
ListViewItem(
title = service.serviceName,
summary = service.host.hostAddress ?: "",
hidden = "Shelly Gen ${
service.attributes["gen"]?.decodeToString() ?: "1"
}#Lamp",
icon = R.drawable.ic_device_lamp,
state = devices.addressExists(service.host.hostAddress ?: ""),
),
)
queue.add(
ShellyAPI.loadName(
"http://" + service.host.hostAddress + "/",
service.attributes["gen"]?.decodeToString()?.toInt() ?: 1,
) { name ->
if (name.isNotEmpty()) adapter.changeTitle(pos, name)
},
)
}
}
resolveNextInQueue()
}
private fun resolveNextInQueue() {
val nextNsdService = pendingServices.poll()
if (nextNsdService != null) {
nsdManager.resolveService(nextNsdService, this)
} else {
isBusy.set(false)
}
}
}

View file

@ -0,0 +1,69 @@
package io.github.domi04151309.home.discovery
import android.content.Context
import android.util.Log
import com.rine.upnpdiscovery.UPnPDevice
import com.rine.upnpdiscovery.UPnPDiscovery
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.DeviceDiscoveryListAdapter
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.helpers.Devices
class UPnPListener(
context: Context,
private val adapter: DeviceDiscoveryListAdapter,
) : UPnPDiscovery.OnDiscoveryListener {
private val devices = Devices(context)
private val addresses = mutableListOf<String>()
override fun onStart() {
// Do nothing.
}
override fun onFoundNewDevice(device: UPnPDevice) {
if (device.server.contains("IpBridge") && !addresses.contains(device.hostAddress)) {
adapter.add(
ListViewItem(
title = device.friendlyName,
summary = device.hostAddress,
hidden = "Hue API#Lamp",
icon = R.drawable.ic_device_lamp,
state = devices.addressExists(device.hostAddress),
),
)
addresses += device.hostAddress
}
if (device.friendlyName.startsWith("FRITZ!") && !addresses.contains(device.hostAddress)) {
adapter.add(
ListViewItem(
title = device.friendlyName,
summary = device.hostAddress,
hidden = "Website#Router",
icon = R.drawable.ic_device_router,
state = devices.addressExists(device.hostAddress),
),
)
addresses += device.hostAddress
}
if (device.server.contains("SimpleHome") && !addresses.contains(device.hostAddress)) {
adapter.add(
ListViewItem(
title = device.friendlyName,
summary = device.hostAddress,
hidden = "SimpleHome API#Raspberry Pi",
icon = R.drawable.ic_device_raspberry_pi,
state = devices.addressExists(device.hostAddress),
),
)
addresses += device.hostAddress
}
}
override fun onFinish(devices: HashSet<UPnPDevice>) {
// Do nothing.
}
override fun onError(e: Exception) {
Log.e(this::class.simpleName, e.toString())
}
}

View file

@ -0,0 +1,33 @@
package io.github.domi04151309.home.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import io.github.domi04151309.home.R
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.helpers.Global
class ControlInfoFragment(
private val device: DeviceItem,
private val title: String,
) : Fragment(R.layout.fragment_control_info) {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val view =
super.onCreateView(inflater, container, savedInstanceState)
?: error("View does not exist yet.")
view.findViewById<ImageView>(R.id.deviceIcon).setImageResource(Global.getIcon(device.iconName))
view.findViewById<TextView>(R.id.titleText).text = title
view.findViewById<TextView>(R.id.subTitleText).text = device.name
return view
}
}

View file

@ -0,0 +1,238 @@
package io.github.domi04151309.home.fragments
import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.graphics.toColorInt
import androidx.fragment.app.Fragment
import com.google.android.material.slider.Slider
import com.skydoves.colorpickerview.ColorPickerView
import com.skydoves.colorpickerview.listeners.ColorListener
import io.github.domi04151309.home.R
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.data.LightStates
import io.github.domi04151309.home.helpers.HueUtils
import io.github.domi04151309.home.helpers.HueUtils.MIN_COLOR_TEMPERATURE
import io.github.domi04151309.home.helpers.SliderUtils
import io.github.domi04151309.home.interfaces.HueRoomInterface
class HueColorFragment(private var lampInterface: HueRoomInterface) : Fragment(R.layout.fragment_hue_color) {
private lateinit var hueAPI: HueAPI
private lateinit var colorPickerView: ColorPickerView
private lateinit var ctText: TextView
private lateinit var ctBar: Slider
private lateinit var hueSatText: TextView
private lateinit var hueBar: Slider
private lateinit var satBar: Slider
private class OnSliderTouchListener(
private val fragment: HueColorFragment,
private val action: (slider: Slider) -> Unit,
) : Slider.OnSliderTouchListener {
override fun onStartTrackingTouch(slider: Slider) {
fragment.pauseUpdates()
}
override fun onStopTrackingTouch(slider: Slider) {
action(slider)
fragment.resumeUpdates()
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
hueAPI = HueAPI(requireContext(), lampInterface.device.id)
val view =
super.onCreateView(inflater, container, savedInstanceState)
?: error("View does not exist yet.")
colorPickerView = view.findViewById(R.id.colorPickerView)
ctText = view.findViewById(R.id.ctTxt)
ctBar = view.findViewById(R.id.ctBar)
hueSatText = view.findViewById(R.id.hueSatTxt)
hueBar = view.findViewById(R.id.hueBar)
satBar = view.findViewById(R.id.satBar)
val availableInputs = arrayOf(colorPickerView, ctBar, hueBar, satBar)
val ctViews = arrayOf(ctText, ctBar)
val hueSatViews = arrayOf(colorPickerView, hueSatText, hueBar, satBar)
setupColorControls(hueAPI)
setupTemperatureControls(hueAPI)
fun updateFunction(data: LightStates.Light) {
if (lampInterface.canReceiveRequest) {
if (data.ct == -1) {
ctViews.forEach {
it.visibility = View.GONE
}
} else {
ctViews.forEach {
it.visibility = View.VISIBLE
}
SliderUtils.setProgress(ctBar, data.ct)
}
if (data.hue == -1 || data.sat == -1) {
hueSatViews.forEach {
it.visibility = View.GONE
}
} else {
hueSatViews.forEach {
it.visibility = View.VISIBLE
}
colorPickerView.selectByHsvColor(HueUtils.hueSatToRGB(data.hue, data.sat))
SliderUtils.setProgress(hueBar, data.hue)
SliderUtils.setProgress(satBar, data.sat)
}
availableInputs.forEach {
it.isEnabled = data.on
}
}
}
view.post {
view.postDelayed({
colorPickerView.selectByHsvColor(
HueUtils.hueSatToRGB(
lampInterface.lampData.state.hue,
lampInterface.lampData.state.sat,
),
)
}, LOADING_DELAY)
updateFunction(lampInterface.lampData.state)
lampInterface.lampData.addOnDataChangedListener(::updateFunction)
}
return view
}
internal fun pauseUpdates() {
lampInterface.canReceiveRequest = false
}
internal fun resumeUpdates() {
Handler(Looper.getMainLooper()).postDelayed({
lampInterface.canReceiveRequest = true
}, UPDATE_DELAY)
}
@SuppressLint("ClickableViewAccessibility")
private fun setupColorPicker(hueAPI: HueAPI) {
colorPickerView.setColorListener(
ColorListener { color, fromUser ->
if (fromUser) {
val hueSat = HueUtils.rgbToHueSat(color)
hueBar.value = hueSat[0].toFloat()
satBar.value = hueSat[1].toFloat()
lampInterface.onColorChanged(color)
}
},
)
colorPickerView.setOnTouchListener { innerView, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
pauseUpdates()
} else if (event.action == MotionEvent.ACTION_UP) {
val hueSat = HueUtils.rgbToHueSat(colorPickerView.color)
hueAPI.changeHueSatOfGroup(lampInterface.id, hueSat[0], hueSat[1])
resumeUpdates()
}
innerView.performClick()
}
}
private fun setupColorControls(hueAPI: HueAPI) {
hueBar.setLabelFormatter { value: Float ->
HueUtils.hueToDegree(value.toInt())
}
satBar.setLabelFormatter { value: Float ->
HueUtils.satToPercent(value.toInt())
}
SliderUtils.setSliderGradient(
hueBar,
HueUtils.defaultColors(),
)
SliderUtils.setSliderGradient(
satBar,
intArrayOf(
Color.WHITE,
Color.RED,
),
)
hueBar.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
val color = HueUtils.hueSatToRGB(value.toInt(), satBar.value.toInt())
colorPickerView.selectByHsvColor(color)
lampInterface.onColorChanged(color)
}
SliderUtils.setSliderGradientNow(
satBar,
intArrayOf(
Color.WHITE,
HueUtils.hueToRGB(value.toInt()),
),
)
}
hueBar.addOnSliderTouchListener(
OnSliderTouchListener(this) { slider ->
hueAPI.changeHueOfGroup(lampInterface.id, slider.value.toInt())
},
)
satBar.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
val color = HueUtils.hueSatToRGB(hueBar.value.toInt(), value.toInt())
colorPickerView.selectByHsvColor(color)
lampInterface.onColorChanged(color)
}
}
satBar.addOnSliderTouchListener(
OnSliderTouchListener(this) { slider ->
hueAPI.changeSaturationOfGroup(lampInterface.id, slider.value.toInt())
},
)
setupColorPicker(hueAPI)
}
private fun setupTemperatureControls(hueAPI: HueAPI) {
ctBar.setLabelFormatter { value: Float ->
HueUtils.ctToKelvin(value.toInt() + MIN_COLOR_TEMPERATURE)
}
SliderUtils.setSliderGradient(
ctBar,
intArrayOf(
Color.WHITE,
"#FF8B16".toColorInt(),
),
)
ctBar.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
lampInterface.onColorChanged(HueUtils.ctToRGB(value.toInt() + MIN_COLOR_TEMPERATURE))
}
}
ctBar.addOnSliderTouchListener(
OnSliderTouchListener(this) { slider ->
hueAPI.changeColorTemperatureOfGroup(lampInterface.id, slider.value.toInt() + MIN_COLOR_TEMPERATURE)
},
)
}
companion object {
private const val LOADING_DELAY = 200L
private const val UPDATE_DELAY = 5000L
}
}

View file

@ -0,0 +1,225 @@
package io.github.domi04151309.home.fragments
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.core.graphics.toColorInt
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.slider.Slider
import com.skydoves.colorpickerview.ColorPickerView
import com.skydoves.colorpickerview.listeners.ColorListener
import io.github.domi04151309.home.R
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.HueUtils
import io.github.domi04151309.home.helpers.HueUtils.MIN_COLOR_TEMPERATURE
import io.github.domi04151309.home.helpers.SliderUtils
import io.github.domi04151309.home.interfaces.HueAdvancedLampInterface
import org.json.JSONObject
class HueColorSheet(private val lampInterface: HueAdvancedLampInterface) :
BottomSheetDialogFragment(),
Response.Listener<JSONObject> {
private lateinit var colorPickerView: ColorPickerView
private lateinit var ctText: TextView
private lateinit var ctBar: Slider
private lateinit var hueSatText: TextView
private lateinit var hueBar: Slider
private lateinit var satBar: Slider
private lateinit var briText: TextView
private lateinit var briBar: Slider
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val hueAPI = HueAPI(requireContext(), lampInterface.device.id)
val view = inflater.inflate(R.layout.fragment_hue_bri_color, container, false)
colorPickerView = view.findViewById(R.id.colorPickerView)
ctText = view.findViewById(R.id.ctTxt)
ctBar = view.findViewById(R.id.ctBar)
hueSatText = view.findViewById(R.id.hueSatTxt)
hueBar = view.findViewById(R.id.hueBar)
satBar = view.findViewById(R.id.satBar)
briText = view.findViewById(R.id.briTxt)
briBar = view.findViewById(R.id.briBar)
Volley.newRequestQueue(requireContext())
.add(
JsonObjectRequest(
Request.Method.GET,
"${lampInterface.addressPrefix}/lights/${lampInterface.id}",
null,
this,
) { error ->
Toast.makeText(
requireContext(),
Global.volleyError(requireContext(), error),
Toast.LENGTH_LONG,
).show()
},
)
setupColorControls(hueAPI)
setupTemperatureControls(hueAPI)
setupBrightnessControls(hueAPI)
return view
}
override fun onResponse(response: JSONObject) {
val availableInputs = arrayOf(colorPickerView, hueBar, satBar, ctBar, briBar)
val ctViews = arrayOf(ctText, ctBar)
val hueSatViews = arrayOf(colorPickerView, hueSatText, hueBar, satBar)
val briViews = arrayOf(briText, briBar)
val state = response.getJSONObject("state")
if (!state.has("ct")) {
ctViews.forEach {
it.visibility = View.GONE
}
} else {
ctViews.forEach {
it.visibility = View.VISIBLE
}
SliderUtils.setProgress(ctBar, state.getInt("ct") - MIN_COLOR_TEMPERATURE)
}
if (!state.has("hue") && !state.has("sat")) {
hueSatViews.forEach {
it.visibility = View.GONE
}
} else {
hueSatViews.forEach {
it.visibility = View.VISIBLE
}
colorPickerView.selectByHsvColor(
HueUtils.hueSatToRGB(
state.getInt("hue"),
state.getInt("sat"),
),
)
SliderUtils.setProgress(hueBar, state.getInt("hue"))
SliderUtils.setProgress(satBar, state.getInt("sat"))
}
if (!state.has("bri")) {
briViews.forEach {
it.visibility = View.GONE
}
} else {
briViews.forEach {
it.visibility = View.VISIBLE
}
SliderUtils.setProgress(briBar, state.getInt("bri"))
}
availableInputs.forEach {
it.isEnabled = state.optBoolean("on")
}
}
private fun setupColorControls(hueAPI: HueAPI) {
hueBar.setLabelFormatter { value: Float ->
HueUtils.hueToDegree(value.toInt())
}
satBar.setLabelFormatter { value: Float ->
HueUtils.satToPercent(value.toInt())
}
SliderUtils.setSliderGradient(
hueBar,
HueUtils.defaultColors(),
)
SliderUtils.setSliderGradient(
satBar,
intArrayOf(
Color.WHITE,
Color.RED,
),
)
hueBar.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
val color = HueUtils.hueSatToRGB(value.toInt(), satBar.value.toInt())
hueAPI.changeHue(lampInterface.id, value.toInt())
colorPickerView.selectByHsvColor(color)
lampInterface.onColorChanged(color)
lampInterface.onHueSatChanged(value.toInt(), satBar.value.toInt())
}
SliderUtils.setSliderGradientNow(
satBar,
intArrayOf(
Color.WHITE,
HueUtils.hueToRGB(
value.toInt(),
),
),
)
}
satBar.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
val color = HueUtils.hueSatToRGB(hueBar.value.toInt(), value.toInt())
hueAPI.changeSaturation(lampInterface.id, value.toInt())
colorPickerView.selectByHsvColor(color)
lampInterface.onColorChanged(color)
lampInterface.onHueSatChanged(hueBar.value.toInt(), value.toInt())
}
}
colorPickerView.setColorListener(
ColorListener { color, fromUser ->
if (fromUser) {
val hueSat = HueUtils.rgbToHueSat(color)
hueAPI.changeHueSat(lampInterface.id, hueSat[0], hueSat[1])
hueBar.value = hueSat[0].toFloat()
satBar.value = hueSat[1].toFloat()
lampInterface.onColorChanged(color)
lampInterface.onColorChanged(color)
lampInterface.onHueSatChanged(hueSat[0], hueSat[1])
}
},
)
}
private fun setupTemperatureControls(hueAPI: HueAPI) {
ctBar.setLabelFormatter { value: Float ->
HueUtils.ctToKelvin(value.toInt() + MIN_COLOR_TEMPERATURE)
}
SliderUtils.setSliderGradient(
ctBar,
intArrayOf(
Color.WHITE,
"#FF8B16".toColorInt(),
),
)
ctBar.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
hueAPI.changeColorTemperature(lampInterface.id, value.toInt() + MIN_COLOR_TEMPERATURE)
lampInterface.onColorChanged(HueUtils.ctToRGB(value.toInt() + MIN_COLOR_TEMPERATURE))
lampInterface.onCtChanged(value.toInt() + MIN_COLOR_TEMPERATURE)
}
}
}
private fun setupBrightnessControls(hueAPI: HueAPI) {
briBar.setLabelFormatter { value: Float ->
HueUtils.briToPercent(value.toInt())
}
briBar.addOnChangeListener { _, value, fromUser ->
if (fromUser) hueAPI.changeBrightness(lampInterface.id, value.toInt())
lampInterface.onBrightnessChanged(value.toInt())
}
}
}

View file

@ -0,0 +1,171 @@
package io.github.domi04151309.home.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.TextView
import androidx.core.graphics.toColorInt
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.RequestQueue
import com.android.volley.toolbox.Volley
import io.github.domi04151309.home.R
import io.github.domi04151309.home.adapters.HueLampListAdapter
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.data.DeviceItem
import io.github.domi04151309.home.data.ListViewItem
import io.github.domi04151309.home.helpers.HueUtils
import io.github.domi04151309.home.helpers.UpdateHandler
import io.github.domi04151309.home.interfaces.HueAdvancedLampInterface
import io.github.domi04151309.home.interfaces.HueRoomInterface
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
import org.json.JSONArray
import org.json.JSONObject
class HueLampsFragment(private var lampInterface: HueRoomInterface) :
Fragment(R.layout.fragment_hue_lamps),
RecyclerViewHelperInterface,
HueAdvancedLampInterface,
HueAPI.RequestCallback,
CompoundButton.OnCheckedChangeListener {
private lateinit var hueAPI: HueAPI
private lateinit var queue: RequestQueue
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: HueLampListAdapter
private val updateHandler: UpdateHandler = UpdateHandler()
override var id: String = ""
override var canReceiveRequest: Boolean = true
override lateinit var device: DeviceItem
override lateinit var addressPrefix: String
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
hueAPI = HueAPI(requireContext(), lampInterface.device.id)
queue = Volley.newRequestQueue(context)
device = lampInterface.device
addressPrefix = lampInterface.addressPrefix
recyclerView = super.onCreateView(inflater, container, savedInstanceState) as RecyclerView
adapter = HueLampListAdapter(this, this)
recyclerView.layoutManager = LinearLayoutManager(requireContext())
recyclerView.adapter = adapter
return recyclerView
}
override fun onStart() {
super.onStart()
updateHandler.setUpdateFunction {
if (lampInterface.canReceiveRequest && hueAPI.readyForRequest) {
hueAPI.loadLightsByIds(lampInterface.lights ?: JSONArray(), this)
}
}
}
override fun onStop() {
super.onStop()
updateHandler.stop()
}
override fun onItemClicked(
view: View,
position: Int,
) {
id = view.findViewById<TextView>(R.id.hidden).text.toString()
HueColorSheet(this).show(
requireActivity().supportFragmentManager,
HueColorSheet::class.simpleName,
)
}
@Suppress("CognitiveComplexMethod")
override fun onLightsLoaded(response: JSONObject?) {
if (response != null) {
var currentObject: JSONObject
var currentState: JSONObject
var state: Boolean?
val listItems: MutableList<ListViewItem> = mutableListOf()
val colorArray: MutableList<Int> = mutableListOf()
for (i in response.keys()) {
currentObject = response.optJSONObject(i) ?: JSONObject()
currentState = currentObject.optJSONObject("state") ?: JSONObject()
state = currentState.optBoolean("on")
colorArray +=
if (currentState.has("hue") && currentState.has("sat")) {
HueUtils.hueSatToRGB(
currentState.getInt("hue"),
currentState.getInt("sat"),
)
} else if (currentState.has("ct")) {
HueUtils.ctToRGB(currentState.getInt("ct"))
} else {
"#FFFFFF".toColorInt()
}
listItems +=
ListViewItem(
title = currentObject.optString("name"),
summary =
if (currentState.optBoolean("reachable")) {
resources.getString(R.string.hue_brightness) +
": " +
if (state) {
HueUtils.briToPercent(
currentState.optInt(
"bri",
HueUtils.MAX_BRIGHTNESS,
),
)
} else {
"0 %"
}
} else {
resources.getString(R.string.str_unreachable)
},
hidden = i,
state = state,
)
}
adapter.updateData(recyclerView, listItems, colorArray)
}
}
override fun onColorChanged(color: Int) {
// Do nothing.
}
override fun onBrightnessChanged(brightness: Int) {
// Do nothing.
}
override fun onHueSatChanged(
hue: Int,
sat: Int,
) {
// Do nothing.
}
override fun onCtChanged(ct: Int) {
// Do nothing.
}
override fun onCheckedChanged(
compoundButton: CompoundButton,
state: Boolean,
) {
if (compoundButton.isPressed) {
hueAPI.switchLightById(
(compoundButton.parent as ViewGroup).findViewById<TextView>(R.id.hidden).text.toString(),
state,
)
}
}
}

View file

@ -0,0 +1,261 @@
package io.github.domi04151309.home.fragments
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.ContextMenu
import android.view.LayoutInflater
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.github.domi04151309.home.R
import io.github.domi04151309.home.activities.HueSceneActivity
import io.github.domi04151309.home.adapters.HueSceneGridAdapter
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.custom.CustomJsonArrayRequest
import io.github.domi04151309.home.data.SceneGridItem
import io.github.domi04151309.home.helpers.ColorUtils
import io.github.domi04151309.home.helpers.Global
import io.github.domi04151309.home.helpers.HueUtils
import io.github.domi04151309.home.interfaces.HueLampInterface
import io.github.domi04151309.home.interfaces.RecyclerViewHelperInterface
import org.json.JSONException
import org.json.JSONObject
class HueScenesFragment(private var lampInterface: HueLampInterface) :
Fragment(R.layout.fragment_hue_scenes),
RecyclerViewHelperInterface,
Response.Listener<JSONObject> {
private var scenesRequest: JsonObjectRequest? = null
private var selectedScene: CharSequence = ""
private var selectedSceneName: CharSequence = ""
private lateinit var hueAPI: HueAPI
private lateinit var queue: RequestQueue
private lateinit var adapter: HueSceneGridAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
hueAPI = HueAPI(requireContext(), lampInterface.device.id)
queue = Volley.newRequestQueue(context)
val recyclerView = super.onCreateView(inflater, container, savedInstanceState) as RecyclerView
adapter = HueSceneGridAdapter(this, this)
recyclerView.layoutManager = GridLayoutManager(requireContext(), COLUMNS)
recyclerView.adapter = adapter
scenesRequest =
JsonObjectRequest(
Request.Method.GET, lampInterface.addressPrefix + SCENES_PATH, null,
this,
) { error ->
Toast.makeText(
requireContext(),
Global.volleyError(requireContext(), error),
Toast.LENGTH_LONG,
).show()
}
queue.add(scenesRequest)
return recyclerView
}
override fun onResponse(response: JSONObject) {
try {
val gridItems: ArrayList<SceneGridItem> = ArrayList(response.length())
val scenes: List<Pair<String, String>> = getScenes(response)
if (scenes.isNotEmpty()) {
var completedRequests = 0
for (i in scenes.indices) {
queue.add(
JsonObjectRequest(
Request.Method.GET,
lampInterface.addressPrefix + SCENES_PATH + scenes[i].first,
null,
{ sceneResponse ->
gridItems +=
SceneGridItem(
name = scenes[i].second,
hidden = scenes[i].first,
color = getSceneColor(sceneResponse),
)
completedRequests++
if (completedRequests == scenes.size) {
val sortedItems =
gridItems.sortedWith(compareBy { it.color })
.toMutableList()
sortedItems +=
SceneGridItem(
name = resources.getString(R.string.hue_add_scene),
hidden = "add",
)
adapter.updateData(sortedItems)
}
},
{ error ->
Log.e(Global.LOG_TAG, error.toString())
},
),
)
}
} else {
adapter.updateData(
mutableListOf(
SceneGridItem(
name = resources.getString(R.string.hue_add_scene),
hidden = "add",
),
),
)
}
} catch (e: JSONException) {
Log.e(Global.LOG_TAG, e.toString())
}
}
private fun getScenes(response: JSONObject): List<Pair<String, String>> {
val scenes: ArrayList<Pair<String, String>> =
ArrayList(
response.length() / SCENE_FRACTION_ESTIMATE,
)
var currentObject: JSONObject
for (i in response.keys()) {
currentObject = response.getJSONObject(i)
if (currentObject.optString("group") == lampInterface.id) {
scenes.add(Pair(i, currentObject.getString("name")))
}
}
return scenes
}
private fun getSceneColor(response: JSONObject): Int {
val states = response.getJSONObject("lightstates")
val currentSceneValues = ArrayList<Int>(states.length())
var lampObject: JSONObject
for (j in states.keys()) {
lampObject = states.getJSONObject(j)
if (lampObject.getBoolean("on")) {
if (lampObject.has("hue") && lampObject.has("sat")) {
currentSceneValues.clear()
currentSceneValues.add(
HueUtils.hueSatToRGB(
lampObject.getInt("hue"),
lampObject.getInt("sat"),
),
)
} else if (lampObject.has("xy")) {
val xyArray = lampObject.getJSONArray("xy")
currentSceneValues.clear()
currentSceneValues.add(
ColorUtils.xyToRGB(
xyArray.getDouble(0),
xyArray.getDouble(1),
),
)
} else if (lampObject.has("ct")) {
currentSceneValues.add(
HueUtils.ctToRGB(lampObject.getInt("ct")),
)
}
}
}
return if (currentSceneValues.isNotEmpty()) {
currentSceneValues[0]
} else {
Color.WHITE
}
}
override fun onItemClicked(
view: View,
position: Int,
) {
val hiddenText = view.findViewById<TextView>(R.id.hidden).text.toString()
if (hiddenText == "add") {
startActivity(
Intent(requireContext(), HueSceneActivity::class.java).putExtra(
"deviceId",
lampInterface.device.id,
).putExtra("room", lampInterface.id),
)
} else {
hueAPI.activateSceneOfGroup(lampInterface.id, hiddenText)
}
}
override fun onCreateContextMenu(
menu: ContextMenu,
v: View,
menuInfo: ContextMenu.ContextMenuInfo?,
) {
super.onCreateContextMenu(menu, v, menuInfo)
selectedScene = v.findViewById<TextView>(R.id.hidden).text
selectedSceneName = v.findViewById<TextView>(R.id.title).text
if (selectedScene != "add") MenuInflater(requireContext()).inflate(R.menu.activity_hue_lamp_context, menu)
}
override fun onContextItemSelected(item: MenuItem): Boolean =
when (item.title) {
resources.getString(R.string.str_edit) -> {
startActivity(
Intent(requireContext(), HueSceneActivity::class.java)
.putExtra("deviceId", lampInterface.device.id)
.putExtra("room", lampInterface.id)
.putExtra("scene", selectedScene),
)
true
}
resources.getString(R.string.str_delete) -> {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.str_delete)
.setMessage(R.string.hue_delete_scene)
.setPositiveButton(R.string.str_delete) { _, _ ->
val deleteSceneRequest =
CustomJsonArrayRequest(
Request.Method.DELETE,
lampInterface.addressPrefix + SCENES_PATH + selectedScene,
null,
{ queue.add(scenesRequest) },
{ e -> Log.e(Global.LOG_TAG, e.toString()) },
)
queue.add(deleteSceneRequest)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
true
}
else -> {
super.onContextItemSelected(item)
}
}
override fun onStart() {
super.onStart()
if (scenesChanged) {
scenesChanged = false
queue.add(scenesRequest)
}
}
companion object {
private const val SCENE_FRACTION_ESTIMATE = 4
private const val COLUMNS = 3
private const val SCENES_PATH = "/scenes/"
var scenesChanged: Boolean = false
}
}

View file

@ -0,0 +1,62 @@
package io.github.domi04151309.home.helpers
import android.graphics.Color
import kotlin.math.ln
import kotlin.math.pow
@Suppress("MagicNumber")
object ColorUtils {
private const val MAX: Double = 255.0
private const val MIN: Double = 0.0
fun temperatureToRGB(kelvin: Int): Int {
val temp = kelvin / 100.0
val red: Double
val green: Double
val blue: Double
if (temp <= 66) {
red = MAX
green = 99.4708025861 * ln(temp) - 161.1195681661
blue =
if (temp <= 19) {
MIN
} else {
138.5177312231 * ln(temp - 10) - 305.0447927307
}
} else {
red = 329.698727446 * (temp - 60).pow(-0.1332047592)
green = 288.1221695283 * (temp - 60).pow(-0.0755148492)
blue = MAX
}
return Color.rgb(clamp(red), clamp(green), clamp(blue))
}
fun xyToRGB(
x: Double,
y: Double,
): Int {
val cieY = 1.0
val cieX = cieY * x / y
val cieZ = (1 - x - y) * cieY / y
val r = +3.2404542 * cieX - 1.5371385 * cieY - 0.4985314 * cieZ
val g = -0.9692660 * cieX + 1.8760108 * cieY + 0.0415560 * cieZ
val b = +0.0556434 * cieX - 0.2040259 * cieY + 1.0572252 * cieZ
return Color.rgb(formatXyzValue(r), formatXyzValue(g), formatXyzValue(b))
}
private fun formatXyzValue(v: Double): Int =
clamp(
(if (v <= 0.0031308) 12.92 * v else 1.055 * v.pow(1.0 / 2.4) - 0.055) * MAX,
)
private fun clamp(value: Double): Int =
when {
value < MIN -> MIN.toInt()
value > MAX -> MAX.toInt()
else -> value.toInt()
}
}

View file

@ -0,0 +1,54 @@
package io.github.domi04151309.home.helpers
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import org.json.JSONObject
class DeviceSecrets(context: Context, private val id: String) {
private val masterKeyAlias =
MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
private val preferences: SharedPreferences =
EncryptedSharedPreferences.create(
context,
"device_secrets",
masterKeyAlias,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,
)
private val secrets =
JSONObject(
preferences.getString(id, DEFAULT_JSON)
?: DEFAULT_JSON,
)
var username: String
get() = secrets.optString("username")
set(value) {
secrets.put("username", value)
}
var password: String
get() = secrets.optString("password")
set(value) {
secrets.put("password", value)
}
fun updateDeviceSecrets() {
preferences.edit { putString(id, secrets.toString()) }
}
fun deleteDeviceSecrets() {
preferences.edit { remove(id) }
}
companion object {
private const val DEFAULT_JSON = """{ "username": "", "password": "" }"""
}
}

View file

@ -0,0 +1,146 @@
package io.github.domi04151309.home.helpers
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import io.github.domi04151309.home.data.DeviceItem
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.util.Random
@Suppress("TooManyFunctions")
class Devices(private val context: Context) {
private val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val length: Int get() = deviceOrder.length()
private val data: JSONObject get() {
if (storedData == null) {
storedData =
try {
JSONObject(
preferences.getString("devices_json", Global.DEFAULT_JSON)
?: Global.DEFAULT_JSON,
)
} catch (e: JSONException) {
Log.w(Devices::class.simpleName, e)
JSONObject(Global.DEFAULT_JSON)
}
}
return storedData!!
}
private val devicesObject: JSONObject get() = data.optJSONObject("devices") ?: JSONObject()
private val deviceOrder: JSONArray get() {
if (!data.has(ORDER)) {
data.put(ORDER, devicesObject.names() ?: JSONArray())
}
return data.getJSONArray(ORDER)
}
private fun generateRandomId(): String {
val random = Random()
val builder = StringBuilder(ID_LENGTH)
for (index in 0 until ID_LENGTH) {
builder.append(ALLOWED_CHARACTERS[random.nextInt(ALLOWED_CHARACTERS.length)])
}
return builder.toString()
}
private fun convertToDeviceItem(id: String): DeviceItem {
val json = devicesObject.optJSONObject(id) ?: JSONObject()
val device =
DeviceItem(
id,
json.optString("name"),
json.optString("mode"),
json.optString("icon"),
json.optBoolean("hide", false),
json.optBoolean("direct_view", false),
)
device.address = json.optString(ADDRESS)
return device
}
fun getDeviceById(id: String): DeviceItem = convertToDeviceItem(id)
fun getDeviceByIndex(index: Int): DeviceItem = convertToDeviceItem(deviceOrder.getString(index))
fun idExists(id: String): Boolean = devicesObject.has(id)
fun addressExists(address: String): Boolean {
val formattedAddress = DeviceItem.formatAddress(address)
for (i in devicesObject.keys()) {
if (devicesObject.getJSONObject(i).optString(ADDRESS) == formattedAddress) {
return true
}
}
return false
}
fun generateNewId(): String {
var id = generateRandomId()
while (devicesObject.has(id)) id = generateRandomId()
return id
}
fun addDevice(device: DeviceItem) {
if (!idExists(device.id)) deviceOrder.put(device.id)
val deviceObject =
JSONObject()
.put("name", device.name)
.put(ADDRESS, device.address)
.put("mode", device.mode)
.put("icon", device.iconName)
.put("hide", device.hide)
.put("direct_view", device.directView)
devicesObject.put(device.id, deviceObject)
saveChanges()
}
fun deleteDevice(id: String) {
for (i in 0 until deviceOrder.length()) {
if (deviceOrder[i] == id) {
deviceOrder.remove(i)
break
}
}
devicesObject.remove(id)
saveChanges()
DeviceSecrets(context, id).deleteDeviceSecrets()
}
fun moveDevice(
from: Int,
to: Int,
) {
val list =
MutableList(deviceOrder.length()) {
deviceOrder.getString(it)
}
list.add(to, list.removeAt(from))
data.put(ORDER, JSONArray(list))
}
fun saveChanges() {
preferences.edit { putString("devices_json", data.toString()) }
}
companion object {
const val INTENT_EXTRA_DEVICE: String = "device"
private const val ALLOWED_CHARACTERS = "0123456789abcdefghijklmnobqrstuvw"
private const val ID_LENGTH = 8
private const val ORDER = "order"
private const val ADDRESS = "address"
private var storedData: JSONObject? = null
fun reloadFromPreferences() {
storedData = null
}
}
}

View file

@ -0,0 +1,157 @@
package io.github.domi04151309.home.helpers
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.service.controls.DeviceTypes
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.android.volley.ClientError
import com.android.volley.NoConnectionError
import com.android.volley.ParseError
import com.android.volley.TimeoutError
import io.github.domi04151309.home.R
import io.github.domi04151309.home.api.EspEasyAPI
import io.github.domi04151309.home.api.HueAPI
import io.github.domi04151309.home.api.ShellyAPI
import io.github.domi04151309.home.api.SimpleHomeAPI
import io.github.domi04151309.home.api.Tasmota
import io.github.domi04151309.home.api.UnifiedAPI
import io.github.domi04151309.home.interfaces.HomeRecyclerViewHelperInterface
internal object Global {
const val LOG_TAG: String = "HomeApp"
const val DEFAULT_JSON: String = "{\"devices\":{}}"
const val ESP_EASY = "ESP Easy"
const val HUE_API = "Hue API"
const val SHELLY_GEN_1 = "Shelly Gen 1"
const val SHELLY_GEN_2 = "Shelly Gen 2"
const val SHELLY_GEN_3 = "Shelly Gen 3"
const val SIMPLE_HOME_API = "SimpleHome API"
const val TASMOTA = "Tasmota"
const val NODE_RED = "Node-RED"
const val WEBSITE = "Website"
const val FRITZ_AUTO_LOGIN = "Fritz! Auto-Login"
const val GRAFANA_AUTO_LOGIN = "Grafana Auto-Login"
const val PI_HOLE_AUTO_LOGIN = "Pi-hole Auto-Login"
val UNIFIED_MODES =
arrayOf(
ESP_EASY,
HUE_API,
SHELLY_GEN_1,
SHELLY_GEN_2,
SHELLY_GEN_3,
SIMPLE_HOME_API,
TASMOTA,
)
val POWER_MENU_MODES =
arrayOf(
ESP_EASY,
HUE_API,
SHELLY_GEN_1,
SHELLY_GEN_2,
SHELLY_GEN_3,
SIMPLE_HOME_API,
TASMOTA,
)
fun getCorrectAPI(
context: Context,
identifier: String,
deviceId: String,
recyclerViewInterface: HomeRecyclerViewHelperInterface? = null,
tasmotaHelperInterface: HomeRecyclerViewHelperInterface? = null,
): UnifiedAPI =
when (identifier) {
ESP_EASY -> EspEasyAPI(context, deviceId, recyclerViewInterface)
HUE_API -> HueAPI(context, deviceId, recyclerViewInterface)
SIMPLE_HOME_API -> SimpleHomeAPI(context, deviceId, recyclerViewInterface)
TASMOTA -> Tasmota(context, deviceId, tasmotaHelperInterface ?: recyclerViewInterface)
SHELLY_GEN_1 -> ShellyAPI(context, deviceId, recyclerViewInterface, 1)
SHELLY_GEN_2 -> ShellyAPI(context, deviceId, recyclerViewInterface, 2)
SHELLY_GEN_3 -> ShellyAPI(context, deviceId, recyclerViewInterface, 2)
else -> UnifiedAPI(context, deviceId, recyclerViewInterface)
}
@Suppress("CyclomaticComplexMethod")
fun getIcon(
icon: String,
default: Int = R.drawable.ic_warning,
): Int =
when (icon.lowercase()) {
"christmas tree" -> R.drawable.ic_device_christmas_tree
"clock" -> R.drawable.ic_device_clock
"display" -> R.drawable.ic_device_display
"display alt" -> R.drawable.ic_device_display_alt
"docker" -> R.drawable.ic_device_docker
"electricity" -> R.drawable.ic_device_electricity
"entertainment" -> R.drawable.ic_device_speaker
"gauge" -> R.drawable.ic_device_gauge
"grafana" -> R.drawable.ic_device_grafana
"heating" -> R.drawable.ic_device_thermometer
"hygrometer" -> R.drawable.ic_device_hygrometer
"lamp" -> R.drawable.ic_device_lamp
"lights" -> R.drawable.ic_device_lamp
"raspberry pi" -> R.drawable.ic_device_raspberry_pi
"raspberry pi alt" -> R.drawable.ic_device_raspberry_pi_alt
"router" -> R.drawable.ic_device_router
"speaker" -> R.drawable.ic_device_speaker
"schwibbogen" -> R.drawable.ic_device_schwibbogen
"stack" -> R.drawable.ic_device_stack
"socket" -> R.drawable.ic_device_socket
"thermometer" -> R.drawable.ic_device_thermometer
"webcam" -> R.drawable.ic_device_webcam
else -> default
}
@RequiresApi(Build.VERSION_CODES.R)
fun getDeviceType(icon: String): Int =
when (icon.lowercase()) {
"christmas tree", "electricity", "schwibbogen", "socket" -> DeviceTypes.TYPE_OUTLET
"display", "display alt" -> DeviceTypes.TYPE_DISPLAY
"gauge", "heating", "thermometer" -> DeviceTypes.TYPE_AC_HEATER
"hygrometer" -> DeviceTypes.TYPE_HUMIDIFIER
"lamp", "lights" -> DeviceTypes.TYPE_LIGHT
"webcam" -> DeviceTypes.TYPE_CAMERA
else -> DeviceTypes.TYPE_UNKNOWN
}
fun checkNetwork(context: Context): Boolean {
if (
!PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean("safety_checks", true)
) {
return true
}
val connectivityManager =
context.getSystemService(
AppCompatActivity.CONNECTIVITY_SERVICE,
) as ConnectivityManager
val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
return if (capabilities != null) {
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)
} else {
true
}
}
fun volleyError(
c: Context,
error: java.lang.Exception,
): String {
Log.w(LOG_TAG, error)
return when (error) {
is TimeoutError, is NoConnectionError -> c.resources.getString(R.string.main_device_unavailable)
is ParseError -> c.resources.getString(R.string.main_parse_error)
is ClientError -> c.resources.getString(R.string.main_client_error)
else -> c.resources.getString(R.string.main_device_unavailable)
}
}
}

View file

@ -0,0 +1,20 @@
package io.github.domi04151309.home.helpers
import io.github.domi04151309.home.data.LightStates
class HueLightListener {
private var listeners = mutableListOf<(data: LightStates.Light) -> Unit>()
var state: LightStates.Light = LightStates.Light()
set(value) {
if (value != field) {
field = value
listeners.forEach {
it(value)
}
}
}
fun addOnDataChangedListener(listener: (data: LightStates.Light) -> Unit) {
listeners.add(listener)
}
}

View file

@ -0,0 +1,66 @@
package io.github.domi04151309.home.helpers
import android.graphics.Color
@Suppress("MagicNumber")
object HueUtils {
const val MIN_COLOR_TEMPERATURE: Int = 153
const val MAX_BRIGHTNESS: Int = 255
private const val ARGUMENT_OUT_OF_RANGE = "Argument out of range."
fun defaultColors(): IntArray =
IntArray(7) {
index ->
Color.HSVToColor(floatArrayOf(index * 60f, 1f, 1f))
}
fun ctToRGB(ct: Int): Int {
require(!(ct < MIN_COLOR_TEMPERATURE || ct > 500)) { ARGUMENT_OUT_OF_RANGE }
return ColorUtils.temperatureToRGB((6500 - 12.968299711 * (ct - MIN_COLOR_TEMPERATURE)).toInt())
}
fun ctToKelvin(ct: Int): String {
require(!(ct < MIN_COLOR_TEMPERATURE || ct > 500)) { ARGUMENT_OUT_OF_RANGE }
return "${(6500 - 12.968299711 * (ct - MIN_COLOR_TEMPERATURE)).toInt()} K"
}
fun hueSatToRGB(
hue: Int,
sat: Int,
): Int {
require(!(hue > 65_535 || sat > 254)) { ARGUMENT_OUT_OF_RANGE }
return Color.HSVToColor(floatArrayOf(hue * 0.005493248F, sat / 254F, 1F))
}
fun hueToRGB(hue: Int): Int {
require(hue <= 65_535) { ARGUMENT_OUT_OF_RANGE }
return Color.HSVToColor(floatArrayOf(hue * 0.005493248F, 1F, 1F))
}
fun hueToDegree(hue: Int): String {
require(hue <= 65_535) { ARGUMENT_OUT_OF_RANGE }
return "${(hue * 0.005493248F).toInt()}°"
}
fun satToPercent(sat: Int): String {
require(sat <= 254) { ARGUMENT_OUT_OF_RANGE }
return "${(sat / 254F * 100).toInt()} %"
}
fun briToPercent(bri: Int): String =
when {
bri < 1 -> "0 %"
bri > 254 -> "100 %"
else -> "${(bri / 254F * 100).toInt()} %"
}
fun rgbToHueSat(color: Int): IntArray {
val hsv = FloatArray(3)
Color.colorToHSV(color, hsv)
return intArrayOf(
(hsv[0] / 0.0054932478).toInt(),
(hsv[1] * 254).toInt(),
)
}
}

View file

@ -0,0 +1,10 @@
@file:Suppress("HardCodedStringLiteral")
package io.github.domi04151309.home.helpers
internal object P {
const val PREF_COLUMNS = "columns"
const val PREF_COLUMNS_DEFAULT = "auto"
const val PREF_CONTROLS_AUTH = "controls_auth"
const val PREF_CONTROLS_AUTH_DEFAULT = false
}

View file

@ -0,0 +1,68 @@
package io.github.domi04151309.home.helpers
import android.animation.ObjectAnimator
import android.content.res.Resources
import android.graphics.LinearGradient
import android.graphics.Shader
import android.graphics.drawable.LayerDrawable
import android.graphics.drawable.PaintDrawable
import android.view.View
import android.view.animation.DecelerateInterpolator
import com.google.android.material.slider.Slider
object SliderUtils {
private const val CORNER_RADIUS = 16
private const val MARGIN_VERTICAL = 16
private const val MARGIN_HORIZONTAL = 14
private const val ANIMATION_DURATION = 300L
private fun dpToPx(
resources: Resources,
dp: Int,
): Int = (dp * resources.displayMetrics.density).toInt()
fun setSliderGradientNow(
view: View,
colors: IntArray,
) {
val gradient = PaintDrawable()
gradient.setCornerRadius(dpToPx(view.resources, CORNER_RADIUS).toFloat())
gradient.paint.shader =
LinearGradient(
0f, 0f,
view.width.toFloat(), 0f,
colors,
null,
Shader.TileMode.CLAMP,
)
val layers = LayerDrawable(arrayOf(gradient))
layers.setLayerInset(
0,
dpToPx(view.resources, MARGIN_HORIZONTAL),
dpToPx(view.resources, MARGIN_VERTICAL),
dpToPx(view.resources, MARGIN_HORIZONTAL),
dpToPx(view.resources, MARGIN_VERTICAL),
)
view.background = layers
}
fun setSliderGradient(
view: View,
colors: IntArray,
) {
view.post {
setSliderGradientNow(view, colors)
}
}
fun setProgress(
slider: Slider,
value: Int,
) {
val animation = ObjectAnimator.ofFloat(slider, "value", value.toFloat())
animation.duration = ANIMATION_DURATION
animation.interpolator = DecelerateInterpolator()
animation.start()
}
}

View file

@ -0,0 +1,149 @@
package io.github.domi04151309.home.helpers
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.github.domi04151309.home.R
import io.github.domi04151309.home.api.UnifiedAPI
import org.json.JSONArray
import org.json.JSONObject
class TasmotaHelper(private val c: Context, private val tasmota: UnifiedAPI) {
private val prefs = PreferenceManager.getDefaultSharedPreferences(c)
private val nullParent: ViewGroup? = null
fun updateItem(
callback: UnifiedAPI.CallbackInterface,
index: Int,
) {
val array = JSONArray(prefs.getString(tasmota.deviceId, EMPTY_ARRAY))
val arrayItem = array.optJSONObject(index) ?: JSONObject()
val view = LayoutInflater.from(c).inflate(R.layout.dialog_tasmota_add, nullParent, false)
val titleTxt = view.findViewById<EditText>(R.id.title)
val commandTxt = view.findViewById<EditText>(R.id.command)
titleTxt.setText(arrayItem.optString(TITLE))
commandTxt.setText(arrayItem.optString(COMMAND))
MaterialAlertDialogBuilder(c)
.setTitle(R.string.tasmota_add_command)
.setView(view)
.setPositiveButton(android.R.string.ok) { _, _ ->
val newTitle = titleTxt.text.toString()
val newCommand = commandTxt.text.toString()
array.remove(index)
prefs.edit {
putString(
tasmota.deviceId,
array.put(
JSONObject()
.put(
TITLE,
if (newTitle == "") {
c.resources.getString(R.string.tasmota_add_command_dialog_title_empty)
} else {
newTitle
},
)
.put(
COMMAND,
if (newCommand == "") {
c.resources.getString(
R.string.tasmota_add_command_dialog_command_empty,
)
} else {
newCommand
},
),
).toString(),
)
}
tasmota.loadList(callback)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
}
fun addToList(
callback: UnifiedAPI.CallbackInterface,
title: String = "",
command: String = "",
) {
val view = LayoutInflater.from(c).inflate(R.layout.dialog_tasmota_add, nullParent, false)
val titleTxt = view.findViewById<EditText>(R.id.title)
val commandTxt = view.findViewById<EditText>(R.id.command)
titleTxt.setText(title)
commandTxt.setText(command)
MaterialAlertDialogBuilder(c)
.setTitle(R.string.tasmota_add_command)
.setView(view)
.setPositiveButton(android.R.string.ok) { _, _ ->
val newTitle = titleTxt.text.toString()
val newCommand = commandTxt.text.toString()
prefs.edit {
putString(
tasmota.deviceId,
JSONArray(
prefs.getString(tasmota.deviceId, EMPTY_ARRAY),
).put(
JSONObject()
.put(
TITLE,
if (newTitle == "") {
c.resources.getString(
R.string.tasmota_add_command_dialog_title_empty,
)
} else {
newTitle
},
)
.put(
COMMAND,
if (newCommand == "") {
c.resources.getString(
R.string.tasmota_add_command_dialog_command_empty,
)
} else {
newCommand
},
),
).toString(),
)
}
tasmota.loadList(callback)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
}
fun removeFromList(
callback: UnifiedAPI.CallbackInterface,
index: Int,
) {
val array = JSONArray(prefs.getString(tasmota.deviceId, EMPTY_ARRAY))
array.remove(index)
prefs.edit { putString(tasmota.deviceId, array.toString()) }
tasmota.loadList(callback)
}
fun executeOnce(callback: UnifiedAPI.CallbackInterface) {
val view = LayoutInflater.from(c).inflate(R.layout.dialog_tasmota_execute_once, nullParent, false)
val command = view.findViewById<EditText>(R.id.command)
MaterialAlertDialogBuilder(c)
.setTitle(R.string.tasmota_execute_once)
.setView(view)
.setPositiveButton(android.R.string.ok) { _, _ ->
tasmota.execute(command.text.toString(), callback)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
}
companion object {
const val EMPTY_ARRAY: String = "[]"
private const val TITLE = "title"
private const val COMMAND = "command"
}
}

View file

@ -0,0 +1,32 @@
package io.github.domi04151309.home.helpers
import android.os.Handler
import android.os.Looper
class UpdateHandler : Handler(Looper.getMainLooper()) {
var running: Boolean = false
private set
fun setUpdateFunction(function: () -> Unit) {
removeCallbacksAndMessages(null)
postDelayed(
object : Runnable {
override fun run() {
function()
postDelayed(this, UPDATE_DELAY)
}
},
0,
)
running = true
}
fun stop() {
running = false
removeCallbacksAndMessages(null)
}
companion object {
private const val UPDATE_DELAY = 1000L
}
}

View file

@ -0,0 +1,17 @@
package io.github.domi04151309.home.interfaces
import android.view.View
import io.github.domi04151309.home.data.ListViewItem
interface HomeRecyclerViewHelperInterface {
fun onItemClicked(
view: View,
data: ListViewItem,
)
fun onStateChanged(
view: View,
data: ListViewItem,
state: Boolean,
)
}

Some files were not shown because too many files have changed in this diff Show more