2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2016-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
2017-04-26 00:14:25 +02:00
|
|
|
Gobbetti
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2016-06-14 15:56:38 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities.appmanager;
|
|
|
|
|
2017-01-26 17:18:43 +01:00
|
|
|
import android.app.Activity;
|
2016-06-20 17:42:32 +02:00
|
|
|
import android.content.Intent;
|
2016-06-14 15:56:38 +02:00
|
|
|
import android.os.Bundle;
|
2016-06-15 19:56:34 +02:00
|
|
|
import android.view.MenuItem;
|
2017-01-26 17:18:43 +01:00
|
|
|
import android.view.View;
|
2016-06-14 15:56:38 +02:00
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|
|
|
|
2016-06-17 22:43:06 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import androidx.core.app.NavUtils;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.fragment.app.FragmentManager;
|
|
|
|
import androidx.viewpager.widget.ViewPager;
|
2016-06-14 15:56:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractFragmentPagerAdapter;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBFragmentActivity;
|
2017-01-26 17:18:43 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.FwAppInstallerActivity;
|
2016-06-14 15:56:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2016-06-17 22:43:06 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
2016-06-14 15:56:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
public class AppManagerActivity extends AbstractGBFragmentActivity {
|
2016-06-17 22:43:06 +02:00
|
|
|
|
2021-06-20 14:17:18 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AppManagerActivity.class);
|
2017-01-26 17:18:43 +01:00
|
|
|
private int READ_REQUEST_CODE = 42;
|
2016-06-17 22:43:06 +02:00
|
|
|
|
2016-06-14 15:56:38 +02:00
|
|
|
private GBDevice mGBDevice = null;
|
|
|
|
|
2016-06-14 20:04:54 +02:00
|
|
|
public GBDevice getGBDevice() {
|
|
|
|
return mGBDevice;
|
|
|
|
}
|
|
|
|
|
2016-06-14 15:56:38 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_fragmentappmanager);
|
|
|
|
|
|
|
|
Bundle extras = getIntent().getExtras();
|
|
|
|
if (extras != null) {
|
|
|
|
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
|
2017-12-30 23:06:53 +01:00
|
|
|
}
|
|
|
|
if (mGBDevice == null) {
|
2016-06-14 15:56:38 +02:00
|
|
|
throw new IllegalArgumentException("Must provide a device when invoking this activity");
|
|
|
|
}
|
|
|
|
|
2017-01-26 17:18:43 +01:00
|
|
|
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
|
|
|
|
assert fab != null;
|
|
|
|
fab.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
|
intent.setType("*/*");
|
|
|
|
startActivityForResult(intent, READ_REQUEST_CODE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-14 15:56:38 +02:00
|
|
|
// Set up the ViewPager with the sections adapter.
|
|
|
|
ViewPager viewPager = (ViewPager) findViewById(R.id.appmanager_pager);
|
2016-06-20 17:42:32 +02:00
|
|
|
if (viewPager != null) {
|
|
|
|
viewPager.setAdapter(getPagerAdapter());
|
|
|
|
}
|
2016-06-14 15:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected AbstractFragmentPagerAdapter createFragmentPagerAdapter(FragmentManager fragmentManager) {
|
|
|
|
return new SectionsPagerAdapter(fragmentManager);
|
|
|
|
}
|
|
|
|
|
2016-06-17 22:43:06 +02:00
|
|
|
public static synchronized void deleteFromAppOrderFile(String filename, UUID uuid) {
|
|
|
|
ArrayList<UUID> uuids = getUuidsFromFile(filename);
|
|
|
|
uuids.remove(uuid);
|
|
|
|
rewriteAppOrderFile(filename, uuids);
|
|
|
|
}
|
|
|
|
|
2016-06-14 15:56:38 +02:00
|
|
|
public class SectionsPagerAdapter extends AbstractFragmentPagerAdapter {
|
|
|
|
|
2017-01-26 17:18:43 +01:00
|
|
|
SectionsPagerAdapter(FragmentManager fm) {
|
2016-06-14 15:56:38 +02:00
|
|
|
super(fm);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Fragment getItem(int position) {
|
|
|
|
// getItem is called to instantiate the fragment for the given page.
|
|
|
|
switch (position) {
|
|
|
|
case 0:
|
2016-06-14 20:04:54 +02:00
|
|
|
return new AppManagerFragmentCache();
|
2016-06-14 15:56:38 +02:00
|
|
|
case 1:
|
2016-06-14 20:04:54 +02:00
|
|
|
return new AppManagerFragmentInstalledApps();
|
2016-06-14 15:56:38 +02:00
|
|
|
case 2:
|
2016-06-14 20:04:54 +02:00
|
|
|
return new AppManagerFragmentInstalledWatchfaces();
|
2016-06-14 15:56:38 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CharSequence getPageTitle(int position) {
|
|
|
|
switch (position) {
|
|
|
|
case 0:
|
2016-06-25 11:27:56 +02:00
|
|
|
return getString(R.string.appmanager_cached_watchapps_watchfaces);
|
2016-06-14 15:56:38 +02:00
|
|
|
case 1:
|
2016-06-25 11:27:56 +02:00
|
|
|
return getString(R.string.appmanager_installed_watchapps);
|
2016-06-14 15:56:38 +02:00
|
|
|
case 2:
|
2016-06-25 11:27:56 +02:00
|
|
|
return getString(R.string.appmanager_installed_watchfaces);
|
2016-06-14 15:56:38 +02:00
|
|
|
case 3:
|
|
|
|
}
|
|
|
|
return super.getPageTitle(position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 19:56:34 +02:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2016-06-17 22:43:06 +02:00
|
|
|
|
|
|
|
static synchronized void rewriteAppOrderFile(String filename, List<UUID> uuids) {
|
2017-04-12 20:48:28 +02:00
|
|
|
try (BufferedWriter out = new BufferedWriter(new FileWriter(FileUtils.getExternalFilesDir() + "/" + filename))) {
|
2016-06-17 22:43:06 +02:00
|
|
|
for (UUID uuid : uuids) {
|
|
|
|
out.write(uuid.toString());
|
|
|
|
out.newLine();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("can't write app order to file!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized public static void addToAppOrderFile(String filename, UUID uuid) {
|
|
|
|
ArrayList<UUID> uuids = getUuidsFromFile(filename);
|
2016-06-19 21:13:47 +02:00
|
|
|
if (!uuids.contains(uuid)) {
|
|
|
|
uuids.add(uuid);
|
|
|
|
rewriteAppOrderFile(filename, uuids);
|
|
|
|
}
|
2016-06-17 22:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static synchronized ArrayList<UUID> getUuidsFromFile(String filename) {
|
|
|
|
ArrayList<UUID> uuids = new ArrayList<>();
|
2017-04-12 20:48:28 +02:00
|
|
|
try (BufferedReader in = new BufferedReader(new FileReader(FileUtils.getExternalFilesDir() + "/" + filename))) {
|
2016-06-17 22:43:06 +02:00
|
|
|
String line;
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
uuids.add(UUID.fromString(line));
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("could not read sort file");
|
|
|
|
}
|
|
|
|
return uuids;
|
|
|
|
}
|
2016-06-20 17:42:32 +02:00
|
|
|
|
2017-01-26 17:18:43 +01:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
|
2022-07-25 14:25:27 +02:00
|
|
|
super.onActivityResult(requestCode, resultCode, resultData);
|
2017-01-26 17:18:43 +01:00
|
|
|
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
|
|
|
|
Intent startIntent = new Intent(AppManagerActivity.this, FwAppInstallerActivity.class);
|
|
|
|
startIntent.setAction(Intent.ACTION_VIEW);
|
|
|
|
startIntent.setDataAndType(resultData.getData(), null);
|
|
|
|
startActivity(startIntent);
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 15:56:38 +02:00
|
|
|
}
|