diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..f257bb2 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,35 @@ +name: New Release +on: + workflow_dispatch: + +jobs: + release: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Init values + id: values + run: | + version=$(grep -Po -m 1 '(?<=versionName ).*' ./app/build.gradle | tr -d \") + echo ::set-output name=name::DualWallpaper v$version + echo ::set-output name=tag::v$version + echo ::set-output name=apk::DualWallpaper_v$version + - name: Create release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + release_name: ${{ steps.values.outputs.name }} + tag_name: ${{ steps.values.outputs.tag }} + body: No changelog + - name: Add apk + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./app/release/app-release.apk + asset_name: ${{ steps.values.outputs.apk }}.apk + asset_content_type: application/vnd.android.package-archive diff --git a/README.md b/README.md new file mode 100644 index 0000000..f97e932 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# DualWallpaper +Customize your device even more by having two separate sets of wallpapers for light and dark mode. When your device's theme changes, this app will change your wallpaper to the one you've selected accordingly. To handle the change this app offers two possibilities: ``Wallpaper Service`` and ``Live Wallpaper``. In my opinion the first option is more reliable and works better but in some cases the app might get killed by the system and in this case I'd suggest using the second one. You have to see for yourself which option works best on your device. You can download and install the latest apk [here](https://github.com/Yanndroid/DualWallpaper/raw/master/app/release/app-release.apk), future updates are available directly via the app. + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore index 42afabf..65d12b9 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1 +1,2 @@ -/build \ No newline at end of file +/build +google-services.json \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index ded6235..b62df32 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,6 @@ plugins { id 'com.android.application' + id 'com.google.gms.google-services' } android { @@ -10,7 +11,7 @@ android { minSdk 26 targetSdk 32 versionCode 1 - versionName "1.0" + versionName "1.0.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -30,12 +31,18 @@ android { configurations.all { exclude group: 'androidx.appcompat', module: 'appcompat' exclude group: 'androidx.core', module: 'core' - //exclude group: 'androidx.drawerlayout', module: 'drawerlayout' - //exclude group: 'androidx.viewpager', module: 'viewpager' + exclude group: 'androidx.drawerlayout', module: 'drawerlayout' + exclude group: 'androidx.viewpager', module: 'viewpager' + exclude group: 'androidx.fragment', module: 'fragment' + exclude group: 'androidx.customview', module: 'customview' + exclude group: 'androidx.coordinatorlayout', module: 'coordinatorlayout' } dependencies { - implementation 'io.github.oneuiproject:lib:1.0.0' - implementation 'io.github.oneuiproject.sesl:appcompat:1.0.0' - implementation 'io.github.oneuiproject.sesl:material:1.0.0' + implementation 'io.github.oneuiproject:design:1.0.0' + implementation 'io.github.oneuiproject.sesl:appcompat:1.2.1' + implementation 'io.github.oneuiproject.sesl:material:1.2.0' + implementation 'io.github.oneuiproject.sesl:preference:1.1.0' + implementation 'io.github.oneuiproject.sesl:recyclerview:1.1.0' + implementation 'com.google.firebase:firebase-database:20.0.5' } \ No newline at end of file diff --git a/app/release/app-release.apk b/app/release/app-release.apk new file mode 100644 index 0000000..454c88b Binary files /dev/null and b/app/release/app-release.apk differ diff --git a/app/release/output-metadata.json b/app/release/output-metadata.json new file mode 100644 index 0000000..208c9fc --- /dev/null +++ b/app/release/output-metadata.json @@ -0,0 +1,20 @@ +{ + "version": 3, + "artifactType": { + "type": "APK", + "kind": "Directory" + }, + "applicationId": "de.dlyt.yanndroid.dualwallpaper", + "variantName": "release", + "elements": [ + { + "type": "SINGLE", + "filters": [], + "attributes": [], + "versionCode": 1, + "versionName": "1.0.0", + "outputFile": "app-release.apk" + } + ], + "elementType": "File" +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 619cf72..b0d0d6e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,9 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/BootReceiver.java b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/BootReceiver.java index f8df87f..311be85 100644 --- a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/BootReceiver.java +++ b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/BootReceiver.java @@ -5,11 +5,13 @@ import android.content.Intent; import android.util.Log; +import androidx.preference.PreferenceManager; + public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.e("BootReceiver", intent.getAction()); - if (context.getSharedPreferences("sp", Context.MODE_PRIVATE).getBoolean("use_service_switch", false)) + if (PreferenceManager.getDefaultSharedPreferences(context).getString("main_pref", "off").equals("wps")) context.startForegroundService(new Intent(context, WallpaperService.class)); } } diff --git a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/LiveWallpaper.java b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/LiveWallpaper.java index e61fed9..8f2a18d 100644 --- a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/LiveWallpaper.java +++ b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/LiveWallpaper.java @@ -11,6 +11,8 @@ import android.service.wallpaper.WallpaperService; import android.view.SurfaceHolder; +import java.io.File; + public class LiveWallpaper extends WallpaperService { private WallpaperUtil wallpaperUtil; @@ -26,21 +28,16 @@ public Engine onCreateEngine() { @Override public void onConfigurationChanged(Configuration newConfig) { if (uiMode != newConfig.uiMode) { - //dynamicThemeEngine.update(newConfig); wallpaperUtil.loadWallpaper(false, newConfig.uiMode != 33); uiMode = newConfig.uiMode; } dynamicThemeEngine.update(newConfig); - //wallpaperUtil.loadWallpaper(false, newConfig.uiMode != 33); } private class DynamicThemeEngine extends Engine { private final Handler handler = new Handler(); - private final Runnable runnable = this::draw; private boolean lightMode, portrait; - - private boolean visible = true; - + private boolean visible = true; private final Runnable runnable = this::draw; public DynamicThemeEngine(Context context) { update(context.getResources().getConfiguration()); } @@ -54,9 +51,11 @@ public void update(Configuration config) { private void draw() { SurfaceHolder holder = getSurfaceHolder(); Canvas canvas = null; + String wallpaperPath = wallpaperUtil.getWallpaperPath(true, lightMode); + if (!new File(wallpaperPath).exists()) return; try { canvas = holder.lockCanvas(); - Bitmap bMap = BitmapFactory.decodeFile(wallpaperUtil.getWallpaperPath(true, lightMode)); + Bitmap bMap = BitmapFactory.decodeFile(wallpaperPath); Rect surfaceFrame = holder.getSurfaceFrame(); int cropH = !portrait ? 0 : (bMap.getWidth() - ((bMap.getHeight() / surfaceFrame.height()) * surfaceFrame.width())) / 2; @@ -90,6 +89,10 @@ public void onSurfaceDestroyed(SurfaceHolder holder) { this.visible = false; handler.removeCallbacks(runnable); } + + + + } } \ No newline at end of file diff --git a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/WallpaperUtil.java b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/WallpaperUtil.java index 0d6e764..5d2ff9f 100644 --- a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/WallpaperUtil.java +++ b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/WallpaperUtil.java @@ -4,9 +4,12 @@ import android.app.WallpaperManager; import android.content.Context; import android.content.pm.PackageManager; +import android.net.Uri; import android.os.ParcelFileDescriptor; +import android.widget.Toast; import androidx.core.app.ActivityCompat; +import androidx.preference.PreferenceManager; import java.io.File; import java.io.FileInputStream; @@ -26,9 +29,21 @@ public WallpaperUtil(Context context) { this.wallpaperManager = WallpaperManager.getInstance(context); } - public void saveWallpaper(boolean homeScreen, boolean lightMode) { + public void saveCurrentWallpaper(boolean homeScreen, boolean lightMode) { if (!hasPermission()) return; - saveInputStream(new ParcelFileDescriptor.AutoCloseInputStream(wallpaperManager.getWallpaperFile(homeScreen ? WallpaperManager.FLAG_SYSTEM : WallpaperManager.FLAG_LOCK)), new File(getWallpaperPath(homeScreen, lightMode))); + ParcelFileDescriptor fileDescriptor = wallpaperManager.getWallpaperFile(homeScreen ? WallpaperManager.FLAG_SYSTEM : WallpaperManager.FLAG_LOCK); + if (fileDescriptor == null) { + Toast.makeText(context, R.string.wallpaper_not_supported, Toast.LENGTH_SHORT).show(); + return; + } + saveInputStream(new ParcelFileDescriptor.AutoCloseInputStream(fileDescriptor), new File(getWallpaperPath(homeScreen, lightMode))); + } + + public void saveUriWallpaper(Uri wallpaperUri, boolean homeScreen, boolean lightMode) throws FileNotFoundException { + saveInputStream(context.getContentResolver().openInputStream(wallpaperUri), new File(getWallpaperPath(homeScreen, lightMode))); + if (PreferenceManager.getDefaultSharedPreferences(context).getString("main_pref", "off").equals("wps") && (context.getResources().getConfiguration().uiMode != 33) == lightMode) { + loadWallpaper(homeScreen, lightMode); + } } public void loadWallpaper(boolean homeScreen, boolean lightMode) { @@ -58,7 +73,7 @@ private InputStream loadInputStream(File file) { } } - private void saveInputStream(InputStream in, File file) { + public void saveInputStream(InputStream in, File file) { OutputStream out = null; try { out = new FileOutputStream(file); diff --git a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/AboutActivity.java b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/AboutActivity.java new file mode 100644 index 0000000..633a103 --- /dev/null +++ b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/AboutActivity.java @@ -0,0 +1,76 @@ +package de.dlyt.yanndroid.dualwallpaper.ui; + +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.AppCompatButton; + +import de.dlyt.yanndroid.dualwallpaper.R; +import de.dlyt.yanndroid.dualwallpaper.utils.Updater; +import dev.oneuiproject.oneui.layout.AppInfoLayout; + +public class AboutActivity extends AppCompatActivity implements AppInfoLayout.OnClickListener, Updater.UpdateChecker { + + private AppInfoLayout appInfoLayout; + private String update_url, update_version; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_about); + + appInfoLayout = findViewById(R.id.appInfoLayout); + appInfoLayout.setMainButtonClickListener(this); + Updater.checkForUpdate(this, this); + } + + @Override + public void updateAvailable(boolean available, String url, String versionName) { + appInfoLayout.setStatus(available ? AppInfoLayout.UPDATE_AVAILABLE : AppInfoLayout.NO_UPDATE); + update_url = url; + update_version = versionName; + } + + @Override + public void githubAvailable(String url) { + AppCompatButton about_github = findViewById(R.id.about_github); + about_github.setVisibility(View.VISIBLE); + about_github.setOnClickListener(v -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)))); + } + + @Override + public void noConnection() { + appInfoLayout.setStatus(AppInfoLayout.NO_CONNECTION); + } + + @Override + public void onUpdateClicked(View v) { + if (update_url != null) Updater.downloadAndInstall(this, update_url, update_version); + } + + @Override + public void onRetryClicked(View v) { + Updater.checkForUpdate(this, this); + } + + @Override + public boolean onCreateOptionsMenu(@NonNull Menu menu) { + getMenuInflater().inflate(dev.oneuiproject.oneui.R.menu.app_info_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == dev.oneuiproject.oneui.R.id.menu_app_info) { + appInfoLayout.openSettingsAppInfo(); + return true; + } + return false; + } +} diff --git a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/MainActivity.java b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/MainActivity.java index 65a46df..9345c0f 100644 --- a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/MainActivity.java +++ b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/MainActivity.java @@ -5,29 +5,49 @@ import android.app.NotificationManager; import android.app.WallpaperManager; import android.content.ComponentName; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.content.res.ColorStateList; +import android.graphics.Color; +import android.os.Build; import android.os.Bundle; +import android.provider.Settings; +import android.util.TypedValue; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.SwitchCompat; import androidx.core.app.ActivityCompat; import androidx.core.app.NotificationManagerCompat; +import androidx.fragment.app.FragmentManager; +import androidx.preference.DropDownPreference; +import androidx.preference.Preference; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.PreferenceManager; import androidx.viewpager2.widget.ViewPager2; import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayoutMediator; +import java.io.FileNotFoundException; + import de.dlyt.yanndroid.dualwallpaper.LiveWallpaper; import de.dlyt.yanndroid.dualwallpaper.R; import de.dlyt.yanndroid.dualwallpaper.WallpaperService; import de.dlyt.yanndroid.dualwallpaper.WallpaperUtil; import dev.oneuiproject.oneui.layout.ToolbarLayout; +import dev.oneuiproject.oneui.preference.internal.PreferenceRelatedCard; +import dev.oneuiproject.oneui.utils.PreferenceUtils; public class MainActivity extends AppCompatActivity { - private WallpaperUtil wallpaperUtil; + private static WallpaperUtil wallpaperUtil; + private ViewPagerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,31 +61,164 @@ protected void onCreate(Bundle savedInstanceState) { NotificationManagerCompat.from(this).createNotificationChannel(new NotificationChannel("4000", getString(R.string.wallpaper_service), NotificationManager.IMPORTANCE_LOW)); ToolbarLayout toolbarLayout = findViewById(R.id.toolbarLayout); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - toolbarLayout.setNavigationButtonOnClickListener(v -> onBackPressed()); + toolbarLayout.setNavigationButtonAsBack(); ViewPager2 viewPager = findViewById(R.id.viewPager); - viewPager.setAdapter(new ViewPagerAdapter(this, wallpaperUtil)); + viewPager.setAdapter(adapter = new ViewPagerAdapter(this, wallpaperUtil)); TabLayout tabLayout = findViewById(R.id.tabLayout); tabLayout.seslSetSubTabStyle(); new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> tab.setText(((ViewPagerAdapter) viewPager.getAdapter()).getTitle(position))).attach(); + if (PreferenceManager.getDefaultSharedPreferences(this).getString("main_pref", "off").equals("wps")) + startForegroundService(new Intent(MainActivity.this, WallpaperService.class)); + + FragmentManager fragmentManager = getSupportFragmentManager(); + fragmentManager.beginTransaction().add(R.id.main_preferences, new PreferencesFragment()).commit(); + fragmentManager.executePendingTransactions(); - SwitchCompat use_service_switch = findViewById(R.id.use_service_switch); - use_service_switch.setChecked(getSharedPreferences("sp", MODE_PRIVATE).getBoolean("use_service_switch", false)); - use_service_switch.setOnCheckedChangeListener((buttonView, isChecked) -> { - getSharedPreferences("sp", MODE_PRIVATE).edit().putBoolean("use_service_switch", isChecked).apply(); - if (isChecked) { - startForegroundService(new Intent(MainActivity.this, WallpaperService.class)); - } else { - stopService(new Intent(MainActivity.this, WallpaperService.class)); + if (getIntent().getAction().equals(Intent.ACTION_ATTACH_DATA)) + setWallpaperIntent(getIntent()); + } + + private void setWallpaperIntent(Intent intent) { + CharSequence[] items = new CharSequence[4]; + items[0] = getString(R.string.light) + " " + getString(R.string.lock_screen); + items[1] = getString(R.string.light) + " " + getString(R.string.home_screen); + items[2] = getString(R.string.dark) + " " + getString(R.string.lock_screen); + items[3] = getString(R.string.dark) + " " + getString(R.string.home_screen); + AlertDialog dialog = new AlertDialog.Builder(this) + .setTitle(R.string.set_wallpaper_as) + .setItems(items, (dialog1, which) -> { + try { + wallpaperUtil.saveUriWallpaper(intent.getData(), (which & 1) == 1, ((which >> 1) & 1) == 0); + adapter.notifyItemChanged(((which >> 1) & 1)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + }) + .create(); + dialog.show(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == RESULT_OK && (requestCode >> 3) == 625) { + try { + wallpaperUtil.saveUriWallpaper(data.getData(), (((requestCode >> 1) & 1) == 1), ((requestCode & 1) == 1)); + adapter.notifyItemChanged(1 - (requestCode & 1)); + } catch (FileNotFoundException e) { + e.printStackTrace(); } - }); + } } - public void setLW(View view) { - Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER); - intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(this, LiveWallpaper.class)); - startActivity(intent); + @Override + public boolean onCreateOptionsMenu(@NonNull Menu menu) { + getMenuInflater().inflate(dev.oneuiproject.oneui.R.menu.app_info_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == dev.oneuiproject.oneui.R.id.menu_app_info) { + startActivity(new Intent(this, AboutActivity.class)); + return true; + } + return false; + } + + public static class PreferencesFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceChangeListener { + private Context mContext; + private PreferenceRelatedCard mRelativeLinkCard; + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + mContext = context; + } + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.preferences, rootKey); + } + + @Override + public void onCreate(Bundle bundle) { + super.onCreate(bundle); + DropDownPreference main_pref = findPreference("main_pref"); + main_pref.seslSetSummaryColor(getColoredSummaryColor()); + main_pref.setOnPreferenceChangeListener(this); + } + + @Override + public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + getView().setBackgroundColor(mContext.getColor(dev.oneuiproject.oneui.R.color.oui_background_color)); + getListView().seslSetLastRoundedCorner(false); + } + + @Override + public void onResume() { + setRelativeLinkCard(); + super.onResume(); + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if (preference.getKey().equals("main_pref")) { + switch ((String) newValue) { + case "wps": + mContext.startForegroundService(new Intent(mContext, WallpaperService.class)); + break; + case "lwp": + mContext.stopService(new Intent(mContext, WallpaperService.class)); + Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER); + intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(mContext, LiveWallpaper.class)); + startActivity(intent); + break; + case "off": + mContext.stopService(new Intent(mContext, WallpaperService.class)); + boolean lightMode = getResources().getConfiguration().uiMode != 33; + wallpaperUtil.loadWallpaper(true, lightMode); + wallpaperUtil.loadWallpaper(false, lightMode); + break; + } + return true; + } + return false; + } + + private ColorStateList getColoredSummaryColor() { + TypedValue colorPrimaryDark = new TypedValue(); + mContext.getTheme().resolveAttribute(dev.oneuiproject.oneui.R.attr.colorPrimaryDark, colorPrimaryDark, true); + int[][] states = new int[][]{ + new int[]{android.R.attr.state_enabled}, + new int[]{-android.R.attr.state_enabled} + }; + int[] colors = new int[]{ + Color.argb(0xff, + Color.red(colorPrimaryDark.data), + Color.green(colorPrimaryDark.data), + Color.blue(colorPrimaryDark.data)), + Color.argb(0x4d, + Color.red(colorPrimaryDark.data), + Color.green(colorPrimaryDark.data), + Color.blue(colorPrimaryDark.data)) + }; + return new ColorStateList(states, colors); + } + + private void setRelativeLinkCard() { + if (mRelativeLinkCard == null) { + mRelativeLinkCard = PreferenceUtils.createRelatedCard(mContext); + mRelativeLinkCard.addButton(mContext.getString(R.string.service_notification), v -> startActivity(new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).putExtra(Settings.EXTRA_APP_PACKAGE, mContext.getPackageName()).putExtra(Settings.EXTRA_CHANNEL_ID, "4000"))); + mRelativeLinkCard.addButton(mContext.getString(R.string.live_wallpaper), v -> startActivity(new Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER))); + if (Build.MANUFACTURER.equals("samsung")) { + mRelativeLinkCard.addButton(mContext.getString(R.string.wallpaper_and_style), v -> startActivity(new Intent("com.samsung.intent.action.WALLPAPER_SETTING"))); + } + mRelativeLinkCard.show(this); + } + } } } \ No newline at end of file diff --git a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/ViewPagerAdapter.java b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/ViewPagerAdapter.java index f8f631f..370fd9d 100644 --- a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/ViewPagerAdapter.java +++ b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/ui/ViewPagerAdapter.java @@ -1,6 +1,7 @@ package de.dlyt.yanndroid.dualwallpaper.ui; import android.content.Context; +import android.content.Intent; import android.graphics.BitmapFactory; import android.graphics.Point; import android.view.LayoutInflater; @@ -73,17 +74,6 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.viewpager_page_layout, parent, false)); } - public class ViewHolder extends RecyclerView.ViewHolder { - ImageView lock_screen_preview; - ImageView home_screen_preview; - - public ViewHolder(@NonNull View itemView) { - super(itemView); - lock_screen_preview = itemView.findViewById(R.id.lock_screen_preview); - home_screen_preview = itemView.findViewById(R.id.home_screen_preview); - } - } - private void wallpaperOptionsDialog(ImageView imageView, boolean homeScreen, boolean lightMode) { File wallpaperFile = new File(wallpaperUtil.getWallpaperPath(homeScreen, lightMode)); CharSequence[] dialogOptions = wallpaperFile.exists() ? @@ -94,11 +84,13 @@ private void wallpaperOptionsDialog(ImageView imageView, boolean homeScreen, boo .setItems(dialogOptions, (dialog, which) -> { switch (which) { case 0: - wallpaperUtil.saveWallpaper(homeScreen, lightMode); + wallpaperUtil.saveCurrentWallpaper(homeScreen, lightMode); updateImages(imageView, homeScreen, lightMode); break; case 1: - //todo + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType("image/*"); + ((MainActivity) context).startActivityForResult(intent, 5000 + (homeScreen ? 1 << 1 : 0) + (lightMode ? 1 : 0)); break; case 2: wallpaperFile.delete(); @@ -108,4 +100,15 @@ private void wallpaperOptionsDialog(ImageView imageView, boolean homeScreen, boo }).create(); alertDialog.show(); } + + public class ViewHolder extends RecyclerView.ViewHolder { + ImageView lock_screen_preview; + ImageView home_screen_preview; + + public ViewHolder(@NonNull View itemView) { + super(itemView); + lock_screen_preview = itemView.findViewById(R.id.lock_screen_preview); + home_screen_preview = itemView.findViewById(R.id.home_screen_preview); + } + } } diff --git a/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/utils/Updater.java b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/utils/Updater.java new file mode 100644 index 0000000..4283b03 --- /dev/null +++ b/app/src/main/java/de/dlyt/yanndroid/dualwallpaper/utils/Updater.java @@ -0,0 +1,114 @@ +package de.dlyt.yanndroid.dualwallpaper.utils; + +import android.Manifest; +import android.app.DownloadManager; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.pm.PackageManager; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.net.Uri; +import android.os.Environment; +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.annotation.RequiresPermission; +import androidx.core.content.FileProvider; + +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; +import com.google.firebase.database.DatabaseReference; +import com.google.firebase.database.FirebaseDatabase; +import com.google.firebase.database.ValueEventListener; + +import java.io.File; +import java.util.HashMap; + +import de.dlyt.yanndroid.dualwallpaper.R; + + +public class Updater { + + @RequiresPermission(allOf = {Manifest.permission.INTERNET, Manifest.permission.REQUEST_INSTALL_PACKAGES}) + public static void downloadAndInstall(Context context, String url, String versionName) { + String destination = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + context.getString(R.string.app_name) + "_" + versionName + ".apk"; + Uri fileUri = Uri.parse("file://" + destination); + + File file = new File(destination); + if (file.exists()) file.delete(); + + DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); + DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); + + request.setMimeType("application/vnd.android.package-archive"); + request.setTitle(context.getString(R.string.app_name)); + request.setDescription(versionName); + request.setDestinationUri(fileUri); + + BroadcastReceiver onComplete = new BroadcastReceiver() { + public void onReceive(Context ctxt, Intent intent) { + + Uri apkFileUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", new File(destination)); + Intent install = new Intent(Intent.ACTION_VIEW); + install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + install.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true); + install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + install.setDataAndType(apkFileUri, "application/vnd.android.package-archive"); + context.startActivity(install); + + context.unregisterReceiver(this); + } + }; + context.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); + + downloadManager.enqueue(request); + } + + @RequiresPermission(allOf = {Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE}) + public static void checkForUpdate(Context context, UpdateChecker updateChecker) { + NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); + if (!(networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected())) { + updateChecker.noConnection(); + return; + } + + DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child(context.getString(R.string.app_name)); + mDatabase.addValueEventListener(new ValueEventListener() { + @Override + public void onDataChange(@NonNull DataSnapshot snapshot) { + try { + HashMap hashMap = new HashMap<>(); + for (DataSnapshot child : snapshot.getChildren()) { + hashMap.put(child.getKey(), child.getValue().toString()); + } + + updateChecker.updateAvailable(Integer.parseInt(hashMap.get("versionCode")) > context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode, hashMap.get("apk"), hashMap.get("versionName")); + + if (hashMap.get("github") != null) { + updateChecker.githubAvailable(hashMap.get("github")); + } + } catch (PackageManager.NameNotFoundException e) { + Log.e("Updater.checkForUpdate", e.getMessage()); + updateChecker.updateAvailable(false, null, null); + } + } + + @Override + public void onCancelled(@NonNull DatabaseError error) { + Log.e("Updater.checkForUpdate", error.getMessage()); + updateChecker.updateAvailable(false, null, null); + } + }); + } + + public interface UpdateChecker { + void updateAvailable(boolean available, String url, String versionName); + + void githubAvailable(String url); + + void noConnection(); + } +} diff --git a/app/src/main/res/layout/activity_about.xml b/app/src/main/res/layout/activity_about.xml new file mode 100644 index 0000000..7e14489 --- /dev/null +++ b/app/src/main/res/layout/activity_about.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b458df8..ffeffed 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -26,38 +26,11 @@ android:layout_width="match_parent" android:layout_height="wrap_content" /> - - - - - - - -