Skip to content
Snippets Groups Projects
Commit 8ffbbe55 authored by Aline Bonnet's avatar Aline Bonnet
Browse files

ringtones: fix behaviour

- add default.wav
- fix the initial state of ringtone preference (new Enabled by default)
- extract file handling methods in a dedicated class

Change-Id: I17f2db5e9e3dafcb99d37e4030ad3e735957ef42
Tuleap: #1117
parent e12b9707
Branches
Tags
No related merge requests found
File added
This diff is collapsed.
File deleted
File deleted
...@@ -32,7 +32,6 @@ import android.content.IntentFilter; ...@@ -32,7 +32,6 @@ import android.content.IntentFilter;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
...@@ -58,10 +57,6 @@ import android.widget.LinearLayout; ...@@ -58,10 +57,6 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List; import java.util.List;
import butterknife.BindView; import butterknife.BindView;
...@@ -78,6 +73,7 @@ import cx.ring.model.account.Account; ...@@ -78,6 +73,7 @@ import cx.ring.model.account.Account;
import cx.ring.model.account.ConfigKey; import cx.ring.model.account.ConfigKey;
import cx.ring.service.IDRingService; import cx.ring.service.IDRingService;
import cx.ring.service.LocalService; import cx.ring.service.LocalService;
import cx.ring.utils.FileUtils;
import cx.ring.views.MenuHeaderView; import cx.ring.views.MenuHeaderView;
public class HomeActivity extends AppCompatActivity implements LocalService.Callbacks, public class HomeActivity extends AppCompatActivity implements LocalService.Callbacks,
...@@ -290,10 +286,13 @@ public class HomeActivity extends AppCompatActivity implements LocalService.Call ...@@ -290,10 +286,13 @@ public class HomeActivity extends AppCompatActivity implements LocalService.Call
@Override @Override
protected void onStart() { protected void onStart() {
Log.d(TAG, "onStart"); Log.d(TAG, "onStart");
if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("installed", false)) {
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("installed", true).commit(); String path = getFilesDir().getAbsolutePath() + "/ringtones";
copyAssetFolder(getAssets(), "ringtones", getFilesDir().getAbsolutePath() + "/ringtones"); if (!(new File(path + "/default.wav")).exists()) {
Log.d(TAG, "default.wav doesn't exist. Copying ringtones.");
FileUtils.copyAssetFolder(getAssets(), "ringtones", path);
} }
super.onStart(); super.onStart();
} }
...@@ -403,53 +402,6 @@ public class HomeActivity extends AppCompatActivity implements LocalService.Call ...@@ -403,53 +402,6 @@ public class HomeActivity extends AppCompatActivity implements LocalService.Call
return actionButton; return actionButton;
} }
private static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
try {
String[] files = assetManager.list(fromAssetPath);
new File(toPath).mkdirs();
Log.d(TAG, "Creating :" + toPath);
boolean res = true;
for (String file : files)
if (file.contains("")) {
Log.d(TAG, "Copying file :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
} else {
Log.d(TAG, "Copying folder :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
}
return res;
} catch (Exception e) {
Log.e(TAG, "Error while copying asset folder", e);
return false;
}
}
private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
InputStream in;
OutputStream out;
try {
in = assetManager.open(fromAssetPath);
new File(toPath).createNewFile();
out = new FileOutputStream(toPath);
copyFile(in, out);
in.close();
out.flush();
out.close();
return true;
} catch (Exception e) {
Log.e(TAG, "Error while copying asset", e);
return false;
}
}
private static void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
/* activity gets back to the foreground and user input */ /* activity gets back to the foreground and user input */
@Override @Override
protected void onResume() { protected void onResume() {
......
...@@ -523,7 +523,7 @@ public class AccountCreationFragment extends Fragment { ...@@ -523,7 +523,7 @@ public class AccountCreationFragment extends Fragment {
showImportDialog(); showImportDialog();
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Exception reading file", e); Log.e(TAG, "Exception reading file", e);
Toast.makeText(getActivity(), getContext().getString(R.string.account_cannot_read, data.getData()), Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), getActivity().getString(R.string.account_cannot_read, data.getData()), Toast.LENGTH_LONG).show();
} }
} else { } else {
showImportDialog(); showImportDialog();
......
...@@ -50,6 +50,7 @@ import cx.ring.model.account.Account; ...@@ -50,6 +50,7 @@ import cx.ring.model.account.Account;
import cx.ring.model.account.AccountConfig; import cx.ring.model.account.AccountConfig;
import cx.ring.model.account.ConfigKey; import cx.ring.model.account.ConfigKey;
import cx.ring.service.LocalService; import cx.ring.service.LocalService;
import cx.ring.utils.FileUtils;
import static cx.ring.client.AccountEditionActivity.DUMMY_CALLBACKS; import static cx.ring.client.AccountEditionActivity.DUMMY_CALLBACKS;
...@@ -125,8 +126,9 @@ public class MediaPreferenceFragment extends PreferenceFragment ...@@ -125,8 +126,9 @@ public class MediaPreferenceFragment extends PreferenceFragment
return; return;
} }
File myFile = new File(data.getData().getPath()); String path = FileUtils.getRealPathFromURI(getActivity(), data.getData());
Log.i(TAG, "file selected:" + data.getData()); File myFile = new File(path);
Log.i(TAG, "file selected:" + myFile.getAbsolutePath());
if (requestCode == SELECT_RINGTONE_PATH) { if (requestCode == SELECT_RINGTONE_PATH) {
findPreference(ConfigKey.RINGTONE_PATH.key()).setSummary(myFile.getName()); findPreference(ConfigKey.RINGTONE_PATH.key()).setSummary(myFile.getName());
mCallbacks.getAccount().setDetail(ConfigKey.RINGTONE_PATH, myFile.getAbsolutePath()); mCallbacks.getAccount().setDetail(ConfigKey.RINGTONE_PATH, myFile.getAbsolutePath());
...@@ -146,9 +148,9 @@ public class MediaPreferenceFragment extends PreferenceFragment ...@@ -146,9 +148,9 @@ public class MediaPreferenceFragment extends PreferenceFragment
addPreferencesFromResource(R.xml.account_media_prefs); addPreferencesFromResource(R.xml.account_media_prefs);
audioCodecsPref = (CodecPreference) findPreference("Account.audioCodecs"); audioCodecsPref = (CodecPreference) findPreference("Account.audioCodecs");
videoCodecsPref = (CodecPreference) findPreference("Account.videoCodecs"); videoCodecsPref = (CodecPreference) findPreference("Account.videoCodecs");
boolean isChecked = Boolean.valueOf(mCallbacks.getAccount().getDetail(ConfigKey.RINGTONE_ENABLED));
findPreference(ConfigKey.RINGTONE_PATH.key()).setEnabled(isChecked);
findPreference(ConfigKey.RINGTONE_PATH.key()).setEnabled(
((TwoStatePreference) findPreference(ConfigKey.RINGTONE_ENABLED.key())).isChecked());
addPreferenceListener(ConfigKey.VIDEO_ENABLED, changeVideoPreferenceListener); addPreferenceListener(ConfigKey.VIDEO_ENABLED, changeVideoPreferenceListener);
final Account acc = mCallbacks.getAccount(); final Account acc = mCallbacks.getAccount();
if (acc != null) { if (acc != null) {
......
/*
* Copyright (C) 2004-2016 Savoir-faire Linux Inc.
*
* Author: Aline Bonnet <aline.bonnet@savoirfairelinux.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package cx.ring.utils;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileUtils {
static final String TAG = FileUtils.class.getSimpleName();
public static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
try {
String[] files = assetManager.list(fromAssetPath);
File fileTmp = new File(toPath);
if (!fileTmp.exists()) {
fileTmp.mkdirs();
}
Log.d(TAG, "Creating :" + toPath);
boolean res = true;
for (String file : files)
if (file.contains("")) {
Log.d(TAG, "Copying file :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
} else {
Log.d(TAG, "Copying folder :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
}
return res;
} catch (Exception e) {
Log.e(TAG, "Error while copying asset folder", e);
return false;
}
}
private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
InputStream in;
OutputStream out;
try {
in = assetManager.open(fromAssetPath);
new File(toPath).createNewFile();
out = new FileOutputStream(toPath);
copyFile(in, out);
in.close();
out.flush();
out.close();
return true;
} catch (Exception e) {
Log.e(TAG, "Error while copying asset", e);
return false;
}
}
private static void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
public static String getRealPathFromURI(Context context, Uri contentUri) {
String path;
Cursor cursor;
try {
String[] proj = {MediaStore.Audio.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index);
cursor.close();
} catch (Exception e) {
Log.e(TAG, "Error while saving file to disk", e);
path = null;
}
return path;
}
}
...@@ -31,7 +31,6 @@ along with this program; if not, write to the Free Software ...@@ -31,7 +31,6 @@ along with this program; if not, write to the Free Software
<android.support.v7.preference.Preference <android.support.v7.preference.Preference
android:id="@+id/audio_ringtone_path" android:id="@+id/audio_ringtone_path"
android:enabled="false"
android:key="Account.ringtonePath" android:key="Account.ringtonePath"
android:persistent="false" android:persistent="false"
android:title="@string/account_ringtone_path_label" /> android:title="@string/account_ringtone_path_label" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment