Skip to content

Commit f4d042e

Browse files
Implementing sms / contacts permissions fixes.
1 parent 348094a commit f4d042e

File tree

1 file changed

+38
-72
lines changed

1 file changed

+38
-72
lines changed

components/permissions/TelephonySmsPermissionDemo.tsx

Lines changed: 38 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ const SMS_PERMISSIONS = [
2525
];
2626

2727
// Contacts permissions
28-
const CONTACTS_PERMISSIONS = [
29-
PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
30-
PermissionsAndroid.PERMISSIONS.WRITE_CONTACTS,
31-
];
28+
const CONTACTS_PERMISSIONS = [PermissionsAndroid.PERMISSIONS.READ_CONTACTS, PermissionsAndroid.PERMISSIONS.WRITE_CONTACTS];
3229

3330
interface CellularInfoData {
3431
allowsVoip: boolean | null;
@@ -77,7 +74,7 @@ export default function TelephonySmsPermissionDemo() {
7774
const [smsMessages, setSmsMessages] = useState<SmsEntry[]>([]);
7875
const [phoneNumber, setPhoneNumber] = useState('');
7976
const [smsBody, setSmsBody] = useState('Test message from CodeBuilder');
80-
77+
8178
// Loading states
8279
const [loadingPermissions, setLoadingPermissions] = useState(false);
8380
const [loadingCellular, setLoadingCellular] = useState(false);
@@ -102,12 +99,12 @@ export default function TelephonySmsPermissionDemo() {
10299
try {
103100
const allPermissions = [...TELEPHONY_PERMISSIONS, ...SMS_PERMISSIONS, ...CONTACTS_PERMISSIONS];
104101
const results: Record<string, string> = {};
105-
102+
106103
for (const permission of allPermissions) {
107104
const granted = await PermissionsAndroid.check(permission);
108105
results[permission] = granted ? 'granted' : 'denied';
109106
}
110-
107+
111108
setPermissionStatuses(results);
112109
} catch (error) {
113110
console.error('Error checking permissions:', error);
@@ -143,12 +140,12 @@ export default function TelephonySmsPermissionDemo() {
143140
try {
144141
// Request cellular permissions first
145142
await Cellular.requestPermissionsAsync();
146-
143+
147144
// Request all telephony, SMS, and contacts permissions
148145
const allPermissions = [...TELEPHONY_PERMISSIONS, ...SMS_PERMISSIONS, ...CONTACTS_PERMISSIONS];
149146
const results = await PermissionsAndroid.requestMultiple(allPermissions);
150147
setPermissionStatuses(results);
151-
148+
152149
// Refresh cellular info after permission grant
153150
await loadCellularInfo();
154151
} catch (error) {
@@ -183,9 +180,9 @@ export default function TelephonySmsPermissionDemo() {
183180
}
184181

185182
const { data, hasNextPage, hasPreviousPage } = await Contacts.getContactsAsync(options);
186-
183+
187184
if (loadMore) {
188-
setContacts(prev => [...prev, ...data]);
185+
setContacts((prev) => [...prev, ...data]);
189186
} else {
190187
setContacts(data);
191188
}
@@ -318,21 +315,25 @@ export default function TelephonySmsPermissionDemo() {
318315

319316
const getCellularGenerationName = (gen: Cellular.CellularGeneration) => {
320317
switch (gen) {
321-
case 0: return 'Unknown';
322-
case 1: return '2G';
323-
case 2: return '3G';
324-
case 3: return '4G';
325-
case 4: return '5G';
326-
default: return 'Unknown';
318+
case 0:
319+
return 'Unknown';
320+
case 1:
321+
return '2G';
322+
case 2:
323+
return '3G';
324+
case 3:
325+
return '4G';
326+
case 4:
327+
return '5G';
328+
default:
329+
return 'Unknown';
327330
}
328331
};
329332

330333
const renderContactItem = ({ item }: { item: Contact }) => (
331334
<View style={styles.listItem}>
332335
<Text style={styles.listItemTitle}>{item.name || 'No Name'}</Text>
333-
{item.phoneNumbers && item.phoneNumbers.length > 0 && (
334-
<Text style={styles.listItemSubtitle}>{item.phoneNumbers[0].number}</Text>
335-
)}
336+
{item.phoneNumbers && item.phoneNumbers.length > 0 && <Text style={styles.listItemSubtitle}>{item.phoneNumbers[0].number}</Text>}
336337
</View>
337338
);
338339

@@ -351,7 +352,9 @@ export default function TelephonySmsPermissionDemo() {
351352
<Text style={styles.listItemSubtitle} numberOfLines={2}>
352353
{item.body}
353354
</Text>
354-
<Text style={styles.listItemMeta}>{item.type}{new Date(item.date).toLocaleString()}</Text>
355+
<Text style={styles.listItemMeta}>
356+
{item.type}{new Date(item.date).toLocaleString()}
357+
</Text>
355358
</View>
356359
);
357360

@@ -370,16 +373,12 @@ export default function TelephonySmsPermissionDemo() {
370373
{/* Header */}
371374
<Text style={styles.title}>Telephony, SMS & Contacts</Text>
372375
<Text style={styles.copy}>
373-
This page combines cellular information, telephony permissions, SMS capabilities, contacts access, and call log functionality.
374-
Request all permissions to unlock full functionality.
376+
This page combines cellular information, telephony permissions, SMS capabilities, contacts access, and call log functionality. Request all permissions to unlock
377+
full functionality.
375378
</Text>
376379

377380
{/* Main Permission Request Button */}
378-
<Pressable
379-
style={[styles.mainButton, loadingPermissions && styles.buttonDisabled]}
380-
onPress={requestAllPermissions}
381-
disabled={loadingPermissions}
382-
>
381+
<Pressable style={[styles.mainButton, loadingPermissions && styles.buttonDisabled]} onPress={requestAllPermissions} disabled={loadingPermissions}>
383382
{loadingPermissions ? (
384383
<View style={styles.buttonContent}>
385384
<ActivityIndicator size="small" color="#fff" style={{ marginRight: 8 }} />
@@ -420,11 +419,7 @@ export default function TelephonySmsPermissionDemo() {
420419
<InfoRow label="Mobile Network Code" value={cellularInfo.mobileNetworkCode || 'N/A'} />
421420
<InfoRow label="Permission Status" value={cellularInfo.permission?.status || 'N/A'} />
422421
</View>
423-
<Pressable
424-
style={[styles.actionButton, styles.refreshButton, loadingCellular && styles.buttonDisabled]}
425-
onPress={loadCellularInfo}
426-
disabled={loadingCellular}
427-
>
422+
<Pressable style={[styles.actionButton, styles.refreshButton, loadingCellular && styles.buttonDisabled]} onPress={loadCellularInfo} disabled={loadingCellular}>
428423
{loadingCellular ? (
429424
<View style={styles.buttonContent}>
430425
<ActivityIndicator size="small" color="#fff" style={{ marginRight: 8 }} />
@@ -448,11 +443,7 @@ export default function TelephonySmsPermissionDemo() {
448443
keyboardType="phone-pad"
449444
/>
450445
<View style={styles.buttonRow}>
451-
<Pressable
452-
style={[styles.actionButton, styles.callButton, makingCall && styles.buttonDisabled]}
453-
onPress={makeCall}
454-
disabled={makingCall}
455-
>
446+
<Pressable style={[styles.actionButton, styles.callButton, makingCall && styles.buttonDisabled]} onPress={makeCall} disabled={makingCall}>
456447
{makingCall ? (
457448
<View style={styles.buttonContent}>
458449
<ActivityIndicator size="small" color="#fff" style={{ marginRight: 4 }} />
@@ -462,11 +453,7 @@ export default function TelephonySmsPermissionDemo() {
462453
<Text style={styles.actionButtonText}>📞 Call</Text>
463454
)}
464455
</Pressable>
465-
<Pressable
466-
style={[styles.actionButton, styles.smsButton, sendingSms && styles.buttonDisabled]}
467-
onPress={sendSms}
468-
disabled={sendingSms}
469-
>
456+
<Pressable style={[styles.actionButton, styles.smsButton, sendingSms && styles.buttonDisabled]} onPress={sendSms} disabled={sendingSms}>
470457
{sendingSms ? (
471458
<View style={styles.buttonContent}>
472459
<ActivityIndicator size="small" color="#fff" style={{ marginRight: 4 }} />
@@ -490,7 +477,7 @@ export default function TelephonySmsPermissionDemo() {
490477
{/* Contacts */}
491478
<View style={styles.section}>
492479
<Text style={styles.sectionTitle}>👥 Contacts</Text>
493-
<Pressable
480+
<Pressable
494481
style={[styles.actionButton, styles.contactsButton, loadingContacts && styles.buttonDisabled]}
495482
onPress={() => loadContacts(false)}
496483
disabled={loadingContacts}
@@ -509,7 +496,7 @@ export default function TelephonySmsPermissionDemo() {
509496
<FlatList
510497
data={contacts}
511498
renderItem={renderContactItem}
512-
keyExtractor={(item, index) => item.id ?? `contact-${index}`}
499+
keyExtractor={(item, index) => `contact-${index}`}
513500
style={styles.flatList}
514501
onEndReached={() => loadContacts(true)}
515502
onEndReachedThreshold={0.5}
@@ -522,11 +509,7 @@ export default function TelephonySmsPermissionDemo() {
522509
{/* Call Logs */}
523510
<View style={styles.section}>
524511
<Text style={styles.sectionTitle}>📋 Call Logs</Text>
525-
<Pressable
526-
style={[styles.actionButton, styles.callLogsButton, loadingCallLogs && styles.buttonDisabled]}
527-
onPress={loadCallLogs}
528-
disabled={loadingCallLogs}
529-
>
512+
<Pressable style={[styles.actionButton, styles.callLogsButton, loadingCallLogs && styles.buttonDisabled]} onPress={loadCallLogs} disabled={loadingCallLogs}>
530513
{loadingCallLogs ? (
531514
<View style={styles.buttonContent}>
532515
<ActivityIndicator size="small" color="#fff" style={{ marginRight: 8 }} />
@@ -538,24 +521,15 @@ export default function TelephonySmsPermissionDemo() {
538521
</Pressable>
539522
{callLogs.length > 0 && (
540523
<View style={styles.flatListContainer}>
541-
<FlatList
542-
data={callLogs}
543-
renderItem={renderCallLogItem}
544-
keyExtractor={(item) => item.id}
545-
style={styles.flatList}
546-
/>
524+
<FlatList data={callLogs} renderItem={renderCallLogItem} keyExtractor={(item) => item.id} style={styles.flatList} />
547525
</View>
548526
)}
549527
</View>
550528

551529
{/* SMS Messages */}
552530
<View style={styles.section}>
553531
<Text style={styles.sectionTitle}>💬 SMS Messages</Text>
554-
<Pressable
555-
style={[styles.actionButton, styles.smsMessagesButton, loadingSms && styles.buttonDisabled]}
556-
onPress={loadSmsMessages}
557-
disabled={loadingSms}
558-
>
532+
<Pressable style={[styles.actionButton, styles.smsMessagesButton, loadingSms && styles.buttonDisabled]} onPress={loadSmsMessages} disabled={loadingSms}>
559533
{loadingSms ? (
560534
<View style={styles.buttonContent}>
561535
<ActivityIndicator size="small" color="#fff" style={{ marginRight: 8 }} />
@@ -567,12 +541,7 @@ export default function TelephonySmsPermissionDemo() {
567541
</Pressable>
568542
{smsMessages.length > 0 && (
569543
<View style={styles.flatListContainer}>
570-
<FlatList
571-
data={smsMessages}
572-
renderItem={renderSmsItem}
573-
keyExtractor={(item) => item.id}
574-
style={styles.flatList}
575-
/>
544+
<FlatList data={smsMessages} renderItem={renderSmsItem} keyExtractor={(item) => item.id} style={styles.flatList} />
576545
</View>
577546
)}
578547
</View>
@@ -581,11 +550,8 @@ export default function TelephonySmsPermissionDemo() {
581550
<View style={styles.section}>
582551
<Text style={styles.sectionTitle}>📝 Notes</Text>
583552
<Text style={styles.noteText}>
584-
• Telephony permissions require Play Store declarations{'\n'}
585-
• Call log and SMS reading require native modules in production{'\n'}
586-
• Some features may not work on emulators{'\n'}
587-
• SMS/Call permissions are highly restricted by Google Play{'\n'}
588-
• Phone calls and SMS will open device default apps
553+
• Telephony permissions require Play Store declarations{'\n'}• Call log and SMS reading require native modules in production{'\n'}• Some features may not work
554+
on emulators{'\n'}• SMS/Call permissions are highly restricted by Google Play{'\n'}• Phone calls and SMS will open device default apps
589555
</Text>
590556
</View>
591557
</ScrollView>

0 commit comments

Comments
 (0)